Lab No2
Lab No2
Code:
% Program P8_1
w = 0:2*pi/1023:2*pi;
num = (1/M)*ones(1,M);
den = [1];
subplot(2,1,1)
plot(w/pi,abs(h));
grid
xlabel('\omega /\pi');
ylabel('Amplitude');
subplot(2,1,2)
plot(w/pi,angle(h));
grid
title('Phase Spectrum arg[H(e^{j\omega})]')
xlabel('\omega /\pi');
ylabel('Phase in radians');
Graph:
For M=3
For M=7:
For M=10:
The types of symmetries exhibited by the magnitude and phase spectra are due to - The impulse
response is real. Therefore, the frequency response is periodically conjugate symmetric, the
magnitude response is periodically even symmetric, and the phase response is periodically odd
symmetric. The type of filter represented by the moving average filter is – This is a lowpass filter.
The results of Question Q10.1 can now be explained as follows – It is a lowpass filter. The input
was a sum of two sinusoids, one high frequency and one low frequency. The particular results
depend on the filter length M, but the general result is that the higher frequency sinusoidal
input component is attenuated more than the lower frequency sinusoidal input component.
Q8.2:
Using the Program P8_1 compute and plot the frequency response of a causal LTI discrete-time
system with a transfer function given by
Code:
% Program P8_2
w = 0:pi/1023:pi;
subplot(2,1,1)
plot(w/pi,abs(h));
grid
xlabel('\omega /\pi');
ylabel('Amplitude');
subplot(2,1,2)
plot(w/pi,angle(h));
grid
xlabel('\omega /\pi');
ylabel('Phase in radians');
Q8.3:
Repeat Question Q8.2 for the following transfer function:
What is the difference between the two filters of Eqs. (10.36) and (10.37), respectively? Which
one will you choose for filtering and why?
Code:
% Program P8_3
w = 0:pi/1023:pi;
subplot(2,1,1)
plot(w/pi,abs(h));
grid
xlabel('\omega /\pi');
ylabel('Amplitude');
subplot(2,1,2)
plot(w/pi,angle(h));
grid
xlabel('\omega /\pi');
ylabel('Phase in radians');
Graph:
The type of filter represented by this transfer function is - Bandpass. The difference between the
two filters of Questions 8.2 and 8.3 is - The magnitude responses are the same. The phase
responses might look very different to you at first, but they are actually similar. The phase
response of the second filter exhibits a branch cut of the arctangent function at normalized
frequency 0.4, which is right in the middle of the passband. However, the unwrapped phase
would not show this discontinuity. Both filters have an approximately linear phase in the
passband and their group delays are approximately the negatives of one another. However, the
two filters have different poles. The poles of the first filter are INSIDE the unit circle, whereas
those of the second filer are OUTSIDE the unit circle. Thus, in a causal implementation, the first
filter would be BIBO STABLE, whereas the second filter would be UNSTABLE. Therefore, in most
applications the second filter would be preferable. I shall choose the filter of Eq. 10.36 for the
following reason - It can be both causal and BIBO stable, whereas the filter of Eq. 10.37 cannot
be both because the two poles are both outside of the unit circle.
Q8.4:
Using MATLAB compute and plot the group delay of the causal LTI discrete-time system with a
transfer function given by
for 0 ≤ ω ≤ π.
Code:
% Program P10_4
w = 0:pi/1023:pi;
plot(w/pi,abs(h));
grid
xlabel('\omega /\pi');
ylabel('Group Delay');
Graph:
From this plot we make the following observations: Usually, it is desirable for a filer to have an
approximately linear phase in the passband, which is the same thing as an approximately
constant group delay in the passband. This filter is notch filter; it is a bandstop filter with a narrow
stopband centered at a normalized frequency just below 0.3. From the graph above, we see that
the group delay is approximately constant over much of the passband.
Q8.5:
Using the program, compute and plot the first 100 samples of the impulse responses of the two
filters of Eqs. (8.36) and (8.37), respectively.
Comment on your results.
Code: (a)
% Program q8_5a
% find and plot the first L samples of the impulse response, where
clf;
% initialize
[g t] = impz(num,den,L);
%plot the impulse response
stem(t,g);
ylabel('h[n]');
Graph:
Code: (b)
% Program Q8_5b
% find and plot the first L samples of the impulse response, where
clf;
% initialize
[g t] = impz(num,den,L);
stem(t,g);
Graph:
From these plots we make the following observations: These plots generated by impz give the
impulse response for a causal realization of H(z). As we observed in Q8.3, the causal filter with
H(z) given in (8.36) is BIBO stable, implying that h[n] is absolutely summable, and we see
alternation and exponential decay in the impulse response. On the other hand, the causal filer
with H(z) given in (8.37) has poles outside the unit circle and is unstable. Not surprisingly,
corresponding h[n] shown above displays exponential growth with n.
Q8.6:
Using zplane develop the pole-zero plots of the two filters of Eqs. (10.36) and (10.37), respectively.
Comment on your results.
Code: (a)
% Program Q8 _6a
num=[0.15 0 -0.15];
xlabel('Real Part');
ylabel('Imaginary Part');
Graph:
Code: (b)
% Program Q8_6b
w=0:pi/1023:pi;
num=[0.15 0 -0.15];
xlabel('Real Part');
ylabel('Imaginary Part');
Graph:
From these plots we make the following observations: As we have said repeatedly by this time,
the upper figure shows that the filter in (10.36) has poles inside the unit circle and therefore it’s
causal realization is stable. The lower figure shows that the filter in (10.37) has poles outside the
unit circle; its causal realization is unstable.
Q8.7:
Compute and plot the impulse response of the approximation to the ideal low-pass filter using
Program P8_2. What is the length of the FIR low-pass filter? Which statement in Program P8_2
determines the filter length? Which parameter controls the cutoff frequency?
Code:
% Program P8_7
fc = 0.25;
n = [-6.5:1:6.5];
y = 2*fc*sinc(2*fc*n);
k = n+6.5;
stem(k,y);
title('N = 13');
ylabel('Amplitude');
grid
Graph:
Q8.8:
Modify Program P8_2 to compute and plot the impulse response of the FIR low-pass filter of Eq.
(8.39) with a length of 20 and an angular cutoff frequency of ωc = 0.45.
Code:
% Program P10_8
clf;
fc = 0.45;
n = [-9.5:1:9.5];
y = 2*fc*sinc(2*fc*n);
k = n+9.5;
stem(k,y);
title('N = 19');
ylabel('Amplitude');
grid;
Graph:
Q8.9:
Modify Program P8_2 to compute and plot the impulse response of the FIR low-pass filter of Eq.
(8.39) with a length of 15 and an angular cutoff frequency of ωc = 0.65.
Code:
% Program P8_9
fc = 0.65;
n = [-7.0:1:7.0];
y = 2*fc*sinc(2*fc*n);
k = n+7.0;
stem(k,y);
title('N = 14');
ylabel('Amplitude');
grid
Graph:
Q8.10:
Write a MATLAB program to compute and plot the amplitude response of the FIR low-pass filter
of Eq. (8.39). Using this program, plot the amplitude response for several values of N and
comment on your results.
Code:
% Program P8_10
% Compute and plot the amplitude response of Truncated Ideal Lowpass Filter
clear;
No2 = N/2;
fc = 0.25;
n = [-No2:1:No2];
y = 2*fc*sinc(2*fc*n);
w = 0:pi/511:pi;
plot(w/pi,abs(h));
grid;
title(strcat('|H(e^{j\omega})|, N=',num2str(N)));
xlabel('\omega /\pi');
ylabel('Amplitude');
Graph:
From these plots we can make the following observations – As the filter length increases, the
transition from passband to stopband becomes steeper; i.e., the transition band becomes more
narrow. We also see the Gibb’s phenomena: as the filter order increases, the amplitude response
converges towards the ideal lowpass characteristic. However, the convergence is only in weak
sense – pointwise convergence. The peak ripple at the transition band edges does not decrease
despite the increases in order.
Q8.11:
Run Program P8_3 to compute and plot the gain response of a length-2 moving average filter.
From the plot verify that the 3-dB cutoff frequency is at π/2.
Code:
% Program P8_11
clf;
M = 2;
num = ones(1,M)/M;
[g,w] = gain(num,1);
plot(w/pi,g);grid
Graph:
From the plot it can be seen that the 3-dB cutoff frequency is at - /2 rad/sample.
Q8.12:
Modify Program P8_3 to compute and plot the gain response of a cascade of K length-2 moving-
average filters. Using the modified program plot the gain response for a cascade of 3 sections
and verify that the 3-dB cutoff frequency of the cascade is as given by Eq. (8.41).
Code:
% Program P8_12
clear;
Hz = [1];
for i=1:K;
Hz = conv(Hz,[1 1]);
end;
Hz = (0.5)^K * Hz;
% Convert numerator to dB
[g,w] = gain(Hz,1);
ThreedB = -3*ones(1,length(g));
t1 = 2*acos((0.5)^(1/(2*K)))*ones(1,512)/pi;
t2 = -50:50.5/511:0.5;
plot(w/pi,g,w/pi,ThreedB,t1,t2);grid;
Graph:
From the plot it can be seen that the 3-dB cutoff frequency of the cascade is at - 0.30015
Q8.13:
Modify Program P8_3 to compute and plot the gain response of the high-pass filter of Eq. (8.42).
Run the modified program to plot the gain response for M = 5 and determine its 3-dB cutoff
frequency from the plot.
Code:
% Program P8_13
clear;
n = 0:M-1;
[g,w] = gain(num,1);
plot(w/pi,g);grid;
ylabel('Gain in dB');
Graph:
From the plot we can see that the 3-dB cutoff frequency is at – approximately 0.8196