Professional Documents
Culture Documents
Design of IIR and FIR
Design of IIR and FIR
Design of IIR and FIR
-5
-1 0
''
-30
-35
= ;_r/~ Mz
• F>a~">r°1f,rt"j f·r':'"..; ,,~'".. /
-40 = "':~,£ ttz
• ,..Jtr4:,r,;:ar':l F'&:''J.i,#;f'.(,,/
• Pa'/,r,.;r,1 rA r • -11;,je
-4 5 100
200-
L--__._---;; 300 400 500 • ':;t0vr,ar r:l 1-.r.,,, .~ -:.' ., .2-:.,,g
o frequency (Hz)
Pseudoeooe:
High Pass FIiter
--· .,-
• I
0
' .:'-· 11.z • USJ:: t.i,;~r~t 1 ',,,,..r;. "..,( •:. ,x·4'.:"'
, ,. ~-_,, (i;v h 1 ~ J.E)I • Use 0 1..111.er' J ,,......r:.• r.,r t,:. "X;d~ t"'
transf,;:r i,,r-,:;i ,:;r.
-100 ( ' _ ')od..6 I • Jse freqs(, '~r.r.·•-:.- •-:; ':.l:.1a - •
obta1l'le,j trar,;•~r f~r:~ er a"~
-200
MATLAB Code:
fiJ
:2,
;::: -300 ( Wp = 200•2•pi,
;;:
;f Ws = 300•2~pi;
( fm = s00•2•;:,1;
-400
Ap = 5;
r As= 20;
-500 [N,Wc) = cut:ord (Wp,ws,Ap,.e.s,
(b,a] = butter(H,Wc, s ,,
W = 0: 0 . 01 • f111;
-600
0 100 H = freqs(b,a,W );
200 300 400 500 plot(W./(2"pi),20•log10(a!ls ri
frequency (Hz)
title( 'Low Pas.s ~i:·e· !,
xlabe 1 ( 'frequercy "Z ) '
ylabel('IH (w} {d 9 ),
DESIGN OF IIR FILTERS
AIM:-To design analog and digital IIR Filters using MATLAB
SOFTWARE REQUIRED:-MATLAB R2021 B
1. Design an analog low pass filter with the following spec1ficat1on, given that we get a
monotonic frequency response
Pseudocode:
• Use buttord() function to obtain the order and cutoff frequency of the Butterworth Filter
• Use butter() function to obtain the numerator and denominator coefficients of the filter
transfer function
• use freqs() function to obtain the values of H(w) for a set range of frequencies w, and plot the
obtained transfer function against w using the plot() command
MATLAB Code:
Wp = 200*2*pi;
Ws = 300*2*pi;
fm = 500*2*pi;
Ap = 5;
As= 20;
[N,Wc] = buttord(Wp,Ws,Ap,As, 's');
[b,a] = butter(N,Wc, 's');
W= 0:0.01:fm;
H = freqs(b,a,W);
plot(W./(2*pi),20*log10(abs(H)))
title('Low Pass Filter');
xlabel('frequency (Hz)');
ylabel('IH(w)I (dB)');
.. (ll T
'""'' ,.,.
l
I llfft l " " ' " " •
,, I"
l
,, 2 Oes,gn an ana og
monotonic freciuency res~
IS
• Passband Freque
,'{ ' "' • Stopband FreQJe
i;'
ii.. ... ~,, • Passband Gar. =
---~-t Pseudocode·
"-'
.....~~
-L---- - .,
I-
2 25 3 35 • Use buttord f,.,
...~x' I,,. I 5
,1 ~' .!"- fl't'~ll611C V (HZ) • Use butter() fu
transfer fu"'.Ct-oo
• use freqs{J fa
BSF Olgltal Flltwr
obta ned trans _
~
~ --,-
'"
;:, ~
'
'~- \ w - _, MATLAB Code:
.. , 1,., .. \_
..,, \ T 1~,1 j) 1
Wp = 300•2•pi ;
"'"'; -~C" ,... '
l
~
\ I
~
• I Ws = 200•2•pi;
fm = s00•2•p1;
~!' -"' ► I/ ) ) I
c;
I Ap = 5;
-40 ~ As= 20;
-5-0 .
-60
0
...
'...JJ
200
1
:I' \ \) 400
t
...
h"equency
- 600
,L
800
1
___ _ j
1000
[N,Wc] = butte.rd (
[b,a] = butter(N,
w= 0:0.01 :fm;
H = freqs (b, a,W);
plot(W. /( 2•pi ), 2a•
t itle( High Pass F
20 Digital LPF usln xlabel( ' freq ..
ylabel( IHw)
0 I-
\ ';J l
t
-20
i""' t)
-40 ► ' -i 3 Des~ a.'l
monoton.ic f!eq
-60 i j • Passband F
• S~F
~~
' l
~
• Passba"ld ~
• Stopba!ld
-•oot
·120
·l◄OL....,
I
'
j PMudococM
• 0bta! ete~
' .....
I
0 500 100Q hquenoei
150()
200o 2500 3000 3500
2. Design an analog high pass filter with th .
mono ton ic frequency response e following specification, given that we get a
Pseudocode:
• Use buttord() function to obtain the order and cutoff freq uency of the Butterworth Filter
• Use butter() function to obtain the numerator and denominator coefficients of the filter
transfer function
• use freqs() function to obtain the values of H(w) for a set range of frequencies w, and plot the
obtained transfer function against w using the plot() com mand
MATLAB Code:
Wp = 300*2*pi;
Ws = 200*2*pi;
fm = 500*2*pi;
Ap = 5;
As = 20;
[N,Wc] = buttord(Wp, Ws, Ap , As, 's' );
[b,a] = butter( N, Wc , 'high' , , s , ) ;
W = 0:0.01:fm;
H = freqs(b , a,W);
plot(W./(2* pi ) ,20* log10(abs ( H)) )
title('High Pass Filter') ;
xlabel('frequency (Hz)' );
ylabel('IH(w) I (dB) ');
Pseudocode: . .
ssband and stop ba nd analog frequencies from the d1g1tal
• Obtain the prewarped pa
frequencies provided
• Lise bLttord\) function w ith the analog frequencies to obtain the order ;ind cutoff trequen '/ o'
H:e Butterworth Filter
, Lse butter() function to obtain the numerator and denominator coeff1c1ents of the ;ina cg f •er
transfer function
• use bilinear() function to obtain the numerator and denominator coefficients of the d g ta
filter transfer function
, use freqz() function to obtain the values of H(w) for a set range of frequencies w and pot th"'
obtained transfer function against w using the plot() command
MATLAB Code:
l~P == 0.6*pi;
ws:: 0.2*pi;
Ap == 20*log10(0.8);
AS:: 20~logl0(0.2);
fm == pi;
wp:: 2xtan(Wp/2);
ws == 2xtan(Ws/2);
w == 0:0.01:fm;
[N,Wc] == buttord(wp,ws,Ap,As, 's');
[b,a] = butter(N,Wc,'high','s');
[bz,az] = bilinear(b,a,1);
H == freqz(bz,az,W);
Plot(W,20*log10(abs(H))) .
title( 'Hi gh Pass Filter using BLT' ) ;
xlabel( ' frequency (Hz)');
ylabel( 'IH(w) I (dB)');
dF uency = 100 Hz
• Lower Passban req - 500 Hz
Frequency -
• Upper Pass b an d _ 00 Hz
d Frequency - 2
• Lower Stopban - 250 Hz
• Upper Stopband F requency -
5dB_ 20dB
• Passband Gain = -_
• Stopband Attenuation - -
MATLAB Code:
a_p=S;
a_s=20;
Fs=2000;
w_pl=2*pi*100/Fs;
w_p2=2*pi*500/Fs;
w_s1=2*pi*200/Fs;
w_s2=2*pi*250/Fs;
W_pl=2*Fs*tan(w_pl/2);
W_p2=2*Fs*tan(w_p2/2);
w_s1=2*Fs*tan(w_s1/2);
w_s2=2*Fs*tan(w_s2/2);
(N,Wc]=cheblord([W_pl,W_p2],[W_s1,W_s2],a_p,a_s, 's');
[b,a]=chebyl(N,a_p,Wc, 'stop', 's');
w=linspace(0,pi,1000);
(zd,pd]=bilinear(b,a,Fs);
H=freqz(zd,pd,w);
plot(w*Fs/(2*pi),20*log10(abs(H)));
xlabel('frequency')
ylabel('magnitude in dB')
title('BSF Digital Filter')
5. Design an digital low pass filter using the Impulse Invariance technique, given that we get a
monotonic frequency response
Pseudocode:
. obtain the order and cutoff frequency of the ~utterworth Filter
• Use buttord() function to . th numerator and denominator coefficients of the analog filter
• Use butter() function to obtain e
transfer function
• Use impinvar() function to obtai th
.
filt er t rans fer f unction n e numerator and d •
enominator coefficients of the digital
• use freqz() function to obtain the v I f
. a ues o H(w) for a t
obtained transfer function against w . se range of frequencies w and plot the
using the plot() command
MATLAB Code:
fp = 200*2*pij
fs = 300*2*pi;
Fs = 1000*2*pi;
Ap = Sj
As = 20;
[N,Wc] = buttord(fp,fs,Ap,As, 's');
[b,a] = butter(N,Wc, 's');
[bz,az] = impinvar(b,a,Fs);
[H,W] = freqz(bz,az);
plot(W*Fs/(2*pi), 20 * log10(abs(H)));
title('Digital LPF using impulse invariance');
RESULT:- Thus, the analog and digital IIR filters are designed for
the requi red specifications
fP✓
~ii"
-,j\
1.2
1 ' --
OB
Ios
!.
04
02
,'""\ /'·, /
' ,..
o L----'--
0 100 200 300 400 500
Frequency (Hl)
Low Pass FIR filter using Blackman window (M=21)
1.2
------ ·,
'\
\
OM \
I 0.6 ~ \
-
0,4f-
\ \
\
l
0.2 1
OL
\
\
0 100 '-,
300
400
Frequency (Hz) 500
DESIGN OF- FIR f II.Ti I\:;
dcsign
r,,
, 1·s1,1ckrt1a
Ali• ,, FIR filters with Rectangular, llan1111111, iln1rotr111111
11 windows using MATLAB
~11d
~off ,111ARE
vw
REQUIRED:- MATLAB R2021B
Qe$ign a Low Pass FIR filter using rectangular window
1
, order "" 21
• cutoff frequency = 250 Hz;
• sampling frequency = 1000 Hz
p5 eudocode:
1 use fir 1() function to obtain the filter coefficients from the 01der ari1l rnt,,ff f1e,1urlfh' \ ll liil li.J
~·~ n~ular " indow (rectwin() function)
3
: . use freqz() function to obtain the filter transfer function H(w) from tt,a r1ltcr ,,iom; rr,11112
3 Use plot() command to plot the frequency response of the frlter
MATLAB Code:
fr = 250;
=s = 1000;
II : 2l j
~ = firl (M,fc*2/Fs, ··1ow' ,rectwin(M+l));
[Y,w] = freqz(b);
plot (w"' Fs / (2*pi), abs (H))
i:itle( ' Low Pass FIR filter using rectangular 1-iindo1v ~M-}L)')
xlabel( ' Freq uency (Hz)')
ylabel( · IH(w) I , )
M==11
2. Design a Lou ::i-
1.2
/,,,.------ • order = 21
1/- • cutoff frequency
// • sampling frequSl
ae-
/
i os-
// Pseudocode:
!. --/
/1
I '
_\'\' "\\J
____
\ \_
t1tll1 \llllll for
-IOI.) lllli:l
t- I t'requ~l)C\ (H;·)
end
h • hd. •whn;
Bond Pa?.s FIR tltte~uslng Bh1ck nmn wl11dov.; (M•21)_ -~
1
1
[H,W] = freq
~.il ..
plot(W•Fs/ (2
title(' High
\')~ .
xlabel( 'Freq
1..' 7,.. ylabel(' IH(w
i\cH
~ Cl:-~I
!_
• • l \
D.4"
0 ..i ...
• order= 5
Ol· • cutoff fr,
Ot- • sampling
1
() '-- ~ - .._ ..
i) :x>CI ", -100 600
P•eudocod
800 1000
Frequency (HZ)
1. Use the
2.Usethe
,,..__ 3. Multiply
"~() to Obtain '
4. Use plo
MATLAB Code:
fc = 2s0;
FS = 1000;
M == 11;
hd == [];
for k--0·1:M-1
·
if(k==( M-1)/2)
hd = cat(2,hd,(l-2*(fc/Fs)));
else
hd = cat(2 ,hd,-(2*( fc/Fs) *s in((k-((M-1)/2))*2*pi *( fc/Fs)))/(( k-(M-
l)/Z)*Z*pi * (fc/Fs)));
end
end
whn = [];
for 1=0:l:M- 1
whn = cat(2,whn,(0.5-0.5*cos(2*pi*l/(M-1))));
end
h = hd.*whn;
[H,W] = freqz(h, 1);
plot(W*Fs/(2*pi),abs(H));
title( 'High Pass FIR filter using Hanning window (M=ll)')
xlabel('Frequency (Hz)')
ylabel(' I H(w) I ')
4· Design a High Pass FIR Filter using Hanning Window
• order= 51
• cutoff frequency = 250 Hz
• sampling frequency = 1000 Hz
Pseudocode:
- Use the formula for the high pass filter to generate the causal sequence hd[n]
1
· Use the formula tor the causal Hanning window to generate the windowing sequence w(n]
2
3. Multiply these 2 sequences to obtain the filter coefficients for the transfer runctlon, and use
1reqz() to obtain the frequency response H(w)
4. Use plot() command to plot the frequency response of the filter
1.06 Band Stop FIR fllte r using Han~lng window 1(M=11,
1 1
,,------ --- - _=:J
1.04 - /
I
/
1.02 -
1~
I
0.98 r
Io.96-
~
0.94
0.92
o.9 r
>-
L
\
I
I
I
0.88 L
0.86 \ __ j
0 200 400 600 800 1000
Frequency (H z)
1.1 ~__B_a_n_d~St_o__,_p_F_l_R_fl_lte~r_u_s_ln___,9,.__H----'a_nn~l_n_,,g----'wl_n_d_o_w--1(~M_=_7__,_1)
1~ ,
0.9
Ir
0.8 [
= 0.7
3
~ 0.6 f-
0.5 r-- t ~
1
0 .4 -
o.3 r
~
o 200 400 600
J
800 1000
Frequency (Hz)
MATLAB Code:
fc = 250;
Fs = 1000;
M = 51;
hd = [ ] ;
for k=0:l:M-1
if(k==(M- 1)/2)
hd = cat(2,hd,(1-2*(fc/Fs))) ;
else
hd = cat(2,hd,-(2*(fc/Fs)*sin((k- ( (M- 1)/2 ) )* 2*pi *( f c/F s)) )/ ((k - (M-
l)/2)•2* pi *(fc/Fs)));
end
end
whn = [];
for 1=0:l:M-1
whn = cat(2, whn , (0 . 5-0.5 *cos(2 *pi*l/ (M-1))));
end
h = hd.*whn;
[H,W] = fre qz( h,1 ) ;
plot(W*Fs/( 2*pi),abs(H));
title('High Pass FIR filter using Hanning window (M=51)' )
xlabel('Frequency (Hz)' )
ylabel(' I H(w) I' )
5 Design a Band Pass FIR Filter using Hamming Window
• order=101
• lower cutoff frequency = 200 Hz
• upper cutoff frequency = 250 Hz
• sampling frequency= 2000 Hz
Pseudocode:
1. Use fir1 () function to obtain the filter coefficients from the order and cutoff frequency using
Hamming window (hamming() function)
2. Use freqz() function to obtain the filter transfer function H(w) from the filter coefficients
3. Use plot() command to plot the frequency response of the filter
MATLAB Code:
fcl = 200;
fc2 = 250;
FS :: 2000;
M = 101;
b = fir1(M,[fc 1*2/ Fs , f c2*2/Fs ), 'bandpass' , hamming(M+l ));
[H w] = freqz(b);
l~t(w*Fs/(2*pi), abs (H))
P_tle('Band Pass FIR filter using Hamming window (M=l01)' )
~~abel('Frequency (Hz)' )
ylabel(' IH(w) I' )
6. Band Pass FIR Filter using Blackman Window
• order= 101
• lower cutoff frequency = 200 Hz
• upper cutoff frequency = 250 Hz
• sampl ing frequency = 2000 Hz
Pseudocode:
1. Use fir1 () function to obtain the filter coefficients from the order and cutoff frequency using
Blackman window (blackman() function)
2. Use freqz() function to obtain the filter transfer function H(w) from the filter coefficients
3. Use plot() command to plot the frequency response of the filter
MATLAB Code:
fcl = 200·
fc2 = 250:
Fs = 2000:
M = 101 · ,
b = fir~(M, [fcl *2/Fs, fc2*2/Fs], 'bandpass ' ,blackman(M+l));
[H,w] = freqz(b);
P~ot(w*Fs/(2*pi),abs(H))
title('Band Pass FIR filter using Blackman window (M= 21)')
xlabel( ' Fr equency (Hz) ' )
Ylabel(, IH( w) I')
stop FIR Filter using Hanning Window
13and
7-
order== 11
,• LOwer cutoff frequency == 200 Hz
U per cutoff frequency == 2 50 Hz
: s~rnpling Frequency == 2000 Hz
pseudocode:
1 use fir1 () function to obtain the filter coefficients from the order and cutoff frequency using
. · window (hanni ng() function)
HarnmIng
2_Use freqz() function to obtain the filter transfer function H(w) from the filter coefficients
3_use plot() command to plot the frequency response of the filter
MATLAB Code:
fcl = 200;
fc2 = 250;
Fs = 2000;
M = 12;
b = firl(M, [fc1*2/Fs,fc2*2/Fs], 'stop' ,hanning(M+l));
[H,w] = freqz(b);
plot(w*Fs/(2*pi) ,abs(H))
title(' Band Stop FIR filter using Hanning window (M=ll)')
xlabel( 'Frequency (Hz)')
ylabel(' I H(w) I ')
B. Ba nd st0 P FIR Filter using Hanning Window
• Order== 71
• Lower cutoff frequency == 200 Hz
• Up per cutoff frequency == 250 Hz
• Sampling Frequency == 2000 Hz
Pseudocode:
Hamm:~ Use fir1O function to obtain the filter coefficients from the order and cutoff frequency using
g window (hanning() function)
· Use freqz() function to obtain the filter transfer function H(w) trom the filter coefficients
2
· Use plot() command to plot the frequency response of the filter
3
~
MATLAB Code:
fcl == 200;
fc2 == 250;
Fs = 2000;
M = 72;
b = firl(M,[fc 1*2/Fs ,fc2*2/Fs ], 'stop',hanning(M+l));
[H,w] = freqz(b);
plot(w*Fs/(2*pi),a bs (H))
title('Band Stop FIR filter using Hanning window (M=71)')
xlabel('Frequency (Hz)' )
ylabel(' IH(w) I' )
RESULT:- Thus, the FIR filters are designed for the required
specifications (f)
~~