17 nov'09comp30291: section 41 university of manchester school of computer science comp30291...
TRANSCRIPT
17 Nov'09 Comp30291: Section 4 1
University of Manchester
School of Computer Science
Comp30291
Digital Media Processing 2009-10
Section 4
‘Design of FIR digital filters’
17 Nov'09 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
17 Nov'09 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
17 Nov'09 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.
17 Nov'09 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][
17 Nov'09 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)
17 Nov'09 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!
17 Nov'09 Comp30291: Section 4 8
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 ][)(
17 Nov'09 Comp30291: Section 4 9
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
17 Nov'09 Comp30291: Section 4 10
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
17 Nov'09 Comp30291: Section 4 11
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
17 Nov'09 Comp30291: Section 4 12
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
17 Nov'09 Comp30291: Section 4 13
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’
17 Nov'09 Comp30291: Section 4 14
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]);
17 Nov'09 Comp30291: Section 4 15
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
17 Nov'09 Comp30291: Section 4 16
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)
17 Nov'09 Comp30291: Section 4 17
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;
17 Nov'09 Comp30291: Section 4 18
• 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
17 Nov'09 Comp30291: Section 4 19
(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’ applied symmetrically about n=0.
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.
17 Nov'09 Comp30291: Section 4 20
n
h[n] = (1/3)sinc(n/3)
Starting with ‘ideal’ impulse-response:
17 Nov'09 Comp30291: Section 4 21
n
n
h[n]
h[n]
M=10
Truncate to M/2
Delay by M/2 samples
17 Nov'09 Comp30291: Section 4 22
• 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
17 Nov'09 Comp30291: Section 4 23
• 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 ).
17 Nov'09 Comp30291: Section 4 24
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
17 Nov'09 Comp30291: Section 4 25
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);
17 Nov'09 Comp30291: Section 4 26
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
17 Nov'09 Comp30291: Section 4 27
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.
17 Nov'09 Comp30291: Section 4 28
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.
17 Nov'09 Comp30291: Section 4 29
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).
17 Nov'09 Comp30291: Section 4 30
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.
17 Nov'09 Comp30291: Section 4 31
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
17 Nov'09 Comp30291: Section 4 32
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.
17 Nov'09 Comp30291: Section 4 33
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);
17 Nov'09 Comp30291: Section 4 34
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
When coeffs are symmetric, the filter is linear phase.
17 Nov'09 Comp30291: Section 4 35
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:
17 Nov'09 Comp30291: Section 4 36
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
17 Nov'09 Comp30291: Section 4 37
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
17 Nov'09 Comp30291: Section 4 38
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)
17 Nov'09 Comp30291: Section 4 39
Gain-response of 10th order lowpass FIR filter with C = /3
17 Nov'09 Comp30291: Section 4 40
Gain response of 20th order lowpass FIR filter with C = /3
17 Nov'09 Comp30291: Section 4 41
• 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
17 Nov'09 Comp30291: Section 4 42
By truncating {h[n]} for ideal filter, we effectively multiplied {h[n]} by a rectangular window sequence {rM+1[n]} to produce {h[n]} where
2/n :
M/2n M/2- :
0
1 = ][1 M
nrM
4.3. Windowing
M/2-M/2
rM+1[n]
n
17 Nov'09 Comp30291: Section 4 43
• Sudden transitions to zero at rectangular window edges cause the stop-band ripples we saw in the gain-response graphs. • Why is this?
•It may be shown that DTFT of {rM+1[n]} is:
Effect of rectangular window on freq-response
,...2 ,0 : 1
,...2 ,0 : )2/sin(
)2/)1sin(()(1
M
MeR j
M
17 Nov'09 Comp30291: Section 4 44
DTFT of rect-window {rM+1[n]} with M=20
-3 -2 -1 0 1 2 3
-5
0
5
10
15
20
Dirichelet K of order 20
radians/sample
R
• Purely real•Looks a bit like sinc.
• Height of main lobe = M+1
• Area of main lobe 2 Width = 4/(M+1)
17 Nov'09 Comp30291: Section 4 45
MATLAB program to plot RM+1(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');
17 Nov'09 Comp30291: Section 4 46
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:
• This is frequency domain convolution.
• ‘Multiply, find the area then scale by 1/ 2’
•Time-domain multiplication freq-domain convolution.
17 Nov'09 Comp30291: Section 4 47
Apply this formula to rectangular window
• If H(ej) has ideal brick-wall gain-response, & RM+1(ej) is as shown in previous graph, convolving H(ej) with RM+1(ej) reduces cut-off rate & introduces stop-band ripples.
• The sharper the main-lobe of RM+1(ej) and the lower the ripples, the better.
deReHeH jM
jj
12/1 )(
• Multiplying an ideal impulse-resp {h[n]} by the rect window {rM+1[n]} causes H(ej) to be ‘convolved’ with RM+1(ej).
17 Nov'09 Comp30291: Section 4 48
Illustration for ideal /3 lowpass filter
• What happens to area under curve as increases from 0 to ?
3/
3/ 1
3/
3/ 1
2/1
2/1 )(
deR
deReH
jM
jM
j
otherwise : 0
3/3/ : 1 )( If
jeH
17 Nov'09 Comp30291: Section 4 49
=0
/3/3
Area under curve /3 to +/3 when =0
RM+1(ej)
2
17 Nov'09 Comp30291: Section 4 50
=/6
Area under curve /3 to +/3 when =/6
/2/6
RM+1(ej)
2
17 Nov'09 Comp30291: Section 4 51
=/3
Area under curve /3 to +/3 when =/3
2/30
RM+1(ej)
17 Nov'09 Comp30291: Section 4 52
=/2
Area under curve /3 to +/3 when =/2
5/6/6
RM+1(ej)
0
17 Nov'09 Comp30291: Section 4 53
=2/3
Area under curve /3 to +/3 when =2/3
/3
0
RM+1(ej)
17 Nov'09 Comp30291: Section 4 54
Conclusions from these graphs
• Ripples arise from freq-domain convolution between ideal freq-response & RM+1(ej).
• At =0, area is 2 so gain is 1 which is 0 dB.
• At = C (= /3), area is half of 2, so gain 0.5 ( 6 dB) as half of main lobe lies between /3 & + /3.
• As is increased from /3, main lobe no longer included,so area comes from the ripples.
• Ripples are small & alternately +ve & negative, so area & therefore the gain will be small.
• But area will increase & decrease (ripple) as increases.
17 Nov'09 Comp30291: Section 4 55
• Levels of ripples reduced if {rM+1[n]} replaced by non-rectangular window sequence { wM+1[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][1
MnnwM
4.4. Non-rectangular windows
n
M/2 M/2
wM+1[n]
17 Nov'09 Comp30291: Section 4 56
Hann window w21[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
]
17 Nov'09 Comp30291: Section 4 57
Hann window w41[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
]
17 Nov'09 Comp30291: Section 4 58
‘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+1[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][1
MnnwM
17 Nov'09 Comp30291: Section 4 59
Hamming window w21[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
]
17 Nov'09 Comp30291: Section 4 60
Hamming window w41[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
]
17 Nov'09 Comp30291: Section 4 61
• Multiplying {h[n]} by {wM+1[n]} instead of {rM+1[n]} gradually tapers impulse-response towards zero at window edges.•To understand why this reduces stop-band ripples, compare DTFT of {wM+1[n]} with DTFT of {rM+1[n]} 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
17 Nov'09 Comp30291: Section 4 62
DTFT of Hann window {wM+1[n]}
• Height of main lobe = 1 + M/2 ( half that of rect window)• Area of main lobe 2 (same as rect window)• Width of main lobe 8/M ( twice that of rect window)
• It may be shown that:
WM+1(ej) = 0.5RM+1(ej) + 0.25RM+1(ej(-/(1+M/2)))
+ 0.25RM+1(ej(+/(1+M/2)))
Purely real only for a window that is symmetric about =0.
17 Nov'09 Comp30291: Section 4 63
DTFT of Hamming window {hwM+1[n]}
• Height of main lobe = 1 + 0.54M ( half that of rect window)• Area of main lobe 2 (same as rect window)• Width of main lobe 8/M ( twice that of rect window)
• It may be shown that:
HWM+1(ej) = 0.54RM+1(ej) + 0.23RM+1(ej(-/(1+M/2)))
+ 0.23RM+1(ej(+/(1+M/2)))
Let’s stay with Hann for now as the differences are small.
17 Nov'09 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');
17 Nov'09 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))
17 Nov'09 Comp30291: Section 4 66
• Ripples in W21(ej) greatly reduced in comparison to R21(ej).• This is good!• Main lobe W21(ej) less sharp & lower in comparison to R21(ej).• Reduces sharpness of the cut-off • Price paid for reducing stop-band ripples.
Comparing DTFT of Rect & Hann windows
17 Nov'09 Comp30291: Section 4 67
Ideal /3 lowpass filter with Hann window
• As before, consider what happens to area under curve as increases from 0 to , but now with Hann window ?
3/
3/ 1
3/
3/ 1
2/1
2/1 )(
deW
deWeH
jM
jM
j
otherwise : 0
3/3/ : 1 )( If
jeH
17 Nov'09 Comp30291: Section 4 68
=0
/3/3
Area under curve /3 to +/3 when =0
WM+1(ej)
2
radians/sample
DTFT of Hann window
17 Nov'09 Comp30291: Section 4 69
=/3
2/30
Area under curve /3 to +/3 when =/3
WM+1(ej)
radians/sample
DTFT of Hann window
17 Nov'09 Comp30291: Section 4 70
=/25/6/6
Area under curve /3 to +/3 when =/2
WM+1(ej)
0.1
radians/sample
DTFT of Hann window
17 Nov'09 Comp30291: Section 4 71
=2/3/3
Area under curve /3 to +/3 when =2/3
WM+1(ej)
0
radians/sample
DTFT of Hann window
17 Nov'09 Comp30291: Section 4 72
Conclusions from these graphs
• Have plotted freq-domain convolution between an ideal freq-response & DTFT of Hann window.
• At =0, area is 2 so gain is 1 as for rect window.
• At = /3, area is (half of main lobe), so gain 0.5 (-6dB) as for rect window.
• As is increased from /3, area of main lobe disappears more gradually than for rect window.
• Ripples of DTFT of Hann window are much smaller than for rect window, so ripples in the area & therefore the gain will be much reduced.
• Slower cut-off (bad) but reduced stopband ripples (good)
17 Nov'09 Comp30291: Section 4 73
• 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: {w5[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
17 Nov'09 Comp30291: Section 4 74
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
+
17 Nov'09 Comp30291: Section 4 75
Gain-resp from MATLAB: 4th order (Hann)
17 Nov'09 Comp30291: Section 4 76
• 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]);
17 Nov'09 Comp30291: Section 4 77
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
Since coeffs are symmetric, filter is exactly linear phase.
17 Nov'09 Comp30291: Section 4 78
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)
17 Nov'09 Comp30291: Section 4 79
•‘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’
17 Nov'09 Comp30291: Section 4 80
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 so filter is exactly linear phase.
17 Nov'09 Comp30291: Section 4 81
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)
17 Nov'09 Comp30291: Section 4 82
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 next are for Hann window with M=10, 20 & 60:
17 Nov'09 Comp30291: Section 4 83
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)
17 Nov'09 Comp30291: Section 4 84
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)
17 Nov'09 Comp30291: Section 4 85
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)
17 Nov'09 Comp30291: Section 4 86
Tenth order FIR filter with C = /3 ( Hann window )
17 Nov'09 Comp30291: Section 4 87
20th order FIR filter with C = /3 (Hann window)
17 Nov'09 Comp30291: Section 4 88
• 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 dB,
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
17 Nov'09 Comp30291: Section 4 89
• 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(M+1,beta)
• gives a Kaiser window array of length M+1 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
17 Nov'09 Comp30291: Section 4 90
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)
17 Nov'09 Comp30291: Section 4 91
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
17 Nov'09 Comp30291: Section 4 92
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)
17 Nov'09 Comp30291: Section 4 93
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)
17 Nov'09 Comp30291: Section 4 94
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
17 Nov'09 Comp30291: Section 4 95
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)
17 Nov'09 Comp30291: Section 4 96
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
17 Nov'09 Comp30291: Section 4 97
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)
17 Nov'09 Comp30291: Section 4 98
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
17 Nov'09 Comp30291: Section 4 99
• 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.
• Use MATLAB to get {h[n]}
FIR band-pass filter (/4 to /2)
17 Nov'09 Comp30291: Section 4 100
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)
17 Nov'09 Comp30291: Section 4 101
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.
17 Nov'09 Comp30291: Section 4 102
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
17 Nov'09 Comp30291: Section 4 103
• 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
17 Nov'09 Comp30291: Section 4 104
= 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][)(
17 Nov'09 Comp30291: Section 4 105
4.8 Using MATLAB SP Toolbox
• See notes
17 Nov'09 Comp30291: Section 4 106
• 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
17 Nov'09 Comp30291: Section 4 107
Gain of 40th order FIR lowpass filter designed by ‘Remez’
17 Nov'09 Comp30291: Section 4 108
• 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
17 Nov'09 Comp30291: Section 4 109
• 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)
17 Nov'09 Comp30291: Section 4 110
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;
17 Nov'09 Comp30291: Section 4 111
• 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
17 Nov'09 Comp30291: Section 4 112
• 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’
17 Nov'09 Comp30291: Section 4 113
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. Explain why IIR digital filters cannot have exactly linear phase
responses. 7. Explain why it is impossible to implement an ideal ‘brick-wall’ LPF.8. Show that ( ) = -k corresponds to a delay of k samples.9. Rearrange even order linear phase FIR filter to reduce no. of
multipliers.10. Do FIR filters have passband ripples as well as stopband ripples? 11. Explain why FIR filters have stopband ripples where IIR filters do not.12. Explain why the gain of an FIR low pass filter with 0 dB gain at zero
frequency reduces to -6 dB at its cut-off frequency.
17 Nov'09 Comp30291: Section 4 114
Questions 10 & 6
10. Yes but you can hardly see them.
6. 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