Post on 12-Jan-2016
Nov'04 CS3291: Section 4 1
University of Manchester
Department of Computer Science
CS3291
Digital Signal Processing '04-'05
Section 4:
‘A design technique for FIR digital filters’
Nov'04 CS3291: 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-1 x[n]
y[n]
a0 a1
...
aM-1 aM
Nov'04 CS3291: Section 4 3
• Its impulse-response is {..., 0, ..., a0, a1, a2,..., aM, 0, ...} • Its frequency-response is: M H(e j ) = a n e - j n n=0 •Consider problem of choosing a0, a1,..., aM such that H( ej ) is close to some target frequency response. •Use the inverse DTFT:
deeHnh njj )(
2
1][
Nov'04 CS3291: Section 4 4
• Assume we require low-pass filter whose gain-response approximates ideal 'brick-wall' gain-response:.
0
1
Gi( )
-
/3 - /3
If we take phase-response ()=0 for all , the required frequency-response is:
<</3 : 0
3/ : 1)()( )(jj eGeH
Nov'04 CS3291: Section 4 5
By the inverse DTFT,
0n :
0=n :
)3/sin()/1(
3/1
12
1 ][
3/
3/
nn
denh nj
= (1/3)sinc(n/3) for all n.
0:1
0:)sin(
)(sinc wherex
xx
xx
Nov'04 CS3291: Section 4 6
x
1 2-1-2-3 3
1 sinc(x)
Nov'04 CS3291: Section 4 7
• A digital filter with this impulse-response would have exactly the required low-pass frequency-response.
• But {h[n]} has non-zero samples extending from n = - to ,
• Not a finite impulse-response.
• Also not causal.
1/3
h[n]
n
Ideal impulse response
for low-pass filter
cut-off pi/3
Nov'04 CS3291: Section 4 8
To produce a realisable impulse-response:
(1) Truncate {h[n]} to a FIR by setting h[n] to zero for all values of n outside the range -M/2 n M/2 ( assume M is the order of the required FIR filter and is even ).
(2) Delay resulting sequence by M/2 samples to ensure that the first non-zero sample occurs at n = 0.
Nov'04 CS3291: Section 4 9
n
M=10
h[n] = (1/3)sinc(n/3)
Starting with ideal impulse response:
Nov'04 CS3291: Section 4 10
n
n
h[n]
h[n]
M=10Truncate to M/2
Delay by M/2 samples
Nov'04 CS3291: Section 4 11
• Resulting causal impulse response realised by setting an = h[n] for n=0,1,2,...,M.
• Taking M=4, for example, the finite impulse response obtained for the /3 cut-off low-pass specification is : {..,0,..,0, 0.14, 0.28, 0.33 , 0.28 , 0.14 , 0 ,..,0,..}
Nov'04 CS3291: Section 4 12
Gain & phase responses given in Figure 4.4.
z-1 z-1 z-1 z-1 x[n]
y[n]
0.28 0.28 0.14 0.330.14
• Resulting FIR filter is as shown in Figure 4.1 with a0=0.14, a1=0.28, a2=0.33, a3=0.28, a4=0.14.
( Note:4th order FIR filter has 4 delays & 5 multipliers ).
Nov'04 CS3291: Section 4 13
G(
dB
0
-10
-20
-30
/3
-6 dB
Fig 4.4
Nov'04 CS3291: Section 4 14
• Truncation of {h[n]} to M/2 & the M/2 samples delay produces gain & phase responses different from those originally specified.
• Gain-response: cut-off rate not sharp, two "ripples" appear in stop-band, peak of the first ripple at about -21dB.
• Phase-response: not zero for all as orig specified, ( ) = - ( M/2 ) for | | /3;
linear phase in pass-band slope arctan(M/2) with M = 4 . phase-delay M/2 = 2 samples.
Nov'04 CS3291: Section 4 15
Question.Why does delaying {h[n]} by M/2 produce this effect? Answer: If DTFT of {h[n]} is H(ej) = G()ej(), the DTFT of the delayed impulse-response:
/2) M-)(j(
)(j/2Mj-2/
2/
)2/(
e )G(
)eG(e = )(=
][ =
M/2-nm settingby ][ = ]2/[
jjM
n
jmjM
m
jMm
n
jn
eHe
emhe
emheMnh
Delaying {h[n]} by M/2 samples multiplies H(ej) by e-Mj/2 which increases () by -M / 2. Increases phase-delay -() / by M/2 sampling intervals.
Nov'04 CS3291: Section 4 16
• Can we improve low-pass filter by increasing order to ten?
• Taking 11 terms of { (1 / n) sin (n / 3) } we get, after delaying by 5 samples: {...0,-0.055,-.069, 0,.138,.276,.333,.276,.138,0,-.069,-.055,0,...}.
• If we analyse, by computer, a tenth order FIR filter with this impulse-response, we obtain the gain response shown in fig 4.5.
The graph was produced by the following MATLAB statements:
Nov'04 CS3291: Section 4 17
for n=1:11; a(n)= 0.3333*sinc((n-6)/3); end; H=freqz(a,1,1000); whitebg; plot( [0:999]/1000,20*log10(abs(H) ); axis( 0,1,-60,10] ); grid on; xlabel( ‘rel_freq / pi ); ylabel( ‘Gain (dB)’ )
Alternative graph may be produced by the single statement:
freqz( [-0.055, -0.069, 0, 0.138, 0.276, 0.333, 0.276, 0.138, 0, -0.069, -
0.055] );
Nov'04 CS3291: Section 4 18
A note on the phase-response graphs produced by ‘freqz’
• Plots () against rather than phase lag -().
• Segments at phase intervals of 2 or 360o
-()()
/
2
4
2
1
freqz
Nov'04 CS3291: Section 4 19
Gain response of 10th order lowpass FIR filter with C = /3
Nov'04 CS3291: Section 4 20
Gain response of 20th order lowpass FIR filter with C = /3
Nov'04 CS3291: Section 4 21
• Cut-off rate for 10th order FIR filter sharper than for 4th order.
• More stop-band ripples.
• Gain at peak of first ripple after cut-off remains at -21 dB.
• This effect is due to Gibb's phenomenon.
• Linear phase in passband with phase delay of 5 samples.
• Going to 20th order produces even faster cut-off rates,
& more stop-band ripples,
but main stop-band ripple remains at about -20dB.
• To improve matters we need to discuss " windowing ".
Nov'04 CS3291: Section 4 22
4.2. Windowing:
To design FIR filters we multiplied {h[n]} as calculated by inverse DTFT by a rectangular window sequence {r[n]} where
2/n :
M/2n M/2- :
0
1 = ][
Mnr
r[n]
n
Nov'04 CS3291: Section 4 23
• Causes sudden transition to zero at window edges.• It is these transitions that produce the stop-band ripples. • Levels of ripples reduced if {r[n]} replaced by non-rectangular window sequence { w[n] }. • Produces a more gradual transition at the window edges. • Simple non-rectangular window sequence is Hann window • It is a "raised cosine "
M/2 > n :
M/2n -M/2:
0
))2/1/(cos(1(5.0][
Mn
nw
w[n]
n
M/2 -M/2
Nov'04 CS3291: Section 4 24
• Other types of window exist ( e.g. Hamming, Kaiser ). • Multiplying {h[n]} by {w[n]} instead of {r[n]} gradually tapers impulse-response towards zero at window edges. • Consider again low-pass filter example with rel cut-off /3. • Ideal impulse-response was found to be:
{h[n]} = { …..... , 0.14, 0.28, 0.33, 0.28, 0.14, ………}
• When M = 4, the Hann window {w[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,..}
Nov'04 CS3291: Section 4 25
Resulting "Hann-windowed" FIR filter of order 4 is as shown below with a0=0.04, a1=0.21, a2=0.33, a3 = 0.21, a4 = 0.04.
Its gain-response is approximately as shown in Figure 4.9.
z-1 z-1 z-1 z-1 x[n]
y[n]
a0 a1 a3 a4 a2
Nov'04 CS3291: Section 4 26
G(dB
0
-10
-20
-30
-6dB
2
Fig. 4.9
Nov'04 CS3291: Section 4 27
Figure 4.9: 4th order FIR filter (Hann)
Nov'04 CS3291: Section 4 28
4.3 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 order ( Hann windowed ):
Nov'04 CS3291: Section 4 29
Tenth order FIR filter with C = /3 ( Hann window )
Nov'04 CS3291: Section 4 30
MATLAB program to design & graph 10th order FIR lowpass filter with Hann window
w=hanning(11); for n=1:11; a(n) = 0.3333*sinc((n-6)/3)*w(n); end; h = freqz(a,1,1000); plot([0:999]/1000,20*log10(abs(h)),'k'); axis([0,1,-50,0]); grid on; xlabel('Rel_freq / pi'); ylabel('Gain(dB)');
Nov'04 CS3291: Section 4 31
20th order FIR filter with C = /3 (Hann window)
Nov'04 CS3291: Section 4 32
4.4. Highpass, band-pass & band-stop linear phase FIR filters
• Can be designed almost as easily as low-pass.• Remember to define required gain-response GI() from - to +• Make GI(-) = GI(). • Band-pass filter with pass-band from fS/8 to fS/4 has following gain response ideally:-
Gain
1
Nov'04 CS3291: Section 4 33
• Applying the 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, but there is a nicer way
Nov'04 CS3291: Section 4 34
H(ej) = H1(ej) H2(ej)
- /4/2- -/2 -/4
11
deHdeHnh njnj
)(e
2
1 )(e
2
1 ][ j
2j
1
= (1/2)sinc(n/2) - (1/4)sinc(n/4) for - < n <
Nov'04 CS3291: Section 4 35
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/(sin)6/1(
0 : 6/11
nnc
n
Exercise: Derive the impulse-response for an ideal zero phase 'brick-wall' band-stop digital filter with cut-off frequencies L and U radians per sample.
Nov'04 CS3291: Section 4 36
• Technique not restricted to “conventional” gain-responses. • It is not difficult to design a linear phase filter whose gain-response approximates that below:
Gain
1
0.5
Nov'04 CS3291: Section 4 37
4.5. Summary of design technique
To design an FIR digital filter of even order M, with gain response G I () and linear phase by the windowing method,1) Set H(ej) = G I () the required gain-response. This assumes () = 0.2) IDTFT to produce the 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.
Nov'04 CS3291: Section 4 38
• Instead of obtaining H( ej ) = G I ( ), we get e-jM/2G()
• G() is distorted version of GI() due to windowing.
• Phase-response is () = -M/2 which is linear phase . • -() / = M/2 for all .
• Filter coeffs 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 filters are linear phase.
Nov'04 CS3291: Section 4 39
= 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.
• FIR digital filters whose impulse-responses are symmetric are linear phase. • Let {h[n]} = {…, 1, 3, 5, 5, 3, 1, …}
jjjjj
n
njj eeeeeenheH 5432 35531][)(
Nov'04 CS3291: Section 4 40
• Windowing or "Fourier series approxn technique ".
• It is possible to design FIR filters which are not linear phase
Nov'04 CS3291: Section 4 41
4.6 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.6.1 Fractional sampling interval delay filter:
4.6.2. Differentiator:
4.5.3. Hilbert transformer:
See notes for details
Nov'04 CS3291: Section 4 42
4.6.1. Fractional sampling interval delay filter: Gain-response is one for all , but whose phase-response is () = - 0.5 . Therefore:
e -j / 2 : 0 H(e j ) =
e j / 2 : - 0
Nov'04 CS3291: Section 4 43
4.6.2. Differentiator:
Would produce cos(t) from sin (t). FIR filter can act as a differentiator if it outputs {(/T) cos(n)} when the input is {sin(n)} for any in the range - to . Required gain-response is therefore G I () = /T.Instead of specifying a phase response of zero, specify a required phase lead of /2. Required frequency response is:
(/T) e j / 2 : 0 j /T : 0 H(e j ) = =
(/T e - j / 2 : - 0 -j /T : - 0
Nov'04 CS3291: Section 4 44
4.5.3. Hilbert transformer:
G() = 1 and () = -/2 for 0 < < . “Quadrature phase” rather than linear phase. A linear phase component is added to the 90o phase shift when we delay the windowed impulse response for causality. The required frequency response is:
e - j / 2 : 0 -j : 0 H(e j ) = 0 : = 0 = 0 : = 0
e j / 2 : - 0 j : - 0
Nov'04 CS3291: Section 4 45
Designing & implementing FIR digital filters using SP Toolbox
• Design of linear phase FIR digital filters by windowing technique carried out by command 'FIR1'. • Applied to segment of sampled sound by command 'filter'.
• Design & implement 128th order FIR band-pass digital filter with passband 300 Hz to 3.4 k Hz • Apply it to wav file of mono music sampled at 11.025 kHz.
• Notes: (1) FIR cut-off frequencies specified relative to fS/2 .(2) By default FIR1 uses Hamming window; (other windows such as Hann can be specified).(3) Filter scaled so centre of pass-band has magnitude one.
Nov'04 CS3291: Section 4 46
clear all;[x, fs, nbits] = wavread('caprice.wav');%To design:wlow = 2 * pi * 300 / fs ; % radians/sample rel to fswup = 2 * pi * 3400 / fs ; % radians/sample rel to fsa = fir1(128, [wlow wup] / pi ) ;freqz ( a , 1) ; % plot gain & phase% To implement:L=length(x);y = filter(a, 1, x ); % Remember the delay of 64 samples.wavwrite(x,fs,nbits,'capnew.wav');
Nov'04 CS3291: Section 4 47
Remez Exchange Algorithm method:
• 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 .
• 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.
Nov'04 CS3291: Section 4 48
a = remez (40, [0, 0.3, 0.4,1],[1, 1, 0, 0] );h = freqz (a,1,1000);plot([0:999]/1000,20*log10(abs(h)),'k');axis([0,1,-50,0]);grid on;xlabel('Rel_freq / pi');ylabel('Gain(dB)');
Nov'04 CS3291: Section 4 49
Gain of 40th order FIR lowpass filter designed by “ Remez ”
Nov'04 CS3291: Section 4 50
4.9. Some implementation issues4.9.1. Fixed point implementation of FIR digital filters• FIR digital filters often implemented in mobile equipment. • Low power fixed point DSP processors are norm.• 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 all coeffs by a large constant then round: . A0 = 4, A1 = 21, A2 = 33, A3 = 21 , A4 = 4.
We must divide the output by same constant, in this case 100. Instead of 100, we choose a power of two for the constant. Dividing by a power of two (e.g. 1024) is very simple.
Nov'04 CS3291: Section 4 51
• 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. • FIR filters easy to program in fixed point arithmetic. • Never become unstable as there is no feedback. • In some cases, overflows can be allowed to occur • Can risk overflow more readily with FIR digital filters than with IIR digital filters, and thus have greater coefficient accuracy.
• 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.
Nov'04 CS3291: Section 4 52
MATLAB implementation of 4th order low-pass filter using integer arithmetic only:
A = [4 21 33 21 4 ] ; 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 / 100) ;
disp([' Y = ' num2str(Y)]); end;
Nov'04 CS3291: Section 4 53
4.9.2. Alternative signal flow graphs
•Signal flow-graph below has same impulse-response as fig 4.1.
•Same input x[n] applied to all multipliers.
•Could have advantages & other alternatives exist.
z-1 z-1 z-1+ + +
AMA0AM-2AM-1
x[n]
y[n]
Nov'04 CS3291: Section 4 54
PROBLEMS:1 Design a 10th order FIR low-pass digital filter with cut-off at fS/4 with and without a Hann window. Use MATLAB to compare the gain responses obtained.2 Design a 10th order FIR bandpass digital filter with lower and upper cut-off frequencies at /4 and /2 respectively.3 Write a 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 a phase delay N samples, with N an integer, its impulse-response is symmetric about n=N.
Nov'04 CS3291: Section 4 55
8. Design 6th order FIR filter which is linear phase & delays input by half a sampling interval (plus usual 3 for causality).9. Design 6th order linear phase FIR filter whose gain-response approximates that shown in fig 4.13.10. Show that if input to ideal Hilbert transformer is {cos(n)} output will be {sin(n)} for all . 11.Design 8th order FIR approx to all-pass Hilbert transformer . Sketch a graph of its phase-response.12. Show that ( ) = -k corresponds to a delay of k samples.13 Give H(z), {h[n]} and a program for Fig 4.15 & mention any other advantages it may have fig 4.1. 14 Rearrange figs 4.1 and 4.15 for greater efficiency when M is even & the filter is linear phase.15 Explain why IIR digital filters cannot have exactly linear phase responses, whereas FIR filters can.