university of manchester school of computer science comp30291 digital media processing 2007-08

Post on 07-Jan-2016

23 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

University of Manchester School of Computer Science Comp30291 Digital Media Processing 2007-08 Section 4 ‘Design of FIR digital filters’. x[n]. z -1. z -1. z -1. z -1. a M. a M-1. a 0. a 1. y[n]. 4.1.Introduction - PowerPoint PPT Presentation

TRANSCRIPT

21 Oct'08 Comp30291: Section 4 1

University of Manchester

School of Computer Science

Comp30291

Digital Media Processing 2007-08

Section 4

‘Design of FIR digital filters’

21 Oct'08 Comp30291: Section 4 2

4.1.Introduction

FIR digital filter of order M implemented by programming the signal-flow-graph shown below. Its difference equation is:

y[n] = a0x[n] + a1x[n-1] + a2x[n-2] + ... + aMx[n-M]

z-1 z-1 z-1 z-1x[n]

y[n]

a0 a1

...

aM-1aM

21 Oct'08 Comp30291: Section 4 3

10th order FIR digital filter

z-1 z-1z-1 z-1z-1 z-1 z-1 z-1 z-1 z-1

+ + + + +++ + + +

x[n]

y[n]

a0 a1 a2a10a9a8a3 a4 a5 a7a6

21 Oct'08 Comp30291: Section 4 4

• Its impulse-response is {h[n]} = {..., 0, ..., a0, a1, a2,..., aM, 0, ...} • Taking DTFT of impulse-response gives the frequency-response:

deeHnh njj )(

2

1][

Background, objective & methodology

• Objective is to choose a0, a1,..., aM such that H(ej) is close to some target frequency-response H’(ej) ). • Inverse DTFT of H’(ej) gives required impulse-response :

M

n

njn

n

njj eaenheH0

][ )(

• Methodology is to use inverse DTFT to get an impulse- response {h[n]} & then realise some approximation to it.

21 Oct'08 Comp30291: Section 4 5

Observations about the inverse-DTFT

• It is an integral• It has complex numbers

• Range of integration is from - to

so it involves negative frequencies.

deeHnh njj )(

2

1][

21 Oct'08 Comp30291: Section 4 6

Reminder about integration

aaat

at

eea

ea

dtedttx

1

1

)(

atat aedt

dxetx then )( If (1)

b

adttx curveunder area is )(

any x(t),For (2)

a b

t

(Have +ve & ve areas)

x(t)

21 Oct'08 Comp30291: Section 4 7

Reminder about complex numbers

• Let x = a + j.b, j = [-1]• Modulus: |x| = [a2 + b2] • Arg(x)=tan-1(b/a) + {.sign(b) if a < 0}

= tan2(b,a) = angle(a + j.b) : range - to • Polar: x = Rej where R = |x| & = Arg(x)• De Moivre: ej = cos() + j.sin() • e-j = cos() - j.sin() ej + e-j = 2cos() & ej - e-j = 2 j.sin()• Complex conjugate: x* = a - j.b = Re-j

• That’s about it!

21 Oct'08 Comp30291: Section 4 8

Bits of MATHs

a

aaaaa

MM

1

1...1 :series Geometric

132

ea.eb = e(a+b)

(ea)2 = e2a

|A|.|B| = |A.B| but |A+B| |A| + |B|

etc.

21 Oct'08 Comp30291: Section 4 9

What about the negative frequencies?

• Examine the DTFT formula for H(ej).

• If h[n] real then h[n]ej is complex-conj of h[n]e-j.• Adding up terms gives H(e-j ) as complex conj of H(ej). • G() = G(-) since G() = |H(ej)| & G() = H(e-j)| • (Mod of a complex no. is Mod of its complex conj.)• (-) = () since () = Arg(H(ej)) & (-) = Arg(H(e-j)) • (Arg of a complex no. is Arg of its complex conj).

n

njj enheH ][)(

n

njj enheH ][)(

21 Oct'08 Comp30291: Section 4 10

Gain & phase response graphs with ve frequencies

G()

- 0

-

-()

As G(-) = G(),gain-response is always symmetric about =0

As () = (),phase-response is always anti-symmetric about = 0

21 Oct'08 Comp30291: Section 4 11

Assume we require lowpass filter whose gain-response approximates the ideal 'brick-wall' gain-response:

If we take phase-response () = 0 for all , the required target frequency-response is:

<</3 : 0

3/ : 1)()( )(jj eGeH

4.2. Design of an FIR low-pass filter

G()

/3/3 0 -

1

21 Oct'08 Comp30291: Section 4 12

By inverse DTFT, required impulse-response is:

)3/sin(22

1

2

1

0when 1

2

1

12

1 )(

2

1 ][

3/3/

3/

3/

3/

3/

njjn

eejn

nejn

dedeeHnh

jnjn

nj

njnjj

0 when )3/sin(1

nnn

21 Oct'08 Comp30291: Section 4 13

To complete the calculation:

0 : 1

0 : )sin(

)(sinc wherex

xx

xx

n allfor 3

sinc3

1

0n :

0n :

3/1

)3/sin(1

nn

n[n]h

3

1 )3/(3/

2

1

2

1 1

2

1 ][ ,0When 3/

3/

3/

3/

dΩnhn

21 Oct'08 Comp30291: Section 4 14

Graph of sinc(x) against x

x

1 2-1-2-3 3

1sinc(x)

-4

Main ‘lobe’

‘Zero-crossings’ at x =1, 2, 3,

etc.

‘Ripples’

21 Oct'08 Comp30291: Section 4 15

Plotting this ‘sinc’ graph in MATLAB

clear all; close all; clc;

x = [-10: 0.1 : 10];

y = sinc(x);

figure(1); plot(x,y); grid on;

title('sinc function');

xlabel( 'x'); ylabel('sinc(x)');

legend('sinc(x)', 'Location','Best');

axis([-10 10 -0.3 1]);

21 Oct'08 Comp30291: Section 4 16

Graph of sinc(x) against x

-10 -8 -6 -4 -2 0 2 4 6 8 10

-0.2

0

0.2

0.4

0.6

0.8

sinc(x)

x

sin

c(x

)

sinc(x)

‘Main lobe’

‘Ripples’

‘Zero-crossings’ at x =1, 2, 3, etc

21 Oct'08 Comp30291: Section 4 17

Impulse-response for this ideal (brick-wall) lowpass filter

-20 -15 -10 -5 0 5 10 15 20-0.1

-0.05

0

0.05

0.1

0.15

0.2

0.25

0.3

0.35h[n] = (1/3) sinc(n/3)

n

h[n

]

(1/3)sinc(n/3)

21 Oct'08 Comp30291: Section 4 18

Plotting this ‘stem’ graph in MATLAB

n=[-20:20]; h = (1/3)*sinc(n/3);

figure(1); stem(n,h,'.:'); grid on;

title('h[n] = (1/3) sinc(n/3)');

xlabel( 'n'); ylabel('h[n]');

legend('(1/3)sinc(n/3)', 'Location','Best');

axis([-20 20 -0.1 0.35]);for n=-20:20,

disp(sprintf('n:%2d, h[n]: %6.3f' ,n,h(n+21)));

end;

21 Oct'08 Comp30291: Section 4 19

• Reading from the graph or the MATLAB display, we get:

{h[n]} = { ..., -0.055, -0.07, 0, 0.14, 0.28, 0.33, 0.28, 0.14, 0, -0.07, -0.055, ... }

• A digital filter with this impulse-response would have exactly the ideal frequency-response we applied to the inverse-DTFT.

•‘Brick-wall’ low-pass gain response & phase = 0 for all .

• But {h[n]} has non-zero samples extending from n = - to ,

• Not a finite impulse-response.

• Also not causal.

• Not realisable in practice.

‘Ideal’ impulse-response

21 Oct'08 Comp30291: Section 4 20

(2) Delay resulting sequence by M/2 samples to ensure that the first non-zero sample occurs at n = 0.

Step (1) is ‘truncation’ or ‘windowing’

otherwise : 0

2

2 : ][ ][Set (1)

Mn

Mnhnh

To produce a realisable impulse-response {h[n]}:

Assume M is order of required filter & that M is even.

21 Oct'08 Comp30291: Section 4 21

n

h[n] = (1/3)sinc(n/3)

Starting with ‘ideal’ impulse-response:

21 Oct'08 Comp30291: Section 4 22

n

n

h[n]

h[n]

M=10

Truncate to M/2

Delay by M/2 samples

21 Oct'08 Comp30291: Section 4 23

• If M=10, finite impulse response obtained is:

{ ...0, -0.055, -0.07, 0, 0.14, 0.28, 0.33, 0.28, 0.14, 0, -0.07, -0.055, 0... }

• Obtained by truncating & delaying {h[n]} for ‘ideal’ lowpass digital filter with cut-off /3 radians/sample.

• Taking M = 4, we would obtain: {.., 0, .., 0, 0.14, 0.28, 0.33 , 0.28 , 0.14 , 0 ,..,0,..}

• Resulting causal impulse-response now realised by setting: an = h[n] for n = 0,1,2,...,M.

Causal & finite impulse-response

21 Oct'08 Comp30291: Section 4 24

• Gain & phase responses may be derived by MATLAB.

FIR filter realisation (M=4)

z-1 z-1 z-1 z-1 x[n]

y[n]

0.28 0.28 0.14 0.330.14

a0a1 a2

a4a3

+

( Note:4th order FIR filter has 4 delays & 5 multipliers ).

21 Oct'08 Comp30291: Section 4 25

Plot gain & phase responses of 4th order FIR filter

0 500 1000 1500 2000 2500 3000 3500 4000-200

-100

0

100

Frequency (Hz)

Pha

se (

degr

ees)

0 500 1000 1500 2000 2500 3000 3500 4000-60

-40

-20

0

20

Frequency (Hz)

Mag

nitu

de (

dB)

FC

Fs/2

-6dB

/3 Fs/6

-21 dB

Straight line

Jumpsby 180O

Gain dropsto - dB

21 Oct'08 Comp30291: Section 4 26

MATLAB prog for truncating & plotting gain & phase

n=[-20:20];

h = (1/3)*sinc(n/3);

M=4; % FIR filter order

Fs = 8000; % Sampling freq (Hz)

for n=-M/2 : M/2

a(n + M/2 + 1) = h(n+21);

end;

figure(1); freqz(a,1,200,Fs);

21 Oct'08 Comp30291: Section 4 27

Gain-response of 4th order FIR filter (zoomed)

• Gain starts at 0 dB in pass-band & falls to -6 dB at cut-off frequency.

• There are two ‘stop-band ripples’ & gain is -21 dB at peak of first.

FC

Fs/2

-6dB

/3 Fs/6

0 500 1000 1500 2000 2500 3000 3500 4000

-60

-40

-20

0

20

Frequency (Hz)

Mag

nitu

de (

dB)

-21 dB

Gain dropsto - dB

21 Oct'08 Comp30291: Section 4 28

Phase-lag response of 4th order FIR filter

F Hz

4000200010000

100

200

300O

/2

2 Phase-lag

FC

Fs/2

3000

• Linear phase in pass-band

• Don’t worry about 180 degree jumps in stop-band for now.

21 Oct'08 Comp30291: Section 4 29

Why do we not get a zero phase-response?• We started by specifying that phase = 0 for all .

()

-

• We ended up with a phase-response as follows:

-

-()

Only pass-band part is shown here

• Phase response affected by the delay of M/2 samples.

21 Oct'08 Comp30291: Section 4 30

Estimation of phase-delay from slope

• For a linear phase LTI system, -() / is ‘phase-delay’ in sampling intervals.

• Look at phase-response graph in pass-band.• Phase decreases by 180o in 2000Hz (Fs = 8000 Hz)• radians in /2 radians/second• -() / /2 = 2 sampling intervals• So the phase-delay is 2. • Not a surprise as we delayed the impulse-response by 2

samples to make it causal (M=4).

21 Oct'08 Comp30291: Section 4 31

Using MATLAB functions ‘fir1’ & ‘freqz’

• Same result obtained using ‘fir1’ as follows:

c = fir1(4, 0.33, rectwin(5), 'noscale');• Reason for rectwin(5) & ‘noscale’ will be clear later.• To plot gain & phase response:

freqz(c, 1, 500, Fs);• Plots 500 points & frequency-axis to range 0-Fs/2 !!• Plots () against rather than phase lag -().• Phase ‘unwrapped’ to avoid 360o jumps.

21 Oct'08 Comp30291: Section 4 32

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-200

-150

-100

-50

0

50

100

Normalized Frequency ( rad/sample)

Ph

as

e (

de

gre

es

)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

-45

-40

-35

-30

-25

-20

-15

-10

-5

0

Normalized Frequency ( rad/sample)

Ma

gn

itu

de

(d

B)

Gain & phase responses of 4th order FIR filter by MATLAB

21 Oct'08 Comp30291: Section 4 33

Effect of truncating {h[n]} to M/2 & delaying by M/2 samples

• Gain & phase responses different from those originally specified.

• Gain-response: cut-off rate not ‘brick-wall’, 0 dB at 0 Hz & drops to -6 dB at cut-off

frequency, ‘ripples’ and ‘zeros’ appear in stop-band, peak of the first ripple at about -21dB.

• Phase-response: not zero for all as originally specified, linear phase in pass-band

-( )/ = M/2 for | | /3; phase-delay = M/2 sampling intervals.

Jumps of 180o occur in stop-band. Jumps of 360o avoided by unwrapping.

21 Oct'08 Comp30291: Section 4 34

Can we improve low-pass filter by increasing order to ten?

• Taking 11 terms of { (1/3)sinc(n/3)} we get, after delaying by 5 samples: {...0,-0.055,-.069, 0,.138,.276,.333,.276,.138,0,-.069,-.055,0,...}.

• To obtain same result from MATLAB7:

c = fir1(10, 0.33, rectwin(11), 'noscale'); freqz(c);

21 Oct'08 Comp30291: Section 4 35

10th order FIR digital lowpass filter(/3 rect)

z-1 z-1z-1 z-1z-1 z-1 z-1 z-1 z-1 z-1

+ + + + +++ + + +

x[n]

y[n]

-.055 -.07 0 -.055-.0700.14 .28 .33 0.14.28

The coeffs are symmetric because the filter is linear phase.

21 Oct'08 Comp30291: Section 4 36

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-400

-350

-300

-250

-200

-150

-100

-50

0

Normalized Frequency ( rad/sample)

Ph

as

e (

de

gre

es

)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9-50

-45

-40

-35

-30

-25

-20

-15

-10

-5

0

Normalized Frequency ( rad/sample)

Ma

gn

itu

de

(d

B)

For 10th order low-pass filter with C = /3:

21 Oct'08 Comp30291: Section 4 37

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-700

-600

-500

-400

-300

-200

-100

0

Normalized Frequency ( rad/sample)

Ph

as

e (

de

gre

es

)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-40

-35

-30

-25

-20

-15

-10

-5

0

Normalized Frequency ( rad/sample)

Ma

gn

itu

de

(d

B)

Gain & phase responses of 20th order lowpass FIR filter with C = /3

21 Oct'08 Comp30291: Section 4 38

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-2000

-1800

-1600

-1400

-1200

-1000

-800

-600

-400

-200

0

Normalized Frequency ( rad/sample)

Ph

as

e (

de

gre

es

)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

-50

-40

-30

-20

-10

0

Normalized Frequency ( rad/sample)

Ma

gn

itu

de

(d

B)

Gain & phase responses of 60th order lowpass FIR filter with C = /3

21 Oct'08 Comp30291: Section 4 39

Gain & phase of 100th order low-pass FIR filter with C = /3

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-3500

-3000

-2500

-2000

-1500

-1000

-500

0

Normalized Frequency ( rad/sample)

Ph

as

e (

de

gre

es

)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

-45

-40

-35

-30

-25

-20

-15

-10

-5

0

Normalized Frequency ( rad/sample)

Ma

gn

itu

de

(d

B)

21 Oct'08 Comp30291: Section 4 40

Gain-response of 10th order lowpass FIR filter with C = /3

21 Oct'08 Comp30291: Section 4 41

Gain response of 20th order lowpass FIR filter with C = /3

21 Oct'08 Comp30291: Section 4 42

• Cut-off rate increases as order (M) increases.

• Number of stop-band ripples increases with order.

• Gain at peak of first ripple after cut-off remains at -21 dB.

• Remains exactly ‘linear phase’ in pass-band but slope of

phase-response (=phase-delay) increases since -() / =

M/2.

• NB: freqz ‘unwraps’ phase-response avoiding 360o jumps!

• Filter not really improving with increasing order.

• To improve matters we need to discuss ‘windowing’.

Effect of increasing order on gain & phase responses

21 Oct'08 Comp30291: Section 4 43

By truncating {h[n]} for ideal filter, we effectively multiplied {h[n]} by a rectangular window sequence {rM[n]} to produce {h[n]} where

2/n :

M/2n M/2- :

0

1 = ][

MnrM

4.3. Windowing

M/2-M/2

rM[n]

n

21 Oct'08 Comp30291: Section 4 44

• Sudden transitions to zero at rectangular window edges cause the stop-band ripples we saw in the gain-response graphs. • Why is this? Consider DTFT of {rM[n]}

Effect of rectangular window on freq-response

M

n

njMjM

Mn

nj

n

njM

jM eeeenreR

0

2/2/

2/

.1][)(

)2/sin(

)2/)1sin((

11

1

)...1(

2/2/

2/)1(2/)1(

)12/()2/()1(2/

322/

M

ee

ee

e

ee

e

ee

eeeee

jj

MjMj

j

MjMj

j

MjMj

MjjjjMj

when 0, 2, ...

21 Oct'08 Comp30291: Section 4 45

DTFT of rect-window {rM[n]}

,...2 ,0 : 1

,...2 ,0 : )2/sin(

)2/)1sin(()(

M

MeR j

M

When = 0, RM(ej) = M+1

-3 -2 -1 0 1 2 3

-5

0

5

10

15

20

Dirichelet K of order 20

radians/sample

R

• Shows RM(ej) for M=20• Purely real.• Looks a bit like sinc.• Has main-lobe & ripples.• Ripples cause stop-band ripples in gain-resp

21 Oct'08 Comp30291: Section 4 46

MATLAB program to plot RM(ej)

M = 20;

W = -3.2 : 0.001 : 3.2;

if W==0, R=M+1;

else R=sin((M+1)*W/2)./sin(W/2);

end;

figure(1); plot(W,R); grid on;

title(sprintf('Dirichlet K of order %d ', M));

axis([-3.2 3.2 -M*0.3 M+2]);

xlabel('radians/sample'); ylabel('R');

21 Oct'08 Comp30291: Section 4 47

Frequency-domain convolution

deYeX jj

2/1

• Multiplying two sequences {x[n]} & {[y[n]} to produce {x[n].y[n]} is called ‘time-domain multiplication’.

• It may be shown* that if {x[n]} has DTFT X(ej) & [y[n]} has DTFT Y(ej) then the DTFT of {x[n].y[n]} is:

• Time-domain multiplication is equivalent to frequency-domain convolution.

*Appendix B

21 Oct'08 Comp30291: Section 4 48

Apply this formula to rectangular window

• If H(ej) has ideal brick-wall gain-response, & RM(ej) is as shown in previous graph, convolving H(ej) with RM(ej) reduces cut-off rate & introduces stop-band ripples.

• The sharper the main-lobe of RM(ej) and the lower the ripples, the better.

deReHeH jM

jj

2/1 )(

• Multiplying an ideal impulse resp {h[n]} by the rect window {rM[n]} causes H(ej) to be ‘convolved’ with RM(ej).

21 Oct'08 Comp30291: Section 4 49

Illustration for ideal /3 lowpass filter

• What happens to area under curve as increases from 0 to ?

3/

3/2/1 )(

deReH jM

j

θ)by Ω replace(can otherwise : 0

3/3/ : 1 )( If

jeH

21 Oct'08 Comp30291: Section 4 50

=0

/3/3

Area under curve /3 to +/3:

1

21 Oct'08 Comp30291: Section 4 51

=/6

/3/3

Area 1

21 Oct'08 Comp30291: Section 4 52

=/3

/3/3

Area 0.5

21 Oct'08 Comp30291: Section 4 53

=/2

/3/3

Area 0(ripples)

21 Oct'08 Comp30291: Section 4 54

=2/3

/3/3

Area 0(ripples)

21 Oct'08 Comp30291: Section 4 55

Conclusions from these graphs

• Ripples arise from freq-domain convolution between ideal freq-response & RM(ej).

• At =C (= /3), gain is 0.5 ( 6 dB)

because half of main lobe lies between -/3 & /3.

21 Oct'08 Comp30291: Section 4 56

• Levels of ripples reduced if {rM[n]} replaced by non-rectangular window sequence { wM[n] }. • Produces a more gradual transition at the window edges. • Simple non-rectangular window sequence is Hann window • It’s a ‘raised cosine’ with M+1 non-zero samples centred on n=0.

M/2 > n :

M/2n M/2- :

0

))2/1/(cos(1(5.0][

Mn

nwM

4.4. Non-rectangular windows

n

M/2 M/2

wM[n]

21 Oct'08 Comp30291: Section 4 57

Hann window w20[n]

-10 -8 -6 -4 -2 0 2 4 6 8 100

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1Hann window order 20

n

W[n

]

21 Oct'08 Comp30291: Section 4 58

Hann window w40[n]

-20 -15 -10 -5 0 5 10 15 200

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1Hann window order 40

n

W[n

]

21 Oct'08 Comp30291: Section 4 59

‘Hamming’ & other ‘Hann’ windows

• Slightly different formulae exist for the Hann window.• In practice, the difference is usually unimportant.• MATLAB has 2 functions: ‘hann’ & ‘hanning’.

• ‘hanning(M+1)’ gives our Hann window {wM[n]}

• But it’s shifted by 1+M/2 samples to start at n=1.• Best known non-rectangular window is ‘Hamming’.• Its name & its formula are fairly similar to ‘Hann’:

M/2 > n :

M/2n M/2- :

0

)/2cos(46.054.0][

Mn

nwM

21 Oct'08 Comp30291: Section 4 60

Hamming window w20[n]

-10 -8 -6 -4 -2 0 2 4 6 8 100

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1Hamming window order 20

n

W[n

]

21 Oct'08 Comp30291: Section 4 61

Hamming window w40[n]

-20 -15 -10 -5 0 5 10 15 200

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1Hamming window order 40

n

W[n

]

21 Oct'08 Comp30291: Section 4 62

• Multiplying {h[n]} by {wM[n]} instead of {rM[n]} gradually tapers impulse-response towards zero at window edges.•To understand why this reduces stop-band ripples, compare DTFT of {wM[n]} with DTFT of {rM[n]}. Hann window with M=20.

Effect of non-rectangular windows

-3 -2 -1 0 1 2 3

-5

0

5

10

15

20

DTFT of Hann & Rect windows, order 20

radians/sample

R

Hann

Rect

21 Oct'08 Comp30291: Section 4 63

DTFT of Hann window {wM[n]}

• Formula was needed for plotting previous graph.

• wM[n] = 0.5(1+cos(n/(1+M/2) ) rM[n]

= 0.5rM[n]+ 0.25(ejn/(1+M/2) + e-jn/(1+M/2))rM[n]

where {rM[n]} is rect window.

• We know RM(ej) already.

• It is easy to show that multiplying {wM[n]} by {e-jDn} for any

constant D gives us: RM(ej( - D))

WM(ej) = 0.5RM(ej) + 0.25RM(ej(-/(1+M/2)))

+ 0.25RM(ej(+/(1+M/2)))

Purely real only for a window that is symmetric about =0.

21 Oct'08 Comp30291: Section 4 64

To plot DTFT of Hann & Rect windows

clear all; close all; clc

M=20;

W = -3.2:0.001: 3.2; W=W+0.0000001; % Sorry

R=sin((M+1)*W/2)./sin(W/2);

W1=W-pi/(1+M/2); W2=W+pi/(1+M/2);

R1=sin((M+1)*W1/2)./sin(W1/2); R2=sin((M+1)*W2/2)./sin(W2/2);

Hann=0.5*R + 0.25*R1+0.25*R2;

figure(1); plot(W,Hann,W,R); grid on;

title(sprintf('DTFT of Hann & Rect windows, order %d ', M));

axis([-3.2 3.2 -M*0.3 (M+2)]);

xlabel('radians/sample'); ylabel(‘DTFT');

legend('Hann','Rect','Location','Best');

21 Oct'08 Comp30291: Section 4 65

Plot on dB scale

-3 -2 -1 0 1 2 3-40

-30

-20

-10

0

10

20

30DTFT of Hann & Rect windows, order 20

radians/sample

DT

FT

- d

B

Hann

Rect

20.

log 1

0(a

bs(D

TF

T))

21 Oct'08 Comp30291: Section 4 66

• Ripples in W20(ej) greatly reduced in comparison to R20(ej).• This is good!• Main lobe W20(ej) less sharp & lower in comparison to R20(ej).• Reduces sharpness of the cut-off • Price to be paid for reducing stop-band ripples.

Comparing DTFT of Rect & Hann windows

21 Oct'08 Comp30291: Section 4 67

• Ideal impulse-response was found to be:

{h[n]} = { …..... , 0.14, 0.28, 0.33, 0.28, 0.14, ………}

• When M = 4, Hann window is: {w4[n]} = {..,0,..,0, 0.25, 0.75, 1, 0.75, 0.25, 0,..,0,..}

• Multiplying term by term & delaying by M/2 = 2 samples we get: {..,0,..,0, 0.04, 0.21, 0.33, 0.21, 0.04, 0,..,0,..}

• Consider again low-pass filter with cut-off /3 radians/sample

Applying 4th order Hann window

21 Oct'08 Comp30291: Section 4 68

Resulting ‘Hann-windowed’ FIR filter of order 4:

Its gain-response is shown on next slide.

z-1 z-1 z-1 z-1 x[n]

y[n]

0.21 0.21 0.04 0.330.04

a0a1 a2

a4a3

+

21 Oct'08 Comp30291: Section 4 69

Gain-resp from MATLAB: 4th order (Hann)

21 Oct'08 Comp30291: Section 4 70

• To design 10th order FIR lowpass filter with Hann window & cut-off frequency /3 ( Fs/6):

Effect of increasing order to 10

clear all;M=10for n= -M/2 : M/2 w = 0.5 *(1+cos(n*pi/(1+M/2)) ); c(1+n+M/2) = (1/3)*sinc(n/3)*w;end;Fs = 8000; freqz(c,1,500,Fs); axis([0 4000 -60 0]);

21 Oct'08 Comp30291: Section 4 71

10th order FIR lowpass (/3, Hann)

z-1 z-1z-1 z-1z-1 z-1 z-1 z-1 z-1 z-1

+ + + + +++ + + +

x[n]

y[n]

-.004 -.017 0 -.004-.01700.1 .257 .333 0.1.257

Coeffs are symmetric because filter is exactly linear phase.

21 Oct'08 Comp30291: Section 4 72

Gain & phase resp for 10th order FIR (/3,Hann)

0 500 1000 1500 2000 2500 3000 3500 4000-600

-400

-200

0

Frequency (Hz)

Ph

as

e (

de

gre

es

)

0 500 1000 1500 2000 2500 3000 3500 4000-60

-40

-20

0

Frequency (Hz)

Ma

gn

itu

de

(d

B)

21 Oct'08 Comp30291: Section 4 73

•‘fir1’ uses ‘windowing method’ we have just seen.• By default it uses a Hamming window.• Also scales coeffs to make gain exactly 0 dB at 0 Hz • To design 10th order FIR lowpass filter with Hamming window & cut-off frequency /3 ( Fs/6):

c=fir1(10, 0.33);

Use of MATLAB function ‘fir1’

21 Oct'08 Comp30291: Section 4 74

10th order FIR lowpass (/3, Hamming)

z-1 z-1z-1 z-1z-1 z-1 z-1 z-1 z-1 z-1

+ + + + +++ + + +

x[n]

y[n]

-.005 -.01 0 -.005-.0100.1 .25 .33 0.1.25

Again, coeffs are symmetric because filter is exactly linear phase.

21 Oct'08 Comp30291: Section 4 75

Gain & phase resp for 10th order FIR (/3,Hamming)

0 500 1000 1500 2000 2500 3000 3500 4000-800

-600

-400

-200

0

Frequency (Hz)

Pha

se (

degr

ees)

0 500 1000 1500 2000 2500 3000 3500 4000-60

-40

-20

0

Frequency (Hz)

Mag

nitu

de (

dB)

21 Oct'08 Comp30291: Section 4 76

Effect of windowing on freq-response of FIR digital filter

• Effect is to gradually reduce amplitude of ideal impulse-response towards zero at edges of window rather than to abruptly truncate.

• Effect on gain-response of FIR filter obtained is:

i) to greatly reduce stop-band ripples ( good ). ii) to reduce the cut-off rate ( bad ).

• Phase-response is not affected in the pass-band.

• We can improve the cut-off rate by going to higher orders. • Graphs below are for 10th, 20th & 60th order ( Hann windowed ):

21 Oct'08 Comp30291: Section 4 77

Tenth order FIR filter with C = /3 ( Hann window )

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-600

-500

-400

-300

-200

-100

0

Normalized Frequency ( rad/sample)

Ph

as

e (

de

gre

es

)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

-55

-50

-45

-40

-35

-30

-25

-20

-15

-10

-5

Normalized Frequency ( rad/sample)

Ma

gn

itu

de

(d

B)

21 Oct'08 Comp30291: Section 4 78

20th order FIR filter with C = /3 (Hann window)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-900

-800

-700

-600

-500

-400

-300

-200

-100

0

Normalized Frequency ( rad/sample)

Ph

as

e (

de

gre

es

)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

-70

-60

-50

-40

-30

-20

-10

0

Normalized Frequency ( rad/sample)

Ma

gn

itu

de

(d

B)

21 Oct'08 Comp30291: Section 4 79

60th order FIR filter with C = /3 (Hann window)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-2500

-2000

-1500

-1000

-500

0

Normalized Frequency ( rad/sample)

Ph

as

e (

de

gre

es

)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

-90

-80

-70

-60

-50

-40

-30

-20

-10

0

Normalized Frequency ( rad/sample)

Ma

gn

itu

de

(d

B)

21 Oct'08 Comp30291: Section 4 80

Tenth order FIR filter with C = /3 ( Hann window )

21 Oct'08 Comp30291: Section 4 81

20th order FIR filter with C = /3 (Hann window)

21 Oct'08 Comp30291: Section 4 82

• Reduced from about –21 dB to about –44 dB.

• At frequency of first stop-band ripple:

since 20log10(1/10) =-20 dB,

amplitude reduced by factor 10 with rect window.

since 20 log10(1/100) = -40,

amplitude reduced by factor >100 with Hann window.

i.e. 5 sin(t) becomes 0.5 sin(t) or 0.05 sin(t) .

• What do we do if this is not good enough?

• Answer, use a different window (e.g. Hamming, Kaiser)

• Hamming window similar to Hann but slightly better.

Effect of Hann window on first stop-band ripple

21 Oct'08 Comp30291: Section 4 83

• Offers a range of options from rectangular, through Hamming towards even lower stop-band ripples.

• Ripple reduction at expense of less sharp cut-off rate.

• MATLAB command:

KW = kaiser(N,beta)

• produces a Kaiser window array of length N for any value of beta > 0.

• When beta () = 0, this is a rectangular window & when beta = 5.4414 we get a Hamming window.

• Increasing beta further gives further reduced stop-band ripples with a reduced cut-off sharpness.

4.5. Kaiser window

21 Oct'08 Comp30291: Section 4 84

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-3000

-2000

-1000

0

Normalized Frequency ( rad/sample)

Pha

se (

degr

ees)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9-100

-50

0

Normalized Frequency ( rad/sample)

Mag

nitu

de (

dB)

Order 60 FIR LPF c/o /3 designed with Kaiser window (=5)

21 Oct'08 Comp30291: Section 4 85

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-3000

-2000

-1000

0

Normalized Frequency ( rad/sample)

Pha

se (

degr

ees)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

-80

-60

-40

-20

0

Normalized Frequency ( rad/sample)

Mag

nitu

de (

dB)

Order 60 FIR LPF c/o /3 designed with Hamming window

21 Oct'08 Comp30291: Section 4 86

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-3000

-2000

-1000

0

Normalized Frequency ( rad/sample)

Phase (

degre

es)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

-100

-50

0

Normalized Frequency ( rad/sample)

Magnitude (

dB

)

Order 60 FIR LPF c/o /3 designed with Kaiser window (=8)

21 Oct'08 Comp30291: Section 4 87

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-3500

-3000

-2500

-2000

-1500

-1000

-500

0

Normalized Frequency ( rad/sample)

Pha

se (

degr

ees)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

-120

-100

-80

-60

-40

-20

0

Normalized Frequency ( rad/sample)

Mag

nitu

de (

dB)

Order 100 FIR LPF (/3) designed with Kaiser window (=8)

21 Oct'08 Comp30291: Section 4 88

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-4000

-3000

-2000

-1000

0

Normalized Frequency ( rad/sample)

Phase (

degre

es)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

-80

-60

-40

-20

0

Normalized Frequency ( rad/sample)

Magnitude (

dB

)100th order FIR LPF (/3) designed with Hamming window

21 Oct'08 Comp30291: Section 4 89

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-4000

-3000

-2000

-1000

0

Normalized Frequency ( rad/sample)

Phase (

degre

es)

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

-100

-50

0

Normalized Frequency ( rad/sample)

Magnitude (

dB

)

Order 100 FIR LPF (/3) designed with Kaiser window (=10)

21 Oct'08 Comp30291: Section 4 90

clear all;

beta = 5; N=60;

kw = kaiser(N+1,beta);

hw=hamming(N+1);

a=fir1(N, 0.33, rectwin(N+1), 'noscale');

for n=1:N+1

akw(n)=a(n)*kw(n); ahw(n) = a(n)*hw(n);

end;

figure(1); freqz(akw); grid on;

figure(2); freqz(ahw); grid on;

My MATLAB test program

21 Oct'08 Comp30291: Section 4 91

10 20 30 40 50 60 70 80 90 1000

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Plot of Kaiser window (beta = 10)

21 Oct'08 Comp30291: Section 4 92

4.6. Highpass, bandpass & bandstop linear phase FIR filters

• Can be designed almost as easily as low-pass.• Remember to define required gain-response G() from - to +• Make G(-) = G(). • Band-pass filter with pass-band from FS/8 to FS/4 has following gain response ideally:- Gain

1

21 Oct'08 Comp30291: Section 4 93

• Applying inverse DTFT (not forgetting negative ):

|| 2/ : 0

2/ || 4/ : 1

4/ || : 0

)( jeH

• Taking () = 0 for all initially as before, we obtain:

dededeeHnh njnjnjj 2/

4/

4/

2/ 1

2

1 1

2

1 )(

2

1 ][

• Can evaluate this, apply window & delay as before.

• But there is an easier way to get {h[n]}

FIR band-pass filter (/4 to /2)

21 Oct'08 Comp30291: Section 4 94

H(ej) = H1(ej) H2(ej)

deHdeHnh njnj

)(e

2

1 )(e

2

1 ][ j

2j

1

= (1/2)sinc(n/2) (1/4)sinc(n/4) for < n <

- /4/2- -/2 -/4

11

H1(ej) H2(ej)

Impulse-response for ideal Fs/4-Fs/2 band-pass filter

21 Oct'08 Comp30291: Section 4 95

100th order FIR band-pass filter(/4-/2 Rect)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-15000

-10000

-5000

0

5000

Normalized Frequency ( rad/sample)

Ph

as

e (

de

gre

es

)

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-40

-30

-20

-10

0

Normalized Frequency ( rad/sample)

Ma

gn

itu

de

(d

B)

21 Oct'08 Comp30291: Section 4 96

Exercise By a similar method, or otherwise, show that the impulse-response for an ideal zero phase 'brick-wall' high-pass filter with cut-off frequency /6 radians per sample (i.e. one twelfth of the sampling frequency) is:

h[n] = sinc(n) - (1/6)sinc(n/6) for - < n <

0 : )6/(sinc)6/1(

0 : 6/5

nn

n

21 Oct'08 Comp30291: Section 4 97

FIR filter design in MATLAB

c = fir1(10,0.33,'high') designs a 10th order high-pass filter.

c = fir1(10,[0.2 0.4],'bandpass') designs a 10th order band-pass filter with cut-off frequencies 0.2 and 0.4.

c = fir1(20,[0.2 0.4],'stop') designs a 20th order band-stop filter with cut-off frequencies 0.2 and 0.4.

• By default ‘fir1’ uses a ‘Hamming’ window (very similar to a Hann) and scales the pass-band gain to 0 dB.• Linear phase response obtained.

21 Oct'08 Comp30291: Section 4 98

• Technique not restricted to ‘conventional’ gain-responses. • Design an FIR filter whose gain-response approximates:

Gain

1

0.5

Exercise: Other FIR digital filters

Solution: h[n]= (1/2)sinc(n/2) 0.5 (1/4)sinc(n/4) for < n <

21 Oct'08 Comp30291: Section 4 99

4.7. Summary of ‘windowing method’

To design FIR filter of even order M, with gain-response approximating G() & linear phase,

1) Set H(ej) = G(). This assumes () = 0.2) I-DTFT to produce ideal impulse-response {h[n]}.3) Window to M/2 using chosen window.4) Delay windowed impulse-response by M/2 samples.5) Realise by setting multipliers of FIR filter.

MATLAB routine fir1 does all this for LP, HP, BP, BS

21 Oct'08 Comp30291: Section 4 100

• Instead H(ej) = ej0G( ), we get H(ej) = e-jM/2G() • G() is distorted version of G() due to windowing.• Phase-response is () = -M/2 in pass-band • Linear phase with phase-delay: -() / = M/2 samples.• Filter coeffs are symmetric about M/2.

e.g. {…2, -3, 5, 7, 5, -3, 2, …} M =6 (even)

{…, 1, 3, 5, 5, 3, 1, …} M=5 (odd)• This is because the FIR filter is linear phase.

Properties of resulting FIR digital filter

21 Oct'08 Comp30291: Section 4 101

= e-5j/2 (e5j/2 +3e3j/2 +5ej/2 +5e-j/2 +3e-3j/2 +e-5j/2 ) = e-5j/2 (2cos(2.5) + 6cos (1.5) + 10cos(/2) )

= G()ej() with () = -5/2.

• Hence () / = -5/2 = constant, so H(ej) is linear phase.

Demonstrate that an FIR filter whose impulse-response is symmetric is linear phase.

• Let {h[n]} = {…, 1, 3, 5, 5, 3, 1, …} (symmetric)

jjjjj

n

njj eeeeeenheH 5432 35531][)(

21 Oct'08 Comp30291: Section 4 102

4.8. Further applications of windowing design technique

• Technique even more powerful than has been yet indicated• Not restricted to linear phase filters. • Consider some further examples of its use.

4.8.1 Fractional sampling interval delay filter:

4.8.2. Differentiator:

4.8.3. Hilbert transformer:

See notes for details (no exam questions on this)

21 Oct'08 Comp30291: Section 4 103

• Better than windowing technique, but more complicated. • Available in MATLAB. • Design 40th order FIR lowpass filter whose gain is unity (0 dB) in range 0 to 0.3 radians/sample & zero in range 0.4 to . a = remez (40, [0, 0.3, 0.4, 1], [1, 1, 0, 0] );• The 41 coefficients will be found in array ‘a’. • Produces 'equi-ripple' gain-responses where peaks of stop-band ripples are equal rather than decreasing with increasing frequency. • Highest peak in stop-band lower than for FIR filter of same order designed by windowing technique to have same cut-off rate. • There are 'equi-ripple' pass-band ripples.

4.9. Remez Exchange Algorithm method

21 Oct'08 Comp30291: Section 4 104

Gain of 40th order FIR lowpass filter designed by ‘Remez’

21 Oct'08 Comp30291: Section 4 105

• FIR digital filters often implemented in mobile equipment. • Low power fixed point DSP processors are used.• Typically with a basic 16-bit word-length. • Must be programmed using only integer arithmetic.• Take 4th order FIR filter with impulse response:

{….. 0.04, 0.21, 0.33, 0.21, 0.04, …...}. • Rounding each coeff to nearest integer clearly a mistake. • Multiply each coeff by a large constant, e.g. 100, then round: .

A0 = 4, A1 = 21, A2 = 33, A3 = 21 , A4 = 4.

• We must divide the output by same constant. • Instead of 100, choose a power of two for the constant.

4.10. Fixed point implementatn of FIR digital filters

21 Oct'08 Comp30291: Section 4 106

• Dividing by a power of two (e.g. 1024) is very simple• It’s just an ‘arithmetic right-shift’ operation.• Available instruction on DSPs. • The larger the constant, the more accurate the coefficients. • Careful not to choose too large a constant• If integers produced get too large, we risk overflow • Difficult balancing act between inaccuracy & overflow. • If constant is 2^10 (=1024), rounded 4th order coeffs become:

A0 = 35 A1 = 212 A2 = 341 A3 = 212 A4 = 35• MATLAB prog on next slide uses integer arithmetic • Ready to be ported to a DSP

Fixed point implementation (cont)

21 Oct'08 Comp30291: Section 4 107

4th order low-pass filter using integer arithmetic only

A = [35 212 341 212 35] ; x = [0 0 0 0 0 ] ; while 1

x(1) = input( 'X = '); Y = A(1)*x(1); for k = 5 : -1: 2

Y = Y + A(k)*x(k);x(k) = x(k-1);

end; Y = round( Y/1024); %Arith right-shift 10 places disp([' Y = ' num2str(Y)]); end;

21 Oct'08 Comp30291: Section 4 108

• FIR filters easy to program in fixed point arithmetic. • Never become unstable as there is no feedback. • Can be exactly linear phase• In some cases, overflows can be allowed to occur since if gain is never greater than 1, you know that a +ve overflow will eventually be cancelled out by a ve overflow or vice versa.• This works if you do not use ‘saturation mode’ arithmetic which avoids ‘wrap-round’. • Can risk overflow more readily with FIR digital filters than with IIR digital filters, & thus have greater coefficient accuracy.

Disadvantage: FIR need higher orders than IIR (later).

4.11. Advantages of FIR filters compared with IIR

21 Oct'08 Comp30291: Section 4 109

• Scaling by 1024, is adopting a 'Q-format' of ten.• Programmer assumes a binary point to exist ten bit positions from the right within the 16-bit word.

DSP language: ‘Q-format’

21 Oct'08 Comp30291: Section 4 110

PROBLEMS1. Design a 100th order FIR low-pass digital filter with cut-off at fS/4 with &

without a Hann window. Use MATLAB to compare gain responses obtained.2. Design 10th order FIR bandpass filter with cut-off frequencies at /4 & /2.3. Write MATLAB program for one of these filters using integer arithmetic only.4. Design a 4th order FIR high-pass filter with cut-off at /3.5. Do all FIR filters have exactly linear phase responses?6. Show that if { h[n] } is real, H( e-j ) = H*( ej ) and hence that:

dneHnh j ))(cos(|)(| 1

][0

7. Show that if H(ej) is linear phase with phase delay N samples, with N an integer, its impulse-response is symmetric about n=N.8. Design 6th order linear phase FIR filter whose gain-response approximates that shown in fig 4.13.9. Show that ( ) = -k corresponds to a delay of k samples.10. Rearrange even order linear phase FIR filter to reduce no. of multipliers.11. Do FIR filters have passband ripples as well as stop band ripples?12. Explain why IIR digital filters cannot have exactly linear phase responses.

21 Oct'08 Comp30291: Section 4 111

Discussion of Question 10

z-1 z-1 z-1+ + +

AMA0AM-2AM-1

x[n]

y[n]

• Alternative signal-flow graph is given below.

• Check that it has the same impulse response as normal one.

• Note that multiplier order is reversed.

• It has advantage that input to all multipliers is same.

21 Oct'08 Comp30291: Section 4 112

Discussion of question 10 (cont)

+

A0 A3A2A1

x[n]

y[n]z-1 z-1 z-1+ + z-1+ z-1+ z-1+

Linear phase filter can be rearranged to save multipliers as follows:

Usual FIR signal-flow graph can also be rearranged when linear phase.

Exercise for you.

21 Oct'08 Comp30291: Section 4 113

Questions 11 & 12

11. Yes but you can hardly see them.

12. For linear phase, impulse-response must be symmetric about some value of n, say n=M. If it is an IIR it goes on for ever as n .

So it must go on for ever backwards as n -.

Would have to be non-zero for values on n<0; i.e. non-causal.

n

h[n]If h[n] symmetric & IIR it must be non-causal

M

21 Oct'08 Comp30291: Section 4 114

Appendix A: Effect of delaying {h[n]} by M/2 samples

If DTFT of {h[n]} is G(), the DTFT of {h[n-M/2] is:

)G(e

][ =

M/2-nm settingby ][ = ]2

[

/2j-

2/

)2/(

M

m

jmjM

m

jMm

n

jn

emhe

emheM

nh

The phase lead is now -M/ 2 instead of zero. Phase-delay -() / = M/2 sampling intervals.

21 Oct'08 Comp30291: Section 4 115

Appendix B: Proof of frequency-domain convolution

•DTFT of {x[n].y[n]} is:

• Time-domain multiplication is equivalent to frequency-domain convolution.

deYeX

denxeY

edeeYnxenynx

jj

n

njj

n

njnjj

n

nj

2/1

][2/1

2/1 ][][][

)(

top related