LAB #8 HANDOUT - Eastern Mediterranean Universityfaraday.ee.emu.edu.tr/eeng420-lab/lab...

8
Lab As sistant: 1 Teyre Tekblyrk E EE420 Lab Ha n dout LAB #8 HANDOUT 1. LINEAR-PHASE FIR FILTER In this lab, shapes of impulse and frequency responses and locations of system function zeros of linear-phase FIR filters will be discussed. First of all, let h(n), 0 S; n S; M -I be the impulse response of length (or durati on) M. Then the system function is, M ·I M -I "' h() -(,11-1) "' 1 ( ) M -I - -J) n H() Z = n z =Z 1 n Z which has (M - 1) poles at the origin z = 0 (trivial poles) and (M - 1) zeros loc at ed anywhere in the z-plane_The frequency response function is M-I H ( jW ) '" 1 ( ) -jw -Tr --< w S; Tr \e 11 =0 2. TYPE-1 LINEAR-PHASE FIR FILTERS These type of filte rs have symmetrical impulse response and odd M. In this case /3=0, a= (M- J) 12 isaninteger,and h(n)=h (M -I-n), O-:;'n-:;'M-I. Th en wecan show that (M-/ l l2 ] H I jll') '" () - J,,\ M -1 )12 e = a n coswn e [ 11----0 where s equen ce a(n) is obtained from h(n) as ( M -1 ) a(O )= h -2- the middle sample M -1 ) a(n) = 2h -2--n . ( Since the n, we ha ve (M -1) 12 H,(w)= I a(n) cos wn where H, (w ) IS an amplitude response function and not a magnitude resp ons e function . 1

Transcript of LAB #8 HANDOUT - Eastern Mediterranean Universityfaraday.ee.emu.edu.tr/eeng420-lab/lab...

Page 1: LAB #8 HANDOUT - Eastern Mediterranean Universityfaraday.ee.emu.edu.tr/eeng420-lab/lab handouts/Lab8.pdf · 2012-12-04 · Lab Assistant Neyre Te rblYlk EE420 Lab Handout • w =

Lab Assistant: 1 Teyre Tekblyrk EEE420 Lab Handout

LAB #8 HANDOUT

1. LINEAR-PHASE FIR FILTER

In this lab, shapes of impulse and frequency responses and locations of system function zeros of linear-phase FIR filters will be discussed.

First of all, let h(n), 0 S; n S; M -I be the impulse response of length (or duration) M.

Then the system function is,

M ·I M -I"' h() -(,11-1)"' 1 ( ) M -I ­-J) nH()Z = ~ n z =Z ~ 1 n Z

which has (M -1) poles at the origin z = 0 (trivial poles) and (M -1) zeros located

anywhere in the z-p lane_The frequency response function is

M-I

H ( jW ) '" 1 ( ) -jw -Tr --< w S; Tr\e = ~ lne. 11 =0

2. TYPE-1 LINEAR-PHASE FIR FILTERS

These type of f ilters have symmetrical impulse response and odd M. In this case

/3=0, a= (M- J) 12 isaninteger,and h(n)=h(M -I-n), O-:;'n-:;'M-I. Th en wecan show that

(M-/ ll2 ]

H I jll') '" () -J,,\ M -1 )12 ~ e = ~ a n coswn e [

11----0

where sequence a(n) is obtained from h(n) as

(M-1 )a(O )= h -2- the middle sample

M -1 )a(n) = 2h -2--n .(

Since then, we have (M -1)12

H,(w)= I a(n) coswn

where H , (w) IS an amplitude response function and not a magnitude res ponse

function .

1

Page 2: LAB #8 HANDOUT - Eastern Mediterranean Universityfaraday.ee.emu.edu.tr/eeng420-lab/lab handouts/Lab8.pdf · 2012-12-04 · Lab Assistant Neyre Te rblYlk EE420 Lab Handout • w =

Lab Ll c:s istanL: Neyr-.: TekblYlk EEE420 Lab Handout

M-file:

fun c tion [Hr , w , a , L ]=Hr_Typ e l(h) ; % Comp utes Ampl itude response Hr(w) of a Type-1 LP FIR f i lter ~ c ---- - - - -- - - --- -- -------- ---- ------ - ---------- - - - --- -- - --- -­% [Hr , w,a, L] =H r _Typel (h ) % Hr = Amplitude Response % w = 500 freque n c i es be t ween [O,p i ] over which Hr is % computed % a Type- 1 LP fil t e r coeff ic i e nt s % L Order o f Hr % h Type - l LP fi.l ter i mp ul se re sponse o -f

M leng th (h) ;

L= (M- ) /2 ; a [h (L +1) 2 *h (L:-1:1)]; % 1x( L+1) row v e ct o r n = [ 0 :1:1., ] % (L+1 )x1 column v e c t or w = [0 :1 : 5 00 ] '*p i / 5 00 ; Hr = cos(w*n ) *a ' ;

3. TYPE-2 LINEAR-PHASE FIR FILTERS

These type of fi lters have symmetrical impulse response and even M. In this case /3 =0 , a =(M - i) 12 is not an integer, and h(n)= h(M - l- n), O~n ~ M -l . Then we

can show th at

where

!'vIb(n ) - 2h ( M -n) n =1,2,

2 ' 2

Hence,

(M )/2 { ( 1)1H , (w)= I b (n) cos w n - - J

II - I 2

where Hr (w) is an amplitude response function . Note that w= we get l[

(M )12 { (Hr ( w) = I b (n ) co 7l n - -l)} = 0 II _ I 2

regardless of b(ll ) or h(n) . Hence we cannot use this type (i.e. symmetrical h(l1) and M even.) for highpass or bandstop filters.

2

Page 3: LAB #8 HANDOUT - Eastern Mediterranean Universityfaraday.ee.emu.edu.tr/eeng420-lab/lab handouts/Lab8.pdf · 2012-12-04 · Lab Assistant Neyre Te rblYlk EE420 Lab Handout • w =

Lab Assistant: eyre Tekbl Ylk EEE420 Lab Handout

4. TYPE-3 LINEAR-PHASE FIR FILTERS

These type of filters have antisymmetric impulse response and odd M. In this case fJ = 7[/ 2, a = (M -1) /2 is an integer, and 11 (n) = -h (M -1 - n), 0 5, n5, Jv! -1, and

h(M -1/ 2) = O. Then we can show that

where M ~1

n=1,2, ... ,-­2

Hence, (M./)12

H, (w)= I c(n)sinwn

Note that at w=O and W=7[ we have H,(w)=O, regardless of c(n) or h(n ) . Furthermore, e Jlf/l = j, which means that jH

r (w) is purely imaginary. Hence this

type of filter is not suitable for designing a lowpass or a highpass filte r. However, this behavior is suitable for approximating ideal Hilbert transformers and diffe rentiators.

M-file:

function [Hr ,w,c,L]=Hr_Type3(h); % Computes Amplitude response Hr(w) of a Type-3 LP FI R fi l ter % ------- --- -- --- -------- -- ---------- - - -- ---------------- - --­% [Hr ,w,c, L]=Hr Typ e 3 (h) % Hr = Ampl i t ude Response % w frequencies be t ween [O,pi] over which Hr is c omputed % b Type-3 LP fi l t er coeffi c ients % L Order of Hr % h Type- 3 LP fi lter impulse response o -0

M length (h ) i

L (M- 1) / 2 ;

c [2 * h (L+ 1 : - 1 : 1) J i n [O : l :L] i

w [0 : 1 :5 00] '*pi /5 00;

Hr = s i n (w*n ) *c' i

3

Page 4: LAB #8 HANDOUT - Eastern Mediterranean Universityfaraday.ee.emu.edu.tr/eeng420-lab/lab handouts/Lab8.pdf · 2012-12-04 · Lab Assistant Neyre Te rblYlk EE420 Lab Handout • w =

Lab Ass istant: N yre TekblYlk EEE420 Lab Handout

5. TYPE-4 LINEAR-PHASE FIR FILTERS

These type of f ilters have antisymmetric impulse response and even M. This case is similar to Type-2. We have

Jw . [(ft{ )11 { ( 1)}] .{ ~- w( M -/ )I? ]H (e ) = ~ d ( 11 ) sin w n - 2 e !

where

M 11 = 1,2, ... , ­

2

and

Note that at w = 0 and w = n we have H,. (0) = 0 and ei ;-r/ 2 = j . Hence, this type is

also suitable for designing digital Hilbert transformers and differentiators .

6. WINDOW DESIGN TECHNIQUES

Summary of co mmonly used window function characteristics. Window Transition Width Liw Exact Min. Stopband

Name Approximate Values Attenuation

Rectangular 4n -M

1.8n - ­

M 21 dB

Bartlett 8n -M

6.1n - ­M

25 dB

Hanning 8n -M

6.2][ - ­M

44 dB

Hamming 8n -M

6.6][ - ­

M 53 dB

Blackman ]2][ -M

lln -M

74 dB

MATLAB provides several routines to implement window functions discussed above table.

• w =boxcar(n) returns the n-point rectangular window in array w. • w =triang(n) returns the n-point Bartlett (Triangular) window in array w. • w = hanning(n) returns the n-point symmetric Hann ing window in a

column vector in array w . • w = hamming(n) returns the n-point symmetric Ham ming window in a

column vector in array w. • w = blackman(n) returns the n-point symmetric Blackman window in a

column vector in array w.

4

Page 5: LAB #8 HANDOUT - Eastern Mediterranean Universityfaraday.ee.emu.edu.tr/eeng420-lab/lab handouts/Lab8.pdf · 2012-12-04 · Lab Assistant Neyre Te rblYlk EE420 Lab Handout • w =

Lab Assistant Neyre Te rblYlk EE420 Lab Handout

• w = kaiser(n,beta) returns the beta-valued n-point Kaiser window in array w.

Using these routines, we can use MATLAB to design FI R filters based on the window technique, which also requires an ideal lowpass impulse response hd (n) as shown

below.

func t l o n h d=id ea l I p (wc , M); % I d e al LowPass f i lt e r computation o ~ --- -- -------- - -----------------­

% [hd] == i d eal lp(wc,M); % hd = i d ea l impulse response bet ':leen 0 to M- 1 % wc = c utoff frequency ir radia~s % M == length o f the ideal filter % a lpha = (M-l) / 2; n == [O:1: (M-1)]; % add smallest number to avoid dividing by zero m = n - alph a +eps; h d = sin (wc*m) . / (pi *m) ;

To display the frequ ency domain plots of digital filters, MATLAB provides the freqz routine. Using this routine, we can develope a modified version, called freqz_m, which return s the magnitude response in absolute as well as dB scale, the phase response, and th e group delay response as shown below.

M-file:

fun ction [d b, mag,ph a,grd,w] = freqz_m (b,a) % Modified version of freqz subroutine % -------------------- - --------- --- --­% [db , mag , p h a,grd,w] = freqz_m (b,a) % db = r e l a t ive magnitude in dB computed over 0 to p i rad ians

a

% mag absol u te magnitude computed over o to pi radians % pha phase response in radians over 0 to pi radians % grd Group delay over 0 t o pi radians % W 501 frequency samples between 0 to pi radians 9­o b numerator polynomial of H (z) (for FIR: b=h) 9­a a denominator polynomi al of H (z) (for FIR: a=[l J ) 9­

[H, wJ fre q z (b , a, 1000, 'whole' ) H (H (l:l:501)) '; w = (w( 1:1:501 )) ';

mag a b s (H) ; db 2 0*loglO((mag+eps ) / ma x(mag)) ;

pha angl e (H) ; grd grpdelay(b,a,w) ;

5

Page 6: LAB #8 HANDOUT - Eastern Mediterranean Universityfaraday.ee.emu.edu.tr/eeng420-lab/lab handouts/Lab8.pdf · 2012-12-04 · Lab Assistant Neyre Te rblYlk EE420 Lab Handout • w =

Lab Assjstant: eyre I e blYlk EEE420 ab Han out

Example 1:

Let us design the following digital bandpass filter. lower stopband edge.' WI, = 0.27[,

lower passband edge.' W I!, = 0.357[

upper passband edge. w !p = 0657[,

upper stopband edge .' w2s = 0.87[

These quantities are shown in the following figure.

As = 60dE

Rp = ldE

Rp = ldE

As = 60dE

0.35 0.35 0 65 0 .8 A,---------,.--,--------,--- -,------c--- w1pi

601------i "-L-l__--'

Solution of Example 1 (M-File) :

ws 1 = 0 . 2 *pi; wp 1 0 . 35*pi; ~~ 2 = 0 . 65*pi; ws2 0 . 8*pi ; As = 60;

t r wi dt h = mi n ( (wp 1-ws 1 ) , (ws2 -wp2) ) ; M = ceil (11*pi /tr_width ) +1; n = [0:1 :M- 1J; we 1 = (ws1+wp1)/2; wc2 = (wp2+ws2)/2; hd = ldeal lp(wc2,M) - ldeal lp(wc1,M); w_b l a = (b l ac kman(M))'; h = hd.*w_bla; [db,mag, p h a,grd,w ] = freqz m(h, [1J);

d e l ta_w =2*p i / 1 000; % Actu al Passband Ripple Rp = -mi n( db( wp 1 /del ta_w+1:1:wp2 / de l ta w)) % Mi n S topband Attenuat i on As = -round( max (db (ws2 / de l ta w+1:1 : 5 0 1 )))

% Plots s ubp l ot( 2/ 2,1) ; stem(n , hd) ; title('Ideal I mpuls e Response ' ) axis ([0 M-1 -0.4 O. 5J) ; xlabel ('n') ; ylabe l ('hd(n) ' )

subplot (2,2,2); s.:em (n, w_ bla) ; titl e ( 'Blackman Wind o w ' ) axis ([0 M- 1 0 1.1J ) ; xlabel ('n'); ylabel ('w(n) ')

s ubplo t (2,2,3) ; stem(n/h); ti t le('Actual I mpulse Response ') axis([ O M- 1 - 0.4 0 .5J); xlabel('n'); y l abe l (' h (n) ')

s u bp l o t(2,2,4); plot (w/p i,db ) ; axi s ( [0 1 -150 10J); t i t l e( 'Magn i t ud e Response in db') ; grid; xl a b e l( 'f requen ey i n p i u n i ts') ; yl abel( ' De cibels ' )

6

Page 7: LAB #8 HANDOUT - Eastern Mediterranean Universityfaraday.ee.emu.edu.tr/eeng420-lab/lab handouts/Lab8.pdf · 2012-12-04 · Lab Assistant Neyre Te rblYlk EE420 Lab Handout • w =

Lab A ssistant: eyre TekblYlk EEE420 Lab Handout

Ideal Im pulse Response Blackman Window

0.4

0.2

-0. 2

-0.4 L-_______~______'

o 20 40 60 n n

Actual Impulse Response Magnitude Response in db

o ._ ... :- - '-: . . . - ---­

0.4 I I 02L ~ -50 _·-- ··_·r··_···_·····:-··_·_·_·_· -...... -_.

Ll , 'c .. ,--;.~' _ J ?: 0·r-~"'-'':£S',;::;.i;z>..v:-":.;"{"7,~::a~~]"'-.J ' _L.' ­ ~ -100 JI ~fr --.-rti~(

-0 2 , ,tt , II ! I

-0.4 --~------------' -150 '-----~--------' o 20 40 60 o 0.5

n frequenc y in pi units

Example 2:

The frequency response of an ideal digital differentiator is given by

O--<wS;J[Hd (e)W.) = {iW'

-iw, -J[ --< 'vjJ --< 0

using a Hamming window of length 21, design a digital FIR differentiator. Plot the time- and the frequency-domain responses.

Solution:

Note that M is an even number, then a = (M -1) /2 is an integer and hd (n) will be

zero for all n. hence M must be an odd number, and this will be a Type-3 linear phase FI R filter.

7

Page 8: LAB #8 HANDOUT - Eastern Mediterranean Universityfaraday.ee.emu.edu.tr/eeng420-lab/lab handouts/Lab8.pdf · 2012-12-04 · Lab Assistant Neyre Te rblYlk EE420 Lab Handout • w =

Lab ss i tant: eyre TekblYlk EEE420 Lab Handout

Solution of Example 2 (M-file):

M = 21 ; a lpha = (M -1 )/2; n = O: 1 :M-1; h d = (c o s (p i * (n -alph a ) » . / (n- a lpha ) ; hd (a lpha+ 1) o·, w_ha . = (ha mmi ng (M) ) , ; h = hd. *w_ham; (Hr, w,P, L] =Hr_Ty pe3 (h) ;

% Plots % subpl ots (l , l , l ) ; subplot(2,2,l) ; stem (n,hd); titl e ( ' Ideal Impulse Response ') a x':' s ([ -l M -l.2 l. 2 ] ) ; xl abel ('n' ) ; yl abe l (' hd(n ) ' )

subpl oL (2 , 2 ,2 ); sL em( n,w_ ham ) ; tit le ( 'Hamming Window ') axi s ( [-1 M Ol. 2 ] ) ; xl abe l ( 'n' ) i y l abel ( 'w (n ) , )

s ubplot (2,2,3 ) ; stem(n,h ) i title ( ' Ac tual Imp u lse Resp o n se ') axis ( [-1 M -l.2 l. 2 ]) i x label ( 'n' ) ; y l abe l ('h (n) ' )

subplot(2,2,4 ) i p l ot (w/pi,Hr / pi ) ; t itle('Amplit ude Response') i gr id; xlabel('frequency in pi unit s') ; y l abe l(' s l ope in p i units') i

axi s ( (0 1 0 1] ) i

Ideal Impulse Response Hamming Window

0.8

-.S f 0.6u ..c

0.4-0.5

0: ,>_(1' I-1

.: '

o 5 10 15 20 o 5 10 15 20 n n

Actual Impulse Response Amplitude Response

0.5

~ 0 (:.u ·.·. -' ,-" ' ;':"­ I -0.5 • ,

r'-1 O L-------------------~

o 5 10 15 20 o 0.5

n frequency in pi units

8