Solutions of Matlab
Solutions of Matlab
7
1. i) Output
10
10
Hr
h[n]
5
0
-10
-5
0
5
n
a[n] coeffs
-20
10
10
0.5
freq in pi units
Pole-zero plot
Imaginary Part
5
a[n]
0
-5
0.5
10
0
-0.5
-1
5
n
10
-1
0
Real Part
ii) Program:
bmax = max(b)+1;
bmin = min(b) - 1;
subplot (221) , stem(n,h) , axis ( [ - 1 2*L+1 bmin bmax]) ;
xlabel '(n'), ylabel('h[n]'), title('Impulse Response');
subplot(222), plot(w/pi, Hr), grid on;
xlabel('freq in pi units'), ylabel('Hr'),
title('Type-2 Amplitude Response');
subplot(223), stem(1:L, b), axis([-1 2*L+1 bmin bmax]);
xlabel('n'), ylabel('b[n]'), title('b[n] coeffs');
subplot(224), zplane(h,1), title('Pole-zero plot');
Output:
Enter the values of h
[-4, 1, -1 -2, 5, 6, 6, 5, -2, -1, 1, -4]
Impulse Response
10
20
Hr
h[n]
5
0
-5
0
-20
10
0.5
freq in pi units
Pole-zero plot
n
b[n] coeffs
1
Imaginary Part
10
5
b[n]
0
-5
0.5
11
0
-0.5
-1
10
-1
iii) Program:
0
Real Part
n = 0:N- 1;
% for type- 3
L = (N- 1)/2%
; order of Hr
c = 2*h(L+1:- 1:1)%; type-3 LPF coeffs
i = 0:1 :L ;
w = [0 :1 :500] ' *p i / 500
% ;500 freq between [0 pi]
Hr = s in (w* i ) * c%
' ; amplitude response
cmax = max(c)+1;
cmin = min(c) - 1;
subplot (221) , stem(n,h) , axis ( [ - 1 2*L+1 cmin cmax]) ;
xlabel '(n'), ylabel('h[n]'), title('Impulse Response');
subplot(222), plot(w/pi, Hr), grid on;
xlabel('freq in pi units'), ylabel('Hr'),
title('Type-3 Amplitude Response');
subplot(223), stem(0:L, c), axis([-1 2*L+1 cmin cmax]);
xlabel('n'), ylabel('c[n]'), title('c[n] coeffs');
subplot(224), zplane(h,1), title('Pole-zero plot');
Output:
Enter the values of h
[-4, 1, -1 -2, 5, 6, -5, 2, 1, -1, 4]
Impulse Response
10
20
Hr
h[n]
5
0
-5
0
5
n
c[n] coeffs
-10
10
Imaginary Part
10
c[n]
10
5
0
-5
0
5
n
10
0.5
freq in pi units
Pole-zero plot
1
0.5
10
0
-0.5
-1
-1
0
Real Part
iii) Program:
Impulse Response
10
20
Hr
h[n]
5
0
10
0
-5
0
-10
10
n
d[n] coeffs
Imaginary Part
10
d[n]
5
0
-5
0
10
0.5
freq in pi units
Pole-zero plot
1
0.5
11
0
-0.5
-1
-1
0
Real Part
2. a) Output:
FIR filter using Hamming window
20
0
-20
Gain in dB
-40
-60
-80
-100
-120
-140
0.1
0.2
0.3
0.4
0.5
0.6
Normalized Frequency
0.7
0.8
0.9
b) Output:
FIR filter using Kaiser window
20
0
-20
Gain in dB
-40
-60
-80
-100
-120
-140
0.1
0.2
0.3
0.4
0.5
0.6
Normalised Frequency
0.7
0.8
0.9
3. Program:
clc , clear
al l,close all;
fs = 2000;
wpl = 400*2/fs;
wph = 500*2/fs;
wsl = 300*2/fs;
wsh = 600*2/fs;
delta_f = wsh-wph;
%a)Hamming Window
N = ceil(6.6/delta_f); % est imates order of the f i l t e r
b = fir1(N,[wpl wph]);
[H,W] = freqz(b,1,512,fs);
subplot(211), plot(W,20*log10(abs(H))), grid on;
title('a) FIR bandpass filter using Hamming window');
xlabel('Frequency in Hz'), ylabel('Gain in dB');
% th i s f i l t e r provides about 53dB attenuat ion in stop band
% and pass band r ipp les are less than 0.25dB as expected.
%b)Kaiser Window
Rp = 0.25;
As = 40;
beta = 0.4852*(As- 21)^0.4+0.07886*(As- 21);
% estimates
BETA
value for the window
N = cei l ( ( A s - 7.95) / ( 14 .36*de l ta_
% estimates
f));
order of the
filter
b = f i r 1 (N , [wp l wph],ka i se r (N+1,beta) ) ;
[H,W] = f reqz(b ,1 ,512, f s ) ;
subplot (212) ,p l o t (W,20* log10(abs(H) ) ) ,on;gr id
title('b) FIR filter using Kaiser window');
xlabel('Frequency in Hz'), ylabel('Gain in dB');
a) FIR bandpass filter using Hamming window
Gain in dB
-50
-100
100
200
300
400
500
600
700
Frequency in Hz
b) FIR filter using Kaiser window
800
900
1000
100
200
300
800
900
1000
Gain in dB
50
0
-50
-100
400
500
600
Frequency in Hz
700
4. Output:
FIR LPF using frequency sampling design
50
0
-50
Gain in dB
-100
-150
-200
-250
-300
-350
0.1
0.2
0.3
0.4
0.5
0.6
Normalised Frequency
0.7
0.8
0.9
5. Program:
clc , clear
al l,close all;
M = 25;
alpha = (M-1)/2;
Hrs = [1,1,1,1,zeros(1,18),1,1,1];
k1 = 0:floor((M-1)/2);
k2 = floor((M-1)/2)+1:M-1;
angH = [-alpha*(2*pi)*k1/M, alpha*(2*pi)*(M-k2)/M];
H = Hrs.*exp(j*angH);
h = real(ifft(H,M));
[H,W]=freqz([h],1,1024);
plot(W/pi,20*log10(abs(H))), zoom on, grid on;
title('FIR LPF using frequency sampling design');
xlabel('Normalised Frequency'), ylabel('Gain in dB');
Outout:
FIR LPF using frequency sampling design
10
0
-10
Gain in dB
-20
-30
-40
-50
-60
-70
-80
-90
0.1
0.2
0.3
0.4
0.5
0.6
Normalised Frequency
0.7
0.8
0.9
Comment:When the length of the filter is increase, we get reduction in transition width
but ripples are also increased in the PB in the process. We do not abserve much change in
stopband attenuation.
6. Program:
clc , clear
al l,close all;
M = 20;
alpha = (M-1)/2;
Hrs = [ 1, 1, 1, 0.5, 0.1, zeros(1,11), 0.1,0.5,1,1];
k1 = 0:floor((M-1)/2);
k2 = floor((M-1)/2)+1:M-1;
angH = [-alpha*(2*pi)*k1/M, alpha*(2*pi)*(M-k2)/M];
H = Hrs.*exp(j*angH);
h = real(ifft(H,M));
[H,W]=freqz([h],1,1024);
plot(W/pi,20*log10(abs(H))), zoom on, grid on;
title('FIR LPF using frequency sampling design');
xlabel('Normalised Frequency'), ylabel('Gain in dB');
% Note that stop band attenuat ion i s not very good, i s about
15dB only
Output:
FIR LPF using frequency sampling design
10
0
-10
Gain in dB
-20
-30
-40
-50
-60
-70
-80
-90
0.1
0.2
0.3
0.4
0.5
0.6
Normalised Frequency
0.7
0.8
0.9
7. Output:
FIR LPF using optimum designing
20
Gain in dB
-20
-40
-60
-80
-100
-120
0.1
0.2
0.3
0.4
0.5
0.6
Normalized Frequency
0.7
0.8
0.9
8. Program:
clc , clear
al l,close all;
wp1 = 0.4*pi;
wp2 = 0.6*pi;
Rp = 0.25;
As = 50;
delta_p = (10^(Rp/20)-1)/(10^(Rp/20)+1);
delta_s = (1+delta_p)*(10^(-As/20));
delta_f = 0.2/2; % assuming t rans i t i on width of 0.2p i
b1 = 0.01201;
b2 = 0.09664;
b3 = -0.51325;
b4 = 0.00203;
b5 = -0.5705;
b6 = -0.44314;
Gain in dB
-20
-40
-60
-80
-100
-120
0.1
0.2
0.3
0.4
0.5
0.6
Normalized Frequency
0.7
0.8
0.9
9. In place of remez(), use firpm(). It gives the same output. Actually, this question is re-
dundant. remez is a function of the older versions. In the version that we use of Matlab,
firpm does the same work. You can ask the students to just change the function & observe the output. No need of copying it in the observation copy.
10. Program:
clc , clear
al l,close all;
Fs = 2000;
wp1 = 400*2/Fs;
wp2 = 500*2/Fs;
ws1 = 300*2/Fs;
ws2 = 600*2/Fs;
Rp = 0.25;
As = 50;
%a) Frequency Sampling Design
M = 20;
alpha = (M-1)/2;
Hrs = [zeros(1,4),1,1,zeros(1,14)];
k1 = 0: floor((M-1)/2);
k2 = floor((M-1)/2)+1:M-1;
angH = [-alpha*(2*pi)*k1/M, alpha*(2*pi)*(M-k2)/M];
H = Hrs.*exp(j*angH);
h = real(ifft(H,M));
[H,W]=freqz([h],1,1024);
subplot(211),
plot(W*Fs/(2*pi),20*log10(abs(H))), axis([0 1000 -100 50]),
grid on;
title('a)FIR BPF using frequency sampling design');
xlabel('Frequency in Hz'), ylabel('Gain in dB');
%b) Optimum Design
delta_p = (10^(Rp/20)-1)/(10^(Rp/20)+1);
delta_s = (1+delta_p)*(10^(-As/20));
delta_f = (ws2-wp2)/2;
b1 = 0.01201;
b2 = 0.09664;
b3 = -0.51325;
b4 = 0.00203;
b5 = -0.5705;
b6 = -0.44314;
C = (b1*(log10(delta_p))^2+b2*log10(delta_p)
+b3)*log10(delta_s) +
b4*((log10(delta_p))^2)+b5*log10(delta_p)+b6;
Gain in dB
50
0
-50
-100
100
200
100
200
300
400
500
600
700
Frequency in Hz
b)FIR BPF using optimum designing
800
900
1000
800
900
1000
Gain in dB
50
0
-50
-100
300
400
500
600
Frequency in Hz
700