You are on page 1of 55

Additional MATLAB Examples

Chapter 1
x(n) = A cos(0 n)
M 1.1 Write a MATLAB program to generate a sinusoidal sequence and plot
the sequence using the stem function. The input data specified by the user are the desired length
L , amplitude A , and the angular frequency 0 , where 0 < 0 <  . Using this program,
generate the sinusoidal sequences shown in Fig. 1.1.

Solution:
clc; clear all; close all;
L=input('Desired length= ');
A=input('Amplitude= ');
omega=input('Angular frequency= ');
n=0:L-1;
x=A*cos(omega*n);
stem(n,x);
xlabel('n');ylabel('amplitude');
title([' \ omega_0= ',num2str(omega)]);

0 = 0,  / 8,  / 2, 
The result for various frequencies ( ) is shown below:

Fig. 1.1 Results of M 1.1

M 1.2 Generate and plot each of the following sequences over the indicated interval.
10
x1 (n) =  (m  1)[ (n  2m)   (n  2m  1)]
(a) m =0 , 0  n  25 .
x2 (n) = 2 (n  2)   (n)   (n  4) 5  n  5
(b) , .

© Oxford University Press, All rights reserved.


Solution:
clc; clear all; close all;
for n=0:25;
s=0;
for m=0:10
s=s+(m+1)*(((n-2*m)==0)-((n-2*m+1)==0));
end
x1(n+1)=s;
end
n=0:25;
subplot(121);
stem(n,x1,'.');
xlabel('n');
ylabel('amplitude');
n=-5:5;
x2=2*(n+2==0)+(n==0)-(n-4==0);
subplot(122);
stem(n,x2,'.');
xlabel('n');
ylabel('amplitude');

Results of M 1.2:
 

       

Fig. 1.2 Results of M 1.2 

M 1.3 Generate and plot x ( n) = cos(0.3 n) , 20  n  20 . Is this sequence periodic? If it is,
what is its fundamental period? From the examination of the plot, what interpretation can you
k
fd =
give to the integers k and N where N .
Solution: The frequency of the given signal x ( n) = cos(0.3 n) is given by
 = 0.3
 0.3 3 k
fd = = = =
2 2 20 N

© Oxford University Press, All rights reserved.


Therefore, k = 3 and the period N = 20 . MATLAB program is given here:
clc; clear all; close all;
n=-20:20;
x=cos(0.3*pi*n);
stem(n,x,'.');
xlabel('n');
ylabel('amplitude');

Results of M 1.3:

Fig. 1.3 Results of M 1.3

M 1.4 Consider a discrete-time system with input x(n) and output y (n) related by
n 10
y ( n) =  x( k )
k = n 10

x ( n)
Let 1 be a Gaussian random sequence with mean 0 and variance 10 over 0  n  100 .
Using this sequence, test the time invariance of this system. Choose any values for sample shift
n0
in Eq. (1.81). You should use several realizations of this sequence to arrive at your answer.

Solution:
clc; clear all; close all;
%** input x1(n) **
x1=sqrt(10)*randn(1,100);
x11=[zeros(1,10) x1 zeros(1,10)];
%**input x2(n)=x1(n-n0) **
n0=5; % shifting constant
x2=[zeros(1,n0) x1]; % x1(n) shifted by 5
x22=[zeros(1,n0) x11];
%** y1(n) from x1(n) **
for n=1:100
sum1=0;

© Oxford University Press, All rights reserved.


for k=(n-10):(n+10)
sum1=sum1+x11(k+10);
end
y1(n)=sum1;
end
%* y2(n) from x2(n)=x1(n-n0) **
for n=1:(100+n0)
sum2=0;
for k=(n-10):(n+10)
sum2=sum2+x22(k+10);
end
if n<=n0
y2(n)=0;
else
y2(n)=sum2;
end
end
n=0:(100+n0);
subplot(221);stem(x1,'.');
xlabel('n'); ylabel('x_1(n)');
subplot(222);stem(y1,'.');
xlabel('n');ylabel('y_1(n)');
subplot(223);stem(x2,'.');
xlabel('n'); ylabel('x_2(n)=x_1(n-n0)');
subplot(224);stem(y2,'.')
xlabel('n');
ylabel('y_2(n)=y_1(n-n0)');

The result is shown below. Note that whenever we run this program, we get different
plots because this program involves randn commands. Since for each realization
y2 (n) = y1 (n  n0 )
, system is time invariant.

Fig. 1.4 Results of M 1.4

© Oxford University Press, All rights reserved.


© Oxford University Press, All rights reserved.
Chapter 2

M 2.1 Consider an analog signal x (t ) = cos(26 t ) , 0  t  1 . It is sampled at Ts = 0.1 sec


intervals to obtain x(nTs ) . Plot x( nTs ) . Reconstruct the analog signal y (t ) from the samples
x( nTs ) using the sinc interpolation. Determine the frequency of the reconstructed signal.
Comment on the result.

Or

The signals

x1 (t ) = cos(26 t ) and x2 (t ) = cos(6 t )

are both sampled with f s = 10 Hz. Show that the two sequences of samples so obtained are
identical.

Solution: Substituting t = nTs , we obtain

x1 (t ) |t = nT = x1 (nTs ) = x1 (n)
s

= cos(26 nTs )

 13 
= cos  2 n
 10 

 3 
= cos  2 n  2 n
 10 

 3 
= cos  2 n
 10 

x2 (t ) |t = nT = x2 (nTs ) = x2 (n)
s

= cos(6 nTs )

 3 
= cos  2 n = x1 (n)
 10 

© Oxford University Press, All rights reserved.


Hence, x2 (n) = x1 (n) . Thus, the sinusoidal signals are identical and, consequently,
 3 
indistinguishable. If we are given the sampled values generated by cos  2 n , there is some
 10 
ambiguity as to whether these sampled values correspond to x1 (t ) or x2 (t ) . Since x2 (t ) yields
exactly the same values as x1 (t ) when the two are sampled at f s = 10 Hz, we say that the
frequency f 2 = 3 Hz is an alias of the frequency f1 = 13 Hz at the sampling frequency f s = 10
Hz. The result is shown in Fig. 2.1.

clc;clear all; close all;

t=0:0.0005:1;

f=13;fs=10;

xt=cos(2*pi*f*t);

Ts=0.1;

n=0:Ts:1;

xn=cos(2*pi*f*n);

k=0:length(n)-1;

N=length(xn);

Nt=length(t);

xr=zeros(1,Nt);

xp=zeros(1,Nt);

for k=1:N

xr=xr+xn(k)*sinc((t-(k-1)*Ts)/Ts);

end

plot(t,xt);hold on

axis([0 1 -1.2 1.2]);

stem(n,xn,'filled');

hold on;

plot(t,xr);

© Oxford University Press, All rights reserved.


xlabel('Time index');

ylabel('Amplitude');

Fig. 2.1 Results of M 2.1

© Oxford University Press, All rights reserved.


Chapter 3

M 3.1 Write a MTALAB program to compute the convolution of the following two sequences:

x( n) = {2,1, 3, 2,1, 3, 2,1, 3,} , and h( n) = {1,1} . Here x(n) is a periodic sequence.
 

Solution: clc; clear all; close all;

x=[2,1,-3,2,1,-3,2,1,-3,2,1];

h=[1,1];

nx=[0:10];LEx=nx(1);

nh=[0:1];LEh=nh(1);

Nx=length(x);

Nh=length(h);

LEy=LEx+LEh;

REx=nx(Nx);

REh=nh(Nh);

REy=REx+REh;

Ny=Nx+Nh-1;

Xn=[x zeros(1,Ny-Nx)];

Hn=[h zeros(1,Ny-Nh)];

for i=0:Ny-1

sum=0;

for j=0:i

sum=sum+Xn(j+1)*Hn(i-j+1);

end

Y(i+1)=sum;

end

subplot(131);

© Oxford University Press, All rights reserved.


stem(nx,x,'filled');

xlabel('n');

title('x(n)');

subplot(132);

stem(nh,h,'filled');

xlabel('n');

title('h(n)');

subplot(133);

ny=LEy:REy;

stem(ny,Y, 'filled');

xlabel('n');

title('y(n)');

Thus,

y ( n) = {2,3, 2, 1,3, 2, 1,3, 2, 1,}


The response y (n) is periodic with period N = 3 , except for start-up effects (which last for one
period). One period of the convolution is y ( n) = {1, 3, 2} . The result is shown in Fig. 3.1.

Fig. 3.1 Results of M 3.1

© Oxford University Press, All rights reserved.


M 3.2. Time delay estimation in radar (target ranging): Let x ( n) = {3, 11, 7, 0,  1, 4, 2} be a

sampled signal transmitted towards target and y (n) be the received signal.

y (n) = 0.8 x( n  2)  w( n)

where w( n) is Gaussian noise sequence with mean 0 and variance 1 . Compute the cross-
correlation between y (n) and x(n) . Use the plot to estimate the value of the delay D .

Solution: From the construction of y (n) , it follows that y (n) is similar to x ( n  2) and hence
their cross-correlation would show the strongest similarity at m = 2 .

clc; clear all; close all;

%***input signal*******

x=[3 11 7 0 -1 4 2];

nx=-3:3;

%**shifted signal**

xs=x;ny=nx+2;

%**y(n)=x(n-2)+w(n)****

w=randn(1,length(ny));

nw=ny;y=xs+w;

yf=fliplr(y);

%*************

Ryx=conv(x,yf);

N1=length(nx);

N2=length(ny);

n3=(nx(1)+ny(1)):(nx(N1)+ny(N2));

stem(n3,Ryx,'filled');

xlabel('lag variable m');

From Fig. 3.2, we observe that the cross-correlation indeed peaks at m = 2 , which implies that

© Oxford University Press, All rights reserved.


y (n) is similar to x(n) shifted by 2 . Therefore, the value of the delay is D = 2 .

Fig. 3.2 Result of M 3.2

© Oxford University Press, All rights reserved.


Chapter 4

M 4.1. Let x1 (n) be periodic with fundamental period N = 50 , where one period is given by

 ne 0.3n 0  n  25
x1 ( n) = 
 0 26  n  49

and let x2 (n) be periodic with fundamental period N = 100 , where one period is given by

 ne 0.3 n 0  n  25
x2 ( n) = 
 0 26  n  99

These two periodic sequences differ in their periodicity but otherwise have equal non-zero
samples.

(a) Find the DTFS X k of x1 (n) and plot samples of its magnitude and phase versus k .

(b) Find the DTFS X k of x2 (n) and plot samples of its magnitude and phase versus k .

(c) What is the difference between the two DTFS plots of (a) and (b)?

Solution: The MATLAB programs are as follows:

(a) clc; clear all; close all;

N=50;n=0:N-1;

k=0:N-1;

n1=0:25;

xn=[n1.*exp(-0.3*n1) zeros(1,24)];

WN=exp(-j*2*pi/N);

nk=n'*k;

WNnk=WN. (nk);

Xk=(xn*WNnk)/N;

magXk=abs([Xk(N/2+2:N) Xk(1:N/2+1)]);

phaseXk=angle([Xk(N/2+2:N) Xk(1:N/2+1)]);

k1=[-N/2+1:N/2];

© Oxford University Press, All rights reserved.


subplot(121);

stem(k1,magXk,'filled');

xlabel('k');ylabel('|Xk|');

title('Magnitude');

subplot(122);

stem(k1,phaseXk,'filled');

xlabel('k');ylabel('angleXk');

title('Phase');

The result is shown in Fig. 4. 1 (a).

(b) clc; clear all; close all;

N=100;n=0:N-1;

k=0:N-1;

n1=0:25;

xn=[n1.*exp(-0.3*n1) zeros(1,74)];

WN=exp(-j*2*pi/N);

nk=n'*k;

WNnk=WN. (nk);

Xk=(xn*WNnk)/N;

magXk=abs([Xk(N/2+2:N) Xk(1:N/2+1)]);

phaseXk=angle([Xk(N/2+2:N) Xk(1:N/2+1)]);

k1=[-N/2+1:N/2];

subplot(121);

stem(k1,magXk,'filled');

xlabel('k');ylabel('|Xk|');

© Oxford University Press, All rights reserved.


title('Magnitude');

subplot(122);

stem(k1,phaseXk,'filled');

xlabel('k');ylabel('angleXk');

title('Phase');

The result is shown in Fig. 4.1 (b).

(c) The envelope of both figures is same, but Fig. 4.1 (b) is highly dense

Fig, 4.1 (a) Results of M 4.1(a)

Fig, 4.1 (b) Results of M 4.1(b)

© Oxford University Press, All rights reserved.


M 4.2. Write a MATLAB program to find the following periodic signal x(n) is the one period of
the DTFS coefficients:

X k = {5,  2 j ,3,2 j}

Solution: clc; clear all; close all;

N=4;n=0:N-1;

k=0:N-1;

Xk=[5 -2j 3 2j];

WN=exp(-j*2*pi/N);

nk=n'*k;

WNnk=WN. (-nk);

xn=Xk*WNnk;

magxn=abs([xn(N/2+2:N) xn(1:N/2+1)]);

phasexn=angle([xn(N/2+2:N) xn(1:N/2+1)]);

magxn

phasexn

This program produces the following result: magxn =

2.0000 8.0000 6.0000 8.0000

phasexn =

3.1416 0 -0.0000 -0.0000

© Oxford University Press, All rights reserved.


Chapter 5

M 5.1. Let x( n) = {1, 2,3, 4,5, 6, 7,8,9} . Let y ( n) = x ( n  5) . Verify the time-shifting property of

the DTFT.

Solution:

clc; clear all; close all; 

num1=[1 2 3 4 5 6 7 8 9]; 

den=[1]; 

D=5;%Delay 

w=‐pi:0.01:pi; 

Xw=freqz(num1,den,w); 

num2=[zeros(1,D) num1]; 

Yw=freqz(num2,den,w); 

subplot(221); 

plot(w/pi,abs(Xw)); 

xlabel(' \ omega/ \ pi'); 

title('Magnitude spectrum of original sequence'); 

subplot(222); 

plot(w/pi,angle(Xw)); 

xlabel(' \ omega/ \ pi'); 

title('Phase spectrum of original sequence'); 

subplot(223); 

plot(w/pi,abs(Yw)); 

xlabel(' \ omega/ \ pi'); 

title('Magnitude spectrum of shifted sequence'); 

subplot(224); 

© Oxford University Press, All rights reserved.


plot(w/pi,angle(Yw)); 

xlabel(' \ omega/ \ pi'); 

title('Phase spectrum of shifted sequence'); 

The result is shown in Fig. 5.1.

Fig. 5.1 Results of M 5.1


jn
 n
M 5.2. Let x(n) = cos   , 0  n  100 and y (n) = e 4
x(n) . Verify the frequency-shifting
 2
property of the DTFT.

Solution:

clc; clear all; close all; 

n=0:100; 

w=‐pi:0.01*pi:pi; 

x=cos(n*pi/2); 

Xw=x*exp(‐j*n'*w); 

y=exp(j*n*pi/4).*x; 

Yw=y*exp(‐j*n'*w); 

subplot(221); 

© Oxford University Press, All rights reserved.


plot(w/pi,abs(Xw)); 

xlabel(' \ omega/ \ pi'); 

title('Magnitude spectrum of X'); 

subplot(222); 

plot(w/pi,angle(Xw)); 

xlabel(' \ omega/ \ pi'); 

title('Phase spectrum of X'); 

subplot(223); 

plot(w/pi,abs(Yw)); 

xlabel(' \ omega/ \ pi'); 

title('Magnitude spectrum of Y'); 

subplot(224); 

plot(w/pi,angle(Yw)); 

xlabel(' \ omega/ \ pi'); 

title('Phase spectrum of Y'); 

The resulting plot is shown in Fig. 5.2. From plots in Fig. 5.2, we observe that X (e j ) is

indeed shifted by in both magnitude and phase.
4

© Oxford University Press, All rights reserved.


Fig. 5.2 Results of M 5.2

M 5.3. Let x1 ( n) = {1,1,1} and x2 ( n) = {1,1,1} . Verify the convolution property of the DTFT.
 

Solution: See Example 5.15, page 209.

clc; clear all; close all; 

w=‐pi:0.01:pi; 

x1=[1 1 1]; 

x2=[1 1 1]; 

y=conv(x1,x2); 

X1w=freqz(x1,1,w); 

X2w=freqz(x2,1,w); 

X3w=X1w.*X2w; 

Yw=freqz(y,1,w); 

subplot(221); 

plot(w/pi,abs(X3w)); 

xlabel(' \ omega/ \ pi'); 

© Oxford University Press, All rights reserved.


title('Product of Magnitude spectra'); 

subplot(222); 

plot(w/pi,angle(X3w)); 

xlabel(' \ omega/ \ pi'); 

title('Sum of Phase spectra'); 

subplot(223); 

plot(w/pi,abs(Yw)); 

xlabel(' \ omega/ \ pi'); 

title('Magnitude spectrum of convolved sequence'); 

subplot(224); 

plot(w/pi,angle(Yw)); 

xlabel(' \ omega/ \ pi'); 

title('Phase spectrum of convolved sequence'); 

The result is shown in Fig. 5.3.

Fig. 5.3 results of M 5.3

M 5.4. Let x( n) = {1, 2,3, 4,5, 6, 7,8,9} and y ( n) = x (  n) . Verify the time-reversal (folding)

property of the DTFT.

© Oxford University Press, All rights reserved.


Solution:

clc; clear all; close all; 

num1=[1 2 3 4 5 6 7 8 9]; 

den=[1]; 

L=length(num1)‐1; 

w=‐pi:0.01:pi; 

Xw=freqz(num1,den,w); 

num2=fliplr(num1); 

Y=freqz(num2,den,w); 

Yw=exp(j*L*w).*Y; 

subplot(221); 

plot(w/pi,abs(Xw)); 

xlabel(' \ omega/ \ pi'); 

title('Magnitude spectrum of original sequence'); 

subplot(222); 

plot(w/pi,angle(Xw)); 

xlabel(' \ omega/ \ pi'); 

title('Phase spectrum o original sequence'); 

subplot(223); 

plot(w/pi,abs(Yw)); 

xlabel(' \ omega/ \ pi'); 

title('Magnitude spectrum of time‐reversed sequence'); 

subplot(224); 

plot(w/pi,angle(Yw)); 

xlabel(' \ omega/ \ pi'); 

© Oxford University Press, All rights reserved.


title('Phase spectrum of time‐reversed sequence'); 

The resulting plot is shown in Fig. 5.4

Fig. 5.4 Results of M 5.4

M 5.5. Determine the frequency response H (e j ) of a system characterized by


n
h(n) = (0.9) u(n) . Plot the magnitude and the phase response.

Solution:

clc; clear all; close all; 

w=‐pi:0.01:pi; 

n=0:100; 

xn=(0.9).n; 

Xw=xn*exp(‐j*n'*w); 

MagX=abs(Xw); 

PhaseX=angle(Xw); 

subplot(121); 

© Oxford University Press, All rights reserved.


plot(w/pi,MagX); 

xlabel(' \ omega/ \ pi'); 

ylabel('Magnitude'); 

subplot(122); 

plot(w/pi,PhaseX); 

xlabel(' \ omega/ \ pi'); 

ylabel('Phase'); 

The resulting plot is shown in Fig. 5.5.

Fig. 5.5 Results of M 5.5

© Oxford University Press, All rights reserved.


Chapter 7
M 7.1. A first-order FIR LPF is described by the difference equation

1
y (n) = [ x(n)  x(n  1)]
2

Find H ( z) and sketch its pole-zero plot. Plot the magnitude response | H (e j ) | and phase
response H (e j ) .

Solution:

Y ( z) 1
= H ( z ) = [1  z 1 ]
X ( z) 2

1
H (e j ) = [1  e  j ]
2

The system has one zero at z = 1 and one pole at z = 0 .

clc;clear all;close all;

w=0:0.01:pi;

b=[0.5 0.5];a=[1];

subplot(311);zplane(b,a)

Hw=(1/2)*(1+exp(-j*w));

subplot(312);plot(w/pi,abs(Hw)); xlabel('frequency in pi
units);

ylabel('magnitude')

subplot(313);

plot(w/pi,angle(Hw)*180/pi);

xlabel('frequency in pi units');

ylabel('phase (degrees)')

We can also use freqz command to determine the frequency response.

© Oxford University Press, All rights reserved.


clc;clear all;close all;

w=0:0.01:pi;

b=[0.5 0.5];a=[1];

subplot(311);zplane(b,a)

Hw=freqz(b,a,w);

subplot(312);plot(w/pi,abs(Hw)); xlabel('frequency in pi
units);

ylabel('magnitude')

subplot(313);

plot(w/pi,angle(Hw)*180/pi);

xlabel('frequency in pi units');

ylabel('phase (degrees)')

The resulting plot is shown in Fig 7.4

1.

© Oxford University Press, All rights reserved.


Fig. 7. 1 Results of M 7.1

M 7.2. A first-order FIR HPF is described by the difference equation

1
y (n) = [ x(n)  x(n  1)]
2

Find H ( z) and sketch its pole-zero plot. Plot the magnitude response | H (e j ) | and phase
response H (e j ) .

Solution:

Y ( z) 1
= H ( z ) = [1  z 1 ]
X ( z) 2

1
H (e j ) = [1  e  j ]
2

The system has one zero at z = 1 and one pole at z = 0 .

clc;clear all;close all;

w=0:0.01:pi;

b=[0.5 -0.5];a=[1];

subplot(311);zplane(b,a)

Hw=(1/2)*(1-exp(-j*w));

subplot(312);plot(w/pi,abs(Hw)); xlabel('frequency in pi
units);

ylabel('magnitude')

subplot(313);

plot(w/pi,angle(Hw)*180/pi);

xlabel('frequency in pi units');

ylabel('phase (degrees)')

The resulting plot is shown in Fig 7. 2

© Oxford University Press, All rights reserved.


Fig. 7.2 Results of M 7.2

M 7.3. A first-order FIR band-pass filter is described by the difference equation

1
y (n) = [ x(n)  x(n  2)]
2

Find H ( z) and sketch its pole-zero plot. Plot the magnitude response | H (e j ) | and phase
response H (e j ) .

Solution:

Y ( z) 1
= H ( z ) = [1  z 2 ]
X ( z) 2

1
H (e j ) = [1  e  j 2 ]
2

The system has two zero at z = 1 and two poles at z = 0 .

clc;clear all;close all;

w=0:0.01:pi;

© Oxford University Press, All rights reserved.


b=[0.5 0 -0.5];a=[1];

subplot(311);zplane(b,a)

Hw=(1/2)*(1-exp(-j*2*w));

subplot(312);plot(w/pi,abs(Hw)); xlabel('frequency in pi
units);

ylabel('magnitude')

subplot(313);

plot(w/pi,angle(Hw)*180/pi);

xlabel('frequency in pi units');

ylabel('phase (degrees)')

The resulting plot is shown in Fig 7. 3.

Fig. 7. 3 Results of M 7.3

© Oxford University Press, All rights reserved.


M 7.4. A first-order FIR band-stop filter is described by the difference equation

1
y (n) = [ x(n)  x(n  2)]
2

Find H ( z) and sketch its pole-zero plot. Plot the magnitude response | H (e j ) | and phase
response H (e j ) .

Solution:

Y ( z) 1
= H ( z ) = [1  z 2 ]
X ( z) 2

1
H (e j ) = [1  e  j 2 ]
2

The system has two zeros at z =  j and two poles at z = 0 .

clc;clear all;close all;

w=0:0.01:pi;

b=[0.5 0 0.5];a=[1];

subplot(311);zplane(b,a)

Hw=(1/2)*(1+exp(-j*2*w));

subplot(312);plot(w/pi,abs(Hw)); xlabel('frequency in pi
units);

ylabel('magnitude')

subplot(313);

plot(w/pi,angle(Hw)*180/pi);

xlabel('frequency in pi units');

ylabel('phase (degrees)')

© Oxford University Press, All rights reserved.


The resulting plot is shown in Fig 7.4.

Fig. 7. 4 Results of M 7.4

M 7.5. A first-order IIR LPF is described by the system function

1  a 1  z 1
H ( z) =
2 1  az 1

Plot the magnitude response | H (e j ) | for three different values of a = 0.9,0.7,0.4 .

Solution: clc;clear all;close all;

w=0:0.01:pi;

a1=0.9;a2=0.7;a3=0.4

Hw1=((1-a1)/2)*((1+exp(-j*w))...

./(1-a1*exp(-j*w)));

Hw2=((1-a2)/2)*((1+exp(-j*w))...

© Oxford University Press, All rights reserved.


./(1-a2*exp(-j*w)));

Hw3=((1-a3)/2)*((1+exp(-j*w))...

./(1-a3*exp(-j*w)));

plot(w/pi,abs(Hw1),'--',...

w/pi,abs(Hw2),':',...

w/pi,abs(Hw3));

xlabel('frequency in pi units'); ylabel('magnitude');

legend('a=0.9', 'a=0.7', 'a=0.4');

The resulting plot is shown in Fig 7. 5.

Fig. 7. 5 Results of M 7.5

M 7.6. A first-order IIR HPF is described by the system function

1  a 1  z 1
H ( z) =
2 1  az 1

© Oxford University Press, All rights reserved.


Plot the magnitude response | H (e j ) | and phase response H (e j ) for three different values of
a = 0.9,0.7,0.4 .

Solution: clc;clear all;close all;

w=0:0.01:pi;

a1=0.9;a2=0.7;a3=0.4

Hw1=((1+a1)/2)*((1-exp(-j*w))...

./(1-a1*exp(-j*w)));

Hw2=((1+a2)/2)*((1-exp(-j*w))...

./(1-a2*exp(-j*w)));

Hw3=((1+a3)/2)*((1-exp(-j*w))...

./(1-a3*exp(-j*w)));

plot(w/pi,abs(Hw1),'--',...

w/pi,abs(Hw2),':',...

w/pi,abs(Hw3));

xlabel('frequency in pi units'); ylabel('magnitude');

legend('a=0.9', 'a=0.7', 'a=0.4');

The resulting plot is shown in Fig 7. 6.

© Oxford University Press, All rights reserved.


Fig. 7. 6 Results of M 7.6

M 7.7. A second-order IIR band-pass filter is described by the system function

1 a 1  z 2
H ( z) =
2 1  b(1  a) z 1  az 2

(a) Plot the magnitude response | H (e j ) | for three different values of a = 0.3,0.6,0.9 and
b = 0.35 .

(b) Plot the magnitude response | H (e j ) | for three different values of b = 0.2,0.6,0.9
and a = 0.7 .

Solution: clc;clear all;close all;

w=0:0.01:pi; a1=0.9;a2=0.6;a3=0.3; b=0.35;

num=(1-exp(-j*2*w));

den1=(1-b*(1+a1)*exp(-j*w)...

+a1*exp(-j*2*w));

den2=(1-b*(1+a2)*exp(-j*w)...

+a2*exp(-j*2*w));

© Oxford University Press, All rights reserved.


den3=(1-b*(1+a3)*exp(-j*w)...

+a3*exp(-j*2*w));

Hw1=((1-a1)/2)*(num./den1);

Hw2=((1-a2)/2)*(num./den2);

Hw3=((1-a3)/2)*(num./den3);

a=0.7;b1=0.9;b2=0.6;b3=0.2;

den4=(1-b1*(1+a)*exp(-j*w)...

+a*exp(-j*2*w));

den5=(1-b2*(1+a)*exp(-j*w)...

+a*exp(-j*2*w));

den6=(1-b3*(1+a)*exp(-j*w)...

+a*exp(-j*2*w));

Hw11=((1-a)/2)*(num./den4);

Hw21=((1-a)/2)*(num./den5);

Hw31=((1-a)/2)*(num./den6);

subplot(211);

plot(w/pi,abs(Hw1),'--',...

w/pi,abs(Hw2),':',...

w/pi,abs(Hw3));

xlabel('frequency in pi units'); ylabel('magnitude');

legend('a=0.9', 'a=0.6', 'a=0.3');

subplot(212);

plot(w/pi,abs(Hw11),'--',...

w/pi,abs(Hw21),':',w/pi,abs(Hw31));

xlabel('frequency in pi units'); ylabel('magnitude');

© Oxford University Press, All rights reserved.


legend('b=0.9', 'b=0.6', 'b=0.2');

The resulting plots are shown in Fig. 7. 7.

Fig. 7. 7 Results of M 7.7

M 7.8. A second-order IIR band-stop filter is described by the system function

1  a 1  2bz 1  z 2
H ( z) =
2 1  b(1  a) z 1  az 2

(a) Plot the magnitude response | H (e j ) | and phase response H (e j ) for three different
values of a = 0.3,0.6,0.9 and b = 0.6 .

(b) Plot the magnitude response | H (e j ) | for three different values of b = 0.2,0.6,0.9
and a = 0.7 .

Solution: clc;clear all;close all;

w=0:0.01:pi;

a1=0.9;a2=0.6;a3=0.3; b=0.6;

num=(1-2*b*exp(-j*w)+exp(-j*2*w));

den1=(1-b*(1+a1)*exp(-j*w)...

© Oxford University Press, All rights reserved.


+a1*exp(-j*2*w));

den2=(1-b*(1+a2)*exp(-j*w)...

+a2*exp(-j*2*w));

den3=(1-b*(1+a3)*exp(-j*w)...

+a3*exp(-j*2*w));

Hw1=((1+a1)/2)*(num./den1);

Hw2=((1+a2)/2)*(num./den2);

Hw3=((1+a3)/2)*(num./den3);

a=0.7;b1=0.9;b2=0.6;b3=0.2;

num4=(1-2*b1*exp(-j*w)+exp(-j*2*w));

num5=(1-2*b2*exp(-j*w)+exp(-j*2*w));

num6=(1-2*b3*exp(-j*w)+exp(-j*2*w));

den4=(1-b1*(1+a)*exp(-j*w)...

+a*exp(-j*2*w));

den5=(1-b2*(1+a)*exp(-j*w)...

+a*exp(-j*2*w));

den6=(1-b3*(1+a)*exp(-j*w)...

+a*exp(-j*2*w));

Hw11=((1+a)/2)*(num4./den4);

Hw21=((1+a)/2)*(num5./den5);

Hw31=((1+a)/2)*(num6./den6);

subplot(211);

plot(w/pi,abs(Hw1),'--',...

w/pi,abs(Hw2),':',w/pi,abs(Hw3));

xlabel('frequency in pi units'); ylabel('magnitude');

© Oxford University Press, All rights reserved.


legend('a=0.9', 'a=0.6', 'a=0.3');

subplot(212);

plot(w/pi,abs(Hw11),'--',...

w/pi,abs(Hw21),':',w/pi,abs(Hw31));

xlabel('frequency in pi units'); ylabel('magnitude');

legend('b=0.9', 'b=0.6', 'b=0.2');

The resulting plots are shown in Fig. 7. 8.

Fig. 7. 8 Results of M 7.8

M 7.9. Consider a third-order all-pass filter described by the system function

0.2  0.18 z 1  0.4 z 2  z 3


H ( z) =
1  0.4 z 1  0.18 z 2  0.2 z 3

© Oxford University Press, All rights reserved.


Plot the pole-zero plot, magnitude response | H (e j ) | , and phase response H (e j ) .

Solution: clc;clear all;close all;

w=0:0.01:pi;

b=[-0.2 0.18 0.4 1];

a=[1 0.4 0.18 -0.2];

Hapw=freqz(b,a,w);

subplot(221);zplane(b,a);

subplot(222);

plot(w/pi,abs(Hapw));

xlabel('frequency in pi units'); ylabel('magnitude');

subplot(223);

plot(w/pi,angle(Hapw)*180/pi);

xlabel('frequency in pi units'); ylabel('Phase (degrees)');

title('Principal value of phase');

subplot(224);

plot(w/pi,unwrap(angle(Hapw))*180/pi);

xlabel('frequency in pi units'); ylabel('Phase (degrees)');

title('Unwrapped phase');

The resulting plots are shown in Fig. 7. 9.

© Oxford University Press, All rights reserved.


Fig. 7.9 Results of M 7.9

© Oxford University Press, All rights reserved.


Chapter 8

M 8.1 Consider a nine-point sequence

x( n) = {1, 2,3, 4,5, 6, 7,8,9}


(a) Decompose and plot the xce ( n) xce ( n) and xco (n) xco (n) components of x(n) x(n) .

(b) Verify the property in Eqs (8.86) and (8.88)

Solution: clc; clear all; close all;

N = 9; n = 0:N-1;

x = [1 2 3 4 5 6 7 8 9];

%***** xce(n)******

n1 = mod(-n,N);

y1 = x(n1+1);

xcen = (x+y1)/2;

subplot(231);

stem(n,xcen,'filled');

xlabel('n'); title('xce(n)');

%***** xco(n)******

n1 = mod(-n,N);

y1 = x(n1+1);

xcon = (x-y1)/2;

subplot(232);

stem(n,xcon,'filled');

xlabel('n'); title('xco(n)');

%****** DFT[x(n)]*****

k = 0:N-1;

© Oxford University Press, All rights reserved.


WN = exp(-j*2*pi/N*n'*k);

Xk = x*WN; subplot(233);

stem(k,real(Xk),'filled');

xlabel('k');

title('Real[X(k)]');

subplot(234);

stem(k,imag(Xk),'filled');

xlabel('k');

title('Imag[X(k)]');

%***** DFT[xce(n)]******

WN = exp(-j*2*pi/N*n'*k);

Xcek = xcen*WN;

subplot(235);

stem(k,Xcek,'filled');

xlabel('k');

ylabel('magnitude');

title('DFT[xce(n)]');

%***** DFT[xco(n)]******

WN = exp(-j*2*pi/N*n'*k);

Xcok = xcon*WN;

subplot(236);

stem(k,imag(Xcok),'filled');

xlabel('k');

ylabel('magnitude');

title('DFT[xco(n)]');

© Oxford University Press, All rights reserved.


The resulting plot is shown in Fig. 8.1.

Fig. 8.1 Results of M 8.1

M 8.2 Given the sinusoid

 n   n 
x (n) = 2 cos  2000  = 2 cos  
 8000   4

obtained by sampling the analog signal

x (t ) = 2 cos(2000 t )

with a sampling rate of f s = 8000 f s = 8000 Hz.

(a) Write a MATLAB program to compute the spectrum of a rectangular window


function with a window size = 50 = 50 .

(b) Write a MATLAB program to compute the spectrum of a Hamming window function
with a window size = 100 = 100 .

(c) Write a MATLAB program to compute the spectrum of a Hanning window function
with a window size = 150 = 150 .

Solution: (a) clc; clear all; close all;

© Oxford University Press, All rights reserved.


%* generate the sine wave sequence

fs=8000; N=50; n=0:N-1;

x=2*sin(2000*pi*[0:1:N-1]/fs);

%**rectangular windowed sequence **

w=boxcar(N);

xw=x.*w';

%** DFT algorithm *****

k=0:N-1;

WN=exp(-j*2*pi/N*n'*k);

Xk=xw*WN;

Xf=abs(Xk)/N;

% Map the frequency bin k to f (Hz)

f=k*fs/N;

subplot(121);

plot(f,Xf);

xlabel('frequency');

title('Spectrum upto sampling frequency');

%** frequencies upto the folding frequency

f=[0:1:N/2]*fs/N;

subplot(122);

plot(f,Xf(1:N/2+1));

xlabel('frequency');

title('Spectrum upto folding frequency');

The resulting plot is shown in Fig. 8.2(a).

© Oxford University Press, All rights reserved.


(b) clc; clear all; close all;

%* generate the sine wave sequence

fs=8000; N=100; n=0:N-1;

x=2*sin(2000*pi*[0:1:N-1]/fs);

%**Hamming windowed sequence **

w=hamming(N);

xw=x.*w';

%** DFT algorithm *****

k=0:N-1;

WN=exp(-j*2*pi/N*n'*k);

Xk=xw*WN;

Xf=abs(Xk)/N;

% Map the frequency bin k to f (Hz)

f=k*fs/N;

subplot(121);

plot(f,Xf);

xlabel('frequency');

title('Spectrum upto sampling frequency');

%** frequencies upto the folding frequency

f=[0:1:N/2]*fs/N;

subplot(122);

plot(f,Xf(1:N/2+1));

xlabel('frequency');

title('Spectrum upto folding frequency');

The resulting plot is shown in Fig. 8.2(b).

© Oxford University Press, All rights reserved.


(c) clc; clear all; close all;

%* generate the sine wave sequence

fs=8000; N=150; n=0:N-1;

x=2*sin(2000*pi*[0:1:N-1]/fs);

%**Hanning windowed sequence **

w=hann(N);

xw=x.*w';

%** DFT algorithm *****

k=0:N-1;

WN=exp(-j*2*pi/N*n'*k);

Xk=xw*WN;

Xf=abs(Xk)/N;

% Map the frequency bin k to f (Hz)

f=k*fs/N;

subplot(121);

plot(f,Xf);

xlabel('frequency');

title('Spectrum upto sampling frequency');

%** frequencies upto the folding frequency

f=[0:1:N/2]*fs/N;

subplot(122);

plot(f,Xf(1:N/2+1));

xlabel('frequency');

title('Spectrum upto folding frequency');

© Oxford University Press, All rights reserved.


The resulting plot is shown in Fig. 8.2(c).

Fig. 8.2 (c) Results of M 8.2 (c)

© Oxford University Press, All rights reserved.


Chapter 9
x(n) = {1,1,1,1}
M 9.1 Compute the 64-point DFT of the sequence  using fft command. Use
fftshift command to shift the magnitude response.

Solution: fftshift command is used to shift zero-frequency component to centre of spectrum, that
N
is, it shifts the DFT by 2 -point. This command is useful for visualizing the Fourier transform
with the zero-frequency component in the middle of the spectrum. This command uses the
frequency-shifting property.
2 2 N
j k n j n

Since e
N 0
x( n) = e N 2
x ( n) = e j n x( n) = ( 1) n x ( n) , we have
DFT
x( n)  X ( k )
N
2 DFT
j ko n
e N
x ( n)  X (  k  k o  N )
N
2 N
j n DFT  N 
e N 2
x (n)  X  k  
N  2 N

clc; clear all; close all;


x=ones(1,4);
X32f=fft(x,64);
shX32f=fftshift(X32f);
N=64,k=0:N-1;
subplot(121);
stem(k,abs(X32f),'filled');
xlabel('k'); ylabel('|X(k)|');
title('64-point DFT');
subplot(122);
stem(k,abs(shX32f),'filled');
xlabel('k'); ylabel('|X(k)|');
title('64-point DFT using fftshift command');

The resulting plot is shown in Fig. 9.1.

© Oxford University Press, All rights reserved.


Fig. 9.1 results of M 9.1

© Oxford University Press, All rights reserved.


Chapter 10
1 1
K1 = K2 =
M 10.1 Let the lattice coefficients of an FIR lattice structure be K 0 = 2 , 4, 2 , and
1
K3 =
3 . Determine the FIR filter coefficients for the direct form structure.

Solution: clc; clear all; close all;

K0=2;K1=1/4;K2=1/2;K3=1/3;

K=[K1 K2 K3];

M=length(K);

a=diag(K);

for m=2:M

for k=1:m-1

a(m,k)=a(m-1,k)+a(m,m)*a(m-1,m-k);

end

end

a=K0.*[1 a(3,:)]

This program give the following result:

a=

2.0000 1.0833 1.2500 0.6667

13 5 2
a3 (1) = 1.0833 = a3 (2) = 1.25 = a3 (3) = 0.6667 =
a3 (0) = 2 , 12 , 4 , and 3.
Hence

M 10.2 Consider an all-pole IIR filter given by

1
H ( z) =
13 1 5 2 1 3
1 z  z  z
24 8 3

Determine the lattice parameters. Comment on its stability.

© Oxford University Press, All rights reserved.


Solution: clc; clear all; close all;

%* direct form filter coefficients *

A=[1 13/24 5/8 1/3];

M=length(A);

K(1)=A(1);

A=A/A(1);

for m=M:-1:2

K(m)=A(m);

B=fliplr(A);

A=(A-K(m)*B)/(1-K(m)*K(m));

A=A(1:m-1);

end

K(2:M)

This program gives the following result:

K=

0.2500 0.5000 0.3333

1 1 1
K1 = K2 = K3 =
Hence 4, 2 , and 3 . The all-pole lattice structure is shown in Fig. 10.2.

Fig. 10.2 Third-order all-pole lattice structure for M 10.2

© Oxford University Press, All rights reserved.


Chapter 11

M 11.1 Determine the amplitude response H r (e j ) and locations of zeros of H ( z) for the given

impulse response

h(n) = {3,  1, 7,4,5,4, 7, 1,3}

11  1
Solution: Since h(n) is symmetric about  p = = 5 and M = 9 , this is a Type-1 linear-
2

phase FIR filter. From Eq. (11.20), we have

a(0) = h(5), a(k ) = 2h(5  k ), 1  k  5

From Eq. (11.22), we have

5
H r (e j ) = a (k ) cos k
k =0

The MATLAB function freqz computes the frequency response, but we cannot determine the

amplitude response from it.

clc; clear all; close all;

h=[3 -1 -7 4 5 4 -7 -1 3];

M=length(h);n=0:M-1;

L=(M-1)/2;

a=[h(L+1) 2*h(L:-1:1)];

w=0:0.01:pi;

k=0:L;

Hr=cos(w'*k)*a';

subplot(221);stem(n,h,'filled');

xlabel('n');ylabel('h(n)');

title('Impulse response');

© Oxford University Press, All rights reserved.


subplot(222);plot(w/pi,Hr)

xlabel('frequency in pi units'); ylabel('Hr');

title('Amplitude response')

subplot(223);stem(k,a,'filled');

xlabel('n');ylabel('a(n)');

title('a(n) coefficients')

subplot(224);zplane(h,1)

xlabel('real axis');

ylabel('imaginary axis');

title('pole-zero plot')

The resulting plot is shown in Fig. 11.1.

Fig. 11.1 Results of M 11.1

© Oxford University Press, All rights reserved.


Chapter 12
M 12.1 Write a MATLAB program to design a Chebyshev digital IIR LPF using bilinear transformation
method to satisfy the following specifications.

0.8  H (e jw )  1   0.2

H (e jw )  0.2 0.32    

Assume T = 1 sec.

Solution: See Example 12.46, page 773.

clc; clear all; close all;

T=1; Ap=-20*log10(0.8);

As=-20*log10(0.2);

wp=0.2*pi;ws=0.32*pi;

Wp=(2/T)*tan(wp/2);%analog passband edge freq

Ws=(2/T)*tan(ws/2);%analog stopband edge freq

N Wc=cheb1ord(Wp, Ws, Ap, As, 's')

b,a = cheby1(N,Ap,Wp,'s');

Hs=tf(b,a)

numd,dend=bilinear(b,a,1/T);

Hz=tf(numd,dend,T)

w=0:0.01:pi;

Hw=freqz(numd,dend,w);

subplot(121);

plot(w/pi,abs(Hw));

xlabel('frequency in pi units');

ylabel('Magnitude');

subplot(122);

© Oxford University Press, All rights reserved.


plot(w/pi,20*log10(abs(Hw)));

xlabel('frequency in pi units');

ylabel('Magnitude (dB)');

The results are N=3, Wc=0.6498.

Analog filter system function:

0.09147
s  0.4867s  0.4351 s  0.09147
3 2

Digital filter system function:

0.008386z3  0.02516z 2  0.02516 z  0.008386


z 3  2.274 z 2  1.967 z  0.6263

The magnitude response of the digital filter is shown in Fig. 12.1.

Fig. 12.1 Results of M 12.1

© Oxford University Press, All rights reserved.

You might also like