Lab Handout 09: FIR Filter Design Using MATLAB
Lab Handout 09: FIR Filter Design Using MATLAB
Lab Handout 09: FIR Filter Design Using MATLAB
p
M-1
jw -jw
n=0
H e = h n e , - w
Type-1 Linear-Phase FIR Filters
!"##etri$al i#%ulse res%onse and M odd& In this case
4
. ( ) M - 1 /2
is an inte/er.
and ( ) ( ) h n = h M - 1- n
. 0 n M - 1. 2hen -e can sho- that
( ) ( )
( )
( )
5 2
cos
1
1
]
M-1
-jw M-1 /2 jw
n=0
H e = a n wn e
-here se)uence ( ) a n
is otained from ( ) h n
as
( )
( )
_
,
_
,
M - 1
a 0 = h : the i!!le saple
2
M - 1 M - "
a n = 2h - n , 1 n
2 2
'ince then. -e ha3e
( ) ( )
( ) 5 2
cos
M-1
r
n=0
H w = a n wn
-here ( )
r
H w
is an aplit#!e resp$nse function and not a ma/nitude res(onse function.
function [Hr,w,a,L]=Hr_Type1(h);
% Computes mp!itu"e response Hr(w)of a Type#1 L$ %&' fi!ter
% ###########################################################
% [Hr,w,a,L]=Hr_Type1(h)
% Hr = mp!itu"e 'esponse
% w = ()) fre*uencies +etween [),pi] o,er which Hr is compute"
% a = Type#1 L$ fi!ter coefficients
% L = -r"er of Hr
% h = Type#1 L$ fi!ter impu!se response
%
. = !en/th(h);
L= (.#1)01;
a = [h(L21) 13h(L4#141)]; % 15(L21) row ,ector
n = [)414L] % (L21)51 co!umn ,ector
w = [)414())]63pi0());
Hr = cos(w3n)3a6;
Type-2 Linear-Phase FIR Filters
!"##etri$al i#%ulse res%onse and M e'en& In this case
4
. ( ) M - 1 /2
is not an
inte/er. and ( ) ( ) h n = h M - 1- n
. 0 n M - 1. 2hen -e can sho- that
( ) ( )
( )
( )
5 2
cos
1
_
' ; 1
,
]
M
-jw M-1 /2 jw
n=1
1
H e = % n w n - e
2
-here
( )
_
,
M M
% n = 2h - n , n=1,2,&&&,
2 2
6ence.
( ) ( )
( ) 5 2
cos
_
' ;
,
M
r
n=1
1
H w = % n w n -
2
-here ( )
r
H w
is an aplit#!e resp$nse function. 7ote that
w='
-e /et
( ) ( )
( ) 5 2
cos 4
_
' ;
,
M
r
n=1
1
H w = % n n-
2
re/ardless of ( ) % n
or ( ) h n
. 6ence -e cannot use this t*(e 0i.e. s*mmetrical ( ) h n
and M
e3en.1 for hi/h(ass or andsto( filters.
Type-" Linear-Phase FIR Filters
Antis"##etri$ i#%ulse res%onse and M odd& In this case ( =' 2 . ( ) M - 1 /2
is an
inte/er. and ( ) ( ) h n =-h M - 1- n
. 0 n M - 1. and ( ) h M - 1 2 =0
. 2hen -e can sho- that
( ) ( )
( )
( )
5 2
sin
1
1
]
1
1
]
M-1
-j w M-1 /2
jw 2
n=1
H e = ) n wn e
-here
( )
_
,
M - 1 M - 1
) n = 2h - n , n=1,2,&&&,
2 2
6ence.
( ) ( )
( ) 5 2
sin
M-1
r
n=1
H w = ) n wn
7ote that at w=0 and
w='
-e ha3e ( ) 4
r
H w
. re/ardless of ( ) ) n
or ( ) h n
.
+urthermore.
j' 2
e = j . -hich means that ( )
r
jH w
is (urel* ima/inar*. 6ence this t*(e of
filter is not suitale for desi/nin/ a lo-(ass or a hi/h(ass filter. 6o-e3er. this eha3ior is
suitale for a((roximatin/ ideal 6ilert transformers and differentiators.
function [Hr,w,c,L]=Hr_Type7(h);
% Computes mp!itu"e response Hr(w) of a Type#7 L$ %&' fi!ter
% ###########################################################
% [Hr,w,c,L]=Hr_Type7(h)
% Hr = mp!itu"e 'esponse
% w = fre*uencies +etween [),pi] o,er which Hr is compute"
% + = Type#7 L$ fi!ter coefficients
% L = -r"er of Hr
% h = Type#7 L$ fi!ter impu!se response
%
. = !en/th(h);
L = (.#1)01;
c = [13h(L214#141)];
n = [)414L];
w = [)414())]63pi0());
Hr = sin(w3n)3c6;
Type-* Linear-Phase FIR Filters
Antis"##etri$ i#%ulse res%onse and M e'en& 2his case is similar to 2*(e-2. 8e ha3e
( ) ( )
( )
( )
5 2
sin
1
1
]
1
_
' ; 1
,
]
M
j w M-1 /2
jw 2
n=1
1
H e = ! n w n - e
2
-here
( )
_
,
M M
! n = 2h - n , n=1,2,&&&,
2 2
and
( ) ( )
( ) 5 2
sin
_
' ;
,
M
r
n=1
1
H w = ! n w n -
2
7ote that at w=0 and
w='
-e ha3e ( ) 4
r
H 0
and
j' 2
e = j . 6ence. this t*(e is also
suitale for desi/nin/ di/ital 6ilert transformers and differentiators.
Assign#ents & 2
&. !A2LA" functions for 2*(e-& and 2*(e-9 is /i3en. 8rite a !A2LA" function to
com(ute the am(litude res(onse ( )
r
H w
and the location of the %eros of ( ) H z
for 2*(e-2
and 2*(e-:.
2. Let
( )
( )
( )
( )
; : & & 2 < = < 2 & & :>
; : & & 2 < = = < 2 & & :>
; : & & 2 < 4 < 2 & & :>
; : & & 2 < = = < 2 & & :>
a+ h n
%+ h n
)+ h n
!+ h n
Determine the 2*(e of the Linear-Phase +I, filters. +ind the am(litude res(onse ( )
r
H w
and
locations of the %eros of ( ) H z
. Also (lot the im(ulse res(onse. am(litude res(onse.
coefficients. and the %ero locations.
(IND)( DE!I*N TE+HNI,UE!
'ummar* of commonl* used -indo- function characteristics.
8indo-
7ame
2ransition
A((roximate
8idth ,w Exact
?alues
!in. 'to(and
Attenuation
,ectan/ular
*'
M
1&-'
M
2& d"
"artlett
-'
M
.&1'
M
2< d"
6annin/
-'
M
.&2'
M
:: d"
6ammin/
-'
M
.&.'
M
<9 d"
"lac@man
12'
M
11'
M
7: d"
!A2LA" (ro3ides se3eral routines to im(lement -indo- functions discussed ao3e tale.
- A oxcar0n1 returns the n-(oint rectan/ular -indo- in arra* -.
- A trian/0n1 returns the n-(oint "artlett 02rian/ular1 -indo- in arra* -.
- A hannin/0n1 returns the n-(oint s*mmetric 6annin/ -indo- in a column
3ector in arra* - .
- A hammin/0n1 returns the n-(oint s*mmetric 6ammin/ -indo- in a column
3ector in arra* -.
- A lac@man0n1 returns the n-(oint s*mmetric "lac@man -indo- in a column
3ector in arra* -.
- A @aiser0n.eta1 returns the eta-3alued n-(oint Baiser -indo- in arra* -.
Csin/ these routines. -e can use !A2LA" to desi/n +I, filters ased on the -indo-
techni)ue. -hich also re)uires an ideal lo-(ass im(ulse res(onse ( )
!
h n
as sho-n elo-.
function h"=i"ea!_!p(wc,.);
% &"ea! Low$ass fi!ter computation
% ################################
% [h"] = i"ea!_!p(wc,.);
% h" = i"ea! impu!se response +etween ) to .#1
% wc = cutoff fre*uency in ra"ians
% . = !en/th of the i"ea! fi!ter
%
a!pha = (.#1)01;
n = [)414(.#1)];
m = n # a!pha 2eps; % a"" sma!!est num+er to a,oi "i,i"e" +y 8ero
h" = sin(wc3m)90(pi3m);
2o dis(la* the fre)uenc* domain (lots of di/ital filters. !A2LA" (ro3ides the -re./ routine.
Csin/ this routin/. -e can de3elo(ed a modified 3ersion. called fre)%Dm. -hich returns the
ma/nitude res(onse in asolute as -ell as d" scale. the (hase res(onse. and the /rou( dela*
res(onse as sho-n elo-.
function ["+,ma/,pha,/r",w] = fre*8_m(+,a)
% .o"ifie" ,ersion of fre*8 su+routine
% ####################################
% ["+,ma/,pha,/r",w] = fre*8_m(+,a)
% "+ = re!ati,e ma/nitu"e in ": compute" o,er ) to pi ra"ians
% ma/ = a+so!ute ma/nitu"e compute" o,er ) to pi ra"ians
% pha = $hase response in ra"ians o,er ) to pi ra"ians
% /r" = ;roup "e!ay o,er ) to pi ra"ians
% w = ()1 fre*uency samp!es +etween ) to pi ra"ians
% + = numerator po!ynomia! of H(8) (for %&'4 +=h)
% a = "enominator po!ynomia! of H(8) (for %&'4 a=[1])
%
[H,w] = fre*8(+,a,1))),6who!e6) ;
H = (H(1414()1))6; w = (w(1414()1))6;
ma/ = a+s(H);
"+ = 1)3!o/1)((ma/2eps)0ma5(ma/));
pha = an/!e(H);
/r" = /rp"e!ay(+,a,w);
Assign#ent 0
9. Desi/n a di/ital +I, lo-(ass filter -ith the follo-in/ s(ecifications:
p p
s s
w =0&2' R =0&2/!0
w =0&"' 1 =/0!0
Ehoose an a((ro(riate -indo- function from ao3e 2ale. Determine the im(ulse res(onse
and (ro3ide a (lot of the fre)uenc* res(onse of the desi/ned filter.
23aple 1: Let us desi/n the follo-in/ di/ital and(ass filter.
1s s
1p p
2p p
2s s
l$wer st$p%an! e!4e : w =0&2', 1 =.0!0
l$wer pass%an! e!4e : w =0&"/' R =1!0
#pper pass%an! e!4e : w =0&./', R =1!0
#pper st$p%an! e!4e : w =0&-' 1 =.0!0
2hese )uantities are sho-n in the follo-in/ fi/ure.
0
0.35 0.65 0.8 0.35
60
1
1
w/pi
D
e
c
i
b
e
l
s
% <o!ution of =5amp!e 1
ws1 = )913pi; wp1 = )97(3pi;
wp1 = )9>(3pi; ws1 = )9?3pi;
s = >);
tr_wi"th = min((wp1#ws1),(ws1#wp1));
. = cei!(113pi0tr_wi"th) 21;
n = [)414.#1];
wc1 = (ws12wp1)01; wc1 = (wp12ws1)01;
h" = i"ea!_!p(wc1,.) # i"ea!_!p(wc1,.);
w_+!a = (+!ac@man(.))6;
h = h"93w_+!a;
["+,ma/,pha,/r",w] = fre*8_m(h,[1]);
"e!ta_w =13pi01)));
'p = #min("+(wp10"e!ta_w21414wp10"e!ta_w)) % ctua! $ass+an" 'ipp!e
s = #roun"(ma5("+(ws10"e!ta_w21414()1))) % .in <top+an" ttenuation
% $!ots
su+p!ot(1,1,1); stem(n,h"); tit!e(6&"ea! &mpu!se 'esponse6)
a5is([) .#1 #)9A )9(]); 5!a+e!(6n6); y!a+e!(6h"(n)6)
su+p!ot(1,1,1); stem(n,w_+!a); tit!e(6:!ac@man Bin"ow6)
a5is([) .#1 ) 191]); 5!a+e!(6n6); y!a+e!(6w(n)6)
su+p!ot(1,1,7); stem(n,h); tit!e(6ctua! &mpu!se 'esponse6)
a5is([) .#1 #)9A )9(]); 5!a+e!(6n6); y!a+e!(6h(n)6)
su+p!ot(1,1,A); p!ot(w0pi,"+); a5is([) 1 #1() 1)]);
tit!e(6.a/nitu"e 'esponse in "+6);/ri";
5!a+e!(6fre*uency in pi units6); y!a+e!(6Ceci+e!s6)
0 20 40 60
-0.4
-0.2
0
0.2
0.4
Ideal Impulse Response
n
h
d
(
n
)
0 20 40 60
0
0.2
0.4
0.6
0.8
1
Blackman Window
n
w
(
n
)
0 20 40 60
-0.4
-0.2
0
0.2
0.4
Actual Impulse Response
n
h
(
n
)
0 0.5 1
-150
-100
-50
0
Magnitude Response in db
frequency in pi units
D
e
c
i
b
e
l
s
Assign#ent 1
:. 2he fre)uenc* res(onse of an ideal andsto( filter is /i3en *
' ;
p
p
e
1, 0 w' "
H = 0,' " w 2' "
1, 2' " w '
usin/ a Baiser -indo-. desi/n a andsto( filter of len/th :< -ith sto(and attenuation of =4
d".
23aple 2: 2he fre)uenc* res(onse of an ideal di/ital differentiator is /i3en *
( )
' ;
p
p p
jw
!
jw, 0 w
H e =
-jw, - w 0
usin/ a 6ammin/ -indo- of len/th 2&. desi/n a di/ital +I, differentiator. Plot the time- and
the fre)uenc*-domain res(onses.
'oln: 7ote that M is an e3en numer. then ( ) M - 1 /2
is an inte/er and ( )
!
h n
-ill e
%ero for all n. hence M must e an odd numer. and this -ill e a 2*(e-9 linear (hase +I,
filter.
% <o!ution of =5amp!e 1
. = 11; a!pha = (.#1)01;
n = )414.#1;
h" = (cos(pi3(n#a!pha)))90(n#a!pha); h"(a!pha21) = );
w_ham = (hammin/(.))6;
h = h"93w_ham;
[Hr,w,$,L]=Hr_Type7(h);
% $!ots
% su+p!ots(1,1,1);
su+p!ot(1,1,1); stem(n,h"); tit!e(6&"ea! &mpu!se 'esponse6)
a5is([#1 . #191 191]); 5!a+e!(6n6); y!a+e!(6h"(n)6)
su+p!ot(1,1,1); stem(n,w_ham); tit!e(6Hammin/ Bin"ow6)
a5is([#1 . ) 191]); 5!a+e!(6n6); y!a+e!(6w(n)6)
su+p!ot(1,1,7); stem(n,h); tit!e(6ctua! &mpu!se 'esponse6)
a5is([#1 . #191 191]); 5!a+e!(6n6); y!a+e!(6h(n)6)
su+p!ot(1,1,A); p!ot(w0pi,Hr0pi); tit!e(6mp!itu"e 'esponse6);/ri";
5!a+e!(6fre*uency in pi units6); y!a+e!(6s!ope in pi units6);
a5is([) 1 ) 1]);
0 5 10 15 20
-1
-0.5
0
0.5
1
Ideal Impulse Response
n
h
d
(
n
)
0 5 10 15 20
0
0.2
0.4
0.6
0.8
1
Hamming Window
n
w
(
n
)
0 5 10 15 20
-1
-0.5
0
0.5
1
Actual Impulse Response
n
h
(
n
)
0 0.5 1
0
0.2
0.4
0.6
0.8
1
Amplitude Response
frequency in pi units
s
l
o
p
e
i
n
p
i
u
n
i
t
s