Lecture 06: Design of Recursive Digital Filters John Chiverton School of Information Technology Mae Fah Luang University 1st Semester 2009/ 2552 Lecture Contents Introduction IIR Filter Design Pole-Zero Placement Method IIR Filter Design from Analogue Filters Summary Outline Introduction IIR Filter Design Pole-Zero Placement Method IIR Filter Design from Analogue Filters Summary What is a Digital Filter? ( Ω) |H ( Ω) | Often used to remove some frequencies from a signal X(Ω) and to allow other frequencies to pass through to the output Y (Ω). π Ω π Ω H(Ω Inv ), Line aria a nt S r Tim e yst em |H ( Ω) | Low Frequencies Ou tpu t, Y Low Pass Band Pass X(Ω ) |H ( Ω) | Middle Frequencies π High Frequencies Inp ut, High Pass Ω Recursive digital filters Input, x[n] Linear Time Invariant System Output, y[n] What is a Recursive digital filter? I “Recursive” comes from the word “to recur” Meaning: to repeat A recursive filter uses past output values (y[n − i]) for the calculation of the current output y[n]: I Recursive Filter Example y[n] = 0.5y[n − 1] + 0.5x[n]. A non-recursive filter only uses input values x[n − i]: I Non-recursive Filter Example y[n] = 0.5x[n − 1] + 0.5x[n]. Generalised Difference Equation Recall the generalised difference equation for causal LTI systems (see Lecture 02): N X a[k]y[n − k] = k=0 M X b[k]x[n − k] k=0 If a[0] = 1, this can then be changed to: y[n] = M X b[k]x[n − k] − k=0 N X a[k]y[n − k]. k=1 The Frequency Response (see Lecture 03) of such a system can be described by: M P H(Ω) = M P b[k] exp(−jkΩ) k=0 exp(0) + N P k=1 = a[k] exp(−jkΩ) b[k] exp(−jkΩ) k=0 N P 1+ k=1 . a[k] exp(−jkΩ) z-transform Representation Fourier based frequency representation: M P H(Ω) = b[k] exp(−jkΩ) k=0 N P 1+ . a[k] exp(−jkΩ) k=1 Can also be represented in the z-domain (z-transform): M P H(z) = b[k]z −k k=0 N P 1+ . a[k]z −k k=1 Both describe a type of frequency response of the system. Comparison of IIR and FIR System Structures The system structure of an IIR filter demonstrates the feedback of the output into the input again. IIR Filter FIR Filter x[n] x[n−1] T x1/4 x[n−2] T x[n−3] x[n] x1/4 x1/4 x1/4 x1/4 x[n−1] T T y[n] y[n] = 1 (x[n] + x[n − 1] + x[n − 2] + x[n − 3]) 4 y[n−1] x[n−3] T x1/4 x1/4 x(−1/10) + x[n−2] T x1/4 + T y[n] y[n] = (x[n] + x[n − 1] + x[n − 2] + x[n − 3]) − 1 y[n − 1] 10 Unit Delay in the z-plane A single z−1 is the same as a unit delay, “T” in a system diagram. x[n] x[n−1] T x1/4 x[n−2] T x1/4 x1/4 x(−1/10) y[n−1] x[n−3] T x1/4 + T y[n] Unit Delay in the z-plane A single z−1 is the same as a unit delay, “T” in a system diagram. x[n] z−1 x1/4 x[n−1] x[n−2] z−1 x1/4 x1/4 x(−1/10) y[n−1] z−1 x1/4 + z−1 x[n−3] y[n] Recursive Digital Filters Recursive digital filters are often known as I Infinite Impulse Response (IIR) Filters as the impulse response of an IIR filter often has an infinite number of coefficients. IIR Filters I Require fewer calculations than FIR filters. I ∴ Faster response to the input signal, I and ∴ shorter frequency response transition width. However! I IIR filters can become unstable. I ∴ Need to think carefully about stability when designing IIR Filters. Outline Introduction IIR Filter Design Pole-Zero Placement Method IIR Filter Design from Analogue Filters Summary IIR Filter Design Overview 1. Filter specification 2. Coefficient calculations 3. Convert transfer function to suitable filter structure 4. Error analysis 5. Implementation Filter Specification Frequency domain parameters - for filter description. 1+ δ p passband ripple 1 1− δ p I Stop band - high attenuation I Pass band - low attenuation (gain≈1) Ω s1 Ω p1 Pass Band Tr a W nsit idt ion h Stop Band Tr an W sitio idt h n δs Ω p2 Ω s2 Stop Band Filter Specification Frequency domain parameters - for filter description. 1+ δ p passband ripple 1 I δp passband ripple 1− δ p I δs stopband ripple I Ωs1 lower stop band edge frequency I Ωp1 lower pass band edge frequency I Ωp2 upper pass band edge frequency δs Ω s1 Ω p1 Pass Band Tr a W nsit idt ion h Stop Band Tr an W sitio idt h n I Ωs2 upper stop band Ω p2 Ω s2 Stop Band edge frequency Outline Introduction IIR Filter Design Pole-Zero Placement Method IIR Filter Design from Analogue Filters Summary Pole-Zero Placement Method A filter can be described in the z-plane with Poles and Zeros (Lecture 04): H(z) = K(z − z1 )(z − z2 )(z − z3 )... zeros Y (z) = = X(z) (z − p1 )(z − p2 )(z − p3 )... poles I Poles located at: z1 , z2 , z3 , ... I Zeros located at: p1 , p2 , p3 , ... Pole-Zero Placement Method A filter can be described in the z-plane with Poles and Zeros (Lecture 04): H(z) = I Poles (X) close to unit circle I I K(z − z1 )(z − z2 )(z − z3 )... zeros Y (z) = = X(z) (z − p1 )(z − p2 )(z − p3 )... poles make large peaks Zeros (O) close to unit circle I make troughs or minima Pole-Zero Placement Method Angle of poles and zeros on z-plane correspond to frequencies that can be used for filter specification. I A bandpass filter, with centre frequency Ω0 radians can have two poles at ±Ω0 radians in the z-plane1 . I Complete attenuation at two frequencies, Ωr1 = 0 radians and Ωr2 = π radians can have two zeros at 0 and π radians. calculated with: r∼ = 1 − Ωcf |H ( Ω) | I The radius of the poles can be or Ωbw r∼ =1− 2 where Ωbw = 2Ωcf is the -3dB bandwidth of the filter. 1 Band Pass Ω 0 (DC) Ω0−Ωcf Ω0+Ωcf Ω0 Complex conjugate pair to make real filter coefficients, when Ω0 6=0 or Ω0 6=π radians (on the real line). π Pole-Zero Placement Method: Example Q. Design a bandpass filter using the Pole-zero placement method with: I centre frequency at Ω0 = π/2; I a bandwidth of Ωbw = π/8; I complete attenuation at Ωr1 = 0 and Ωr2 = π; I and peak unity pass band gain. A. Bandpass filter has x2 poles at ±Ω0 = ±π/2 radians. ∴ H(z) = K zeros (z − r exp(jπ/2))(z − r exp(−jπ/2)) The radii of the poles are given by: Ωbw π/8 r∼ =1− = 0.80365; =1− 2 2 and the zeros are at Ωr1 = 0 and Ωr2 = π, so that H(z) = K (z − exp(jΩr1 ))(z − exp(jΩr2 )) . (z − 0.80365 exp(jπ/2))(z − 0.80365 exp(−jπ/2)) Pole-Zero Placement Method: Example cont’d. As I exp(Ωr1 ) = exp(j0) = cos(0) + j sin(0) = 1 − j0 = 1 I exp(Ωr2 ) = exp(jπ) = cos(π) + j sin(π) = −1 + j0 = −1, then the transfer function becomes: H(z) = K (z − 1)(z + 1) . (z − 0.80365 exp(jπ/2))(z − 0.80365 exp(−jπ/2)) Using Euler’s identity, I exp(jπ/2) = cos(π/2) + j sin(π/2) = +j I and exp(jπ/2) = cos(π/2) − j sin(π/2) = −j, so that H(z) = K (z − 1)(z + 1) . (z − 0.80365j)(z + 0.80365j) the pole zero diagram can then be plotted. Pole-Zero Placement Method: Example cont’d. Recall that H(z) = H(z) = Y (z) , X(z) Y (z) (z − 1)(z + 1) z2 − 1 =K =K 2 . X(z) (z − 0.80365j)(z + 0.80365j) z + 0.64585 Then Y (z)(z 2 + 0.64585) = X(z)K(z 2 − 1). Remembering that each z −1 is a unit delay, so that each z is a unit advance, then the difference equation is: y[n + 2] + 0.64585y[n] = K(x[n + 2] − x[n]) which can be made causal by making n = n − 2 so that y[n] + 0.64585y[n − 2] = K(x[n] − x[n − 2]). K is not known, but can be used to make the peak pass band gain to be unity. Pole-Zero Placement Method: Example cont’d. The frequency response of the filter can be determined from the difference equation: y[n] + 0.64585y[n − 2] = K(x[n] − x[n − 2]), in combination with: M P H(Ω) = Magnitude Frequency Response b[k] exp(−jkΩ) k=0 N P 1+ . a[k] exp(−jkΩ) k=1 So that (using Euler’s identity): H(Ω) = K(1 − cos(2Ω) + j sin(2Ω)) 1 + 0.64585(cos(2Ω) − j sin(2Ω)) which has magnitude frequency response: Mag(H(Ω))2 = K((1 − cos(2Ω))2 + sin2 (2Ω)) . (1 + 0.64585 cos(2Ω))2 + sin2 (2Ω) where K = 0.17708. Pole-Zero Placement Method: Example cont’d. Relating the digital frequencies for previous example to actual frequencies... If the sampling frequency is fs = 500Hz, the sampling frequency corresponds to Ω = 2π, therefore the filter parameters become: I centre frequency at Ω0 = π/2, so actual centre frequency f0 = π/2 2π fs = 125Hz; I a bandwidth of Ωbw = π/8, so actual bandwidth fbw = 31.25Hz; I complete attenuation at Ωr1 = 0 and Ωr2 = π, with actual π frequencies fr1 = 0Hz and fr2 = 2π 500 = 250Hz. Pole-Zero Placement Method, Example 2: Band Stop Filter |H ( Ω) | Q. Design digital bandstop filter using pole-zero placement method with the following parameters: I Centre frequency, Ω0 = π/10 radians (complete attenuation) I Band stop width, Ωw = 2Ωcf = π/20 radians tion No ua tten Band Stop A n atio No nu Atte Attenuation 0 (DC) Ω0−Ωcf Ω0+Ωcf Ω0 Ω π Pole-Zero Placement Method, Example 2: Band Stop Filter I Complete attenuation at Ω0 = π/10, ∴ x2 zeros (complex-conjugate pair) at ±Ω0 = ±π/10: H(z) = K (z − exp(jπ/10))(z − exp(−jπ/10)) poles I Centre frequency at Ω0 = π/10 radians, ∴ x2 poles (complex-conjugate pair) at ±Ω0 = ±π/10, H(z) = K (z − exp(jπ/10))(z − exp(−jπ/10)) (z − r exp(jπ/10))(z − r exp(−jπ/10)) I The poles are scaled with radius r to control the width of the band stop, π/20 Ωw r∼ =1− = 0.92146 =1− 2 2 I resulting in: H(z) = K (z − exp(jπ/10))(z − exp(−jπ/10)) (z − 0.92146 exp(jπ/10))(z − 0.92146 exp(−jπ/10)) Pole-Zero Placement Method, Example 2: Band Stop Filter cont’d. I Transfer function is then (using Euler’s identity like before): H(z) = K z 2 − 1.9021z + 1 z 2 − 1.7527z + 0.84909 I As before, each z is a unit advance, so y[n+2]−1.7527y[n+1]+0.84909y[n] = K(x[n+2]−1.9021x[n+1]+x[n]) letting n = n − 2, making it causal: y[n]−1.7527y[n−1]+0.84909y[n−2] = K(x[n]−1.9021x[n−1]+x[n−2]). I With frequency response: M P H(Ω) = b[k] exp(−jkΩ) k=0 N P 1+ k=1 = a[k] exp(−jkΩ) K(1 − 1.9021 exp(−jΩ) + exp(−j2Ω)) . 1 − 1.7527 exp(−jΩ) + 0.84909 exp(−j2Ω) Pole-Zero Placement Method, Example 2: Band Stop Filter cont’d. Using Euler’s identity: H(z) = K(1 − 1.9021(cos Ω − j sin Ω) + cos 2Ω − j sin 2Ω) . 1 − 1.7527(cos Ω − j sin Ω) + 0.84909(cos 2Ω − j sin 2Ω) Magnitude Frequency response is then: Mag(H(z))2 = K((1 − 1.9021 cos Ω + cos 2Ω)2 + (1.9021 sin Ω − sin 2Ω)2 ) . (1 − 1.7527 cos Ω + 0.84909 cos 2Ω)2 + (1.7527 sin Ω − 0.84909 sin 2Ω)2 ) Magnitude frequency response of the notch or bandstop filter: Outline Introduction IIR Filter Design Pole-Zero Placement Method IIR Filter Design from Analogue Filters Summary Converting Analogue Filters to Digital Filters Most common approach for IIR filter design. I Use well-established analogue filter specifications to design digital IIR filters Two common approaches: I Impulse invariant method I Bilinear transformation Laplace Transform I Analogue filter transfer function h(t) can be specified in the s-plane with the Laplace transform L(h(t)) = H(s) I Hence, the s-plane is for analogue instead of the z-plane (for digital). Can be used to analyse stability of analogue filters, I I Similar to the z-transform for digital filters. ⇐⇒ Discrete (z-plane) Imaginary Axis ON GI RE BL E EG I ON UN ST A jω ER I EG ON UN ER BL Real Axis A E BL A ST ST Analogue (s-plane) UN ST AB L ON ON GI RE GI ST UN LE AB RE ON GI RE E L σ AB L AB ST N IO EG ER T S UN Laplace Transform I Analogue filter transfer function h(t) can be specified in the s-plane with the Laplace transform L(h(t)) = H(s) I Hence, the s-plane is for analogue instead of the z-plane (for digital). Can be used to analyse stability of analogue filters, I I Similar to the z-transform for digital filters. Discrete z-transform Z(h[n]) z-plane H[z] Difference equation, h[n] ⇐⇒ ⇐⇒ ⇐⇒ ⇐⇒ Analogue Laplace transform L(h(t)) s-plane H(s) Differential equation h(t) How to Convert Analogue Frequency to Digital? Problem! I Analogue frequency, ω = 0...∞. I But digital frequency, Ω = 0...2π. So how to convert analogue frequency to digital? Need to swap analogue frequencies with digital frequencies... I If Ω → 2π then Very high analogue frequencies (ω → ∞) I If Ω → 0 then Very low analogue frequencies (ω → 0). Bilinear Transformation IIR Filter Design Bilinear Transformation method replaces analog frequency s or jω with digital frequency Ω using: Ω 2 . s = jω = j tan Ts 2 where ω is analogue frequency, Ω is digital frequency and Ts = 1/fs is the sampling period. OR to find the z-transform H(z): s = jω = 2fs 1 − z −1 1 + z −1 . Bilinear Transformation IIR Filter Design Example: Given an analog filter with: H(ω) = H(s)|s=jω = K(jω − z1 )(jω − z2 )... (jω − p1 )(jω − p2 )... Then bilinear transformation gives H(Ω) = H(s)|s=j2fs tan( Ω ) 2 Ω K(j2fs tan 2 − z1 )(j2fs tan Ω2 − z2 )... = (j2fs tan Ω2 − p1 )(j2fs tan Ω2 − p2 )... or... H(z) = H(s)|s=2f “ 1−z−1 ” s 1+z −1 −1 −1 1−z K(2fs 1−z − z )(2f 1 s 1+z −1 − z2 )... 1+z −1 = −1 1−z −1 (2fs 1−z − p )(2f − p2 )... 1 s −1 −1 1+z 1+z Bilinear Transformation Example Q. Convert the single pole low pass analog filter: H(s) = ωcf s + ωcf into a digital filter (z-plane form) with digital cut-off frequency Ωcf = 0.2π using the bilinear transformation. A. 1. Calculate analogue cut-off frequency ωcf from digital cut-off frequency Ωcf = 0.2π: ωcf = 2fs tan(Ωcf /2) = 2fs tan(0.1π) = 2fs A 2. Therefore analogue transfer function: H(s) = 2fs A s + 2fs A −1 3. Apply bilinear transformation: s = 2fs 1−z : 1+z −1 H(z) = 2fs A −1 2fs 1−z 1+z −1 + 2fs A „ = 2fs 2fs « A(1 + z −1 ) (1 − + A(1 + z −1 ) z −1 ) Bilinear Transformation Example cont’d. The z-transform transfer function of the filter is then: H(z) = Y (z) A + Az −1 = X(z) 1 + A + (A − 1)z −1 Stability Analysis Rearranging to determine the poles for stability analysis gives: H(z) = I So there is 1 pole at z + A−1 1+A z+1 A . 1 + A z + A−1 1+A = 0 or z = − A−1 . 1+A I Remember A = tan(0.1π), so the pole is: z = −0.50953, I the magnitude is less than 1, so the filter is stable. (1) Bilinear Transformation Example cont’d. Difference Equation The difference equation can now be found. Multiplying both sides by both denominators of equation (1) results in ˘ ¯ ˘ ¯ Y (z) 1 + A + (A − 1)z −1 = X(z) A + Az −1 Remembering that each z −1 is a unit delay, so that (1 + A)y[n] + (A − 1)y[n − 1] = Ax[n] + Ax[n − 1] Dividing through by (1 + A) and rearranging gives y[n] = A−1 A (x[n] + x[n − 1]) − y[n − 1], 1+A 1+A where A = tan(0.1π). This is now a difference equation we can use to filter a signal. Bilinear Transformation Example cont’d. Frequency Response The frequency response can be found directly using the bilinear transformation or from the z-transform transfer function. We will compare both approaches. Bilinear Transformation The analogue transfer function from step 2 in earlier slide was: H(s) = 2fs A s + 2fs A The s-plane variable s can be replaced by the Fourier complex frequency variable jω, ˛ 2fs A ˛ = H(ω) = H(s)˛ . jω + 2fs A s=jω The Fourier `frequency can then be converted to the digitial frequency Ω using ´ ω = 2fs tan Ω2 (see earlier slide): ˛ ˛ H(Ω) = H(ω)˛ ω=2fs tan( Ω 2 ) = 2fs A A `Ω´ `Ω´ = j2fs tan 2 + 2fs A j tan 2 + A Bilinear Transformation Example cont’d. So the magnitude frequency response calculated directly from the Bilinear transformation is: s s (tan(0.1π))2 A2 = ` |H(Ω)| = ` ` Ω ´´2 ` Ω ´´2 2 +A + (tan(0.1π))2 tan 2 tan 2 The designed cut-off frequency Ωcf = 0.2π is confirmed by this plot. Bilinear Transformation Example cont’d. Frequency Response from z-Transform Transfer Function Remember the z-transform transfer function calculated earlier (equation (1)): H(z) = Y (z) A + Az −1 . = X(z) 1 + A + (A − 1)z −1 This can be converted to the frequency response using (see earlier slides and lecture 04) M P ˛ ˛ H(Ω) = H(z)˛ = z=exp(−jkΩ) k=0 N P b[k] exp(−jkΩ) a[k] exp(−jkΩ) k=0 So: I b[0] = b[1] = A, I a[0] = 1 + A, I a[1] = A − 1. So the frequency response is given by (using Euler’s identity): H(Ω) = A + A(cos(Ω) − j sin(Ω)) (1 + A) + (A − 1)(cos(Ω) − j sin(Ω)). Bilinear Transformation Example cont’d. So the magnitude is given by: s |H(Ω)| = (A + A cos(Ω))2 + (A sin(Ω))2 ((1 + A) + (A − 1) cos(Ω))2 + ((A − 1) sin(Ω)))2 . which is the same as the frequency response calculated directly from the bilinear transformation. The Bilinear transformation is more quicker here. Famous Analogue Filters Butterworth I Magnitude frequency response: |H(ω)| = 1 ”2n ff1/2 “ ω 1+ ω cf Chebyshev Magnitude frequency response: |H(ω)| = |H ( ω) | magnitude response I n 2 1+2 Cn Ideal 1.0 −1/2 2 (1+ ε ) Chebyshev 0.707 Butterworth ωcf frequency ω 1 “ ω ωcf ”o1/2 Butterworth and Chebyshev Analog Filters Bilinear transformation: jω = j2fs tan(Ω/2), therefore: Magnitude Frequency Response (Analogue) (Digital) |H(ω)| |H(Ω)| Butterworth » 1 “ ”2n –1/2 1+ ωω cf Chebyshev h 2 1+2 Cn 1 “ 1 » “ ”2n –1/2 tan(Ω/2) 1+ tan(Ω /2) cf 1 ω ωcf ”i1/2 h 2 1+2 Cn “ tan(Ω/2) tan(Ωcf /2) ”i1/2 Comparison of IIR and FIR filters Characteristic Multiplications Coefficient quantification sensitivity Overflow errors Stability Linear phase Simulate analog filter Coefficient memory Design complexity IIR least can be high FIR most very low can be high by design no yes very low always always no least moderate most simple adapted from “Understanding digital signal processing” by R. G. Lyons Outline Introduction IIR Filter Design Pole-Zero Placement Method IIR Filter Design from Analogue Filters Summary Lecture Summary This lecture has covered: I Introduction to IIR filters. I Frequency domain parameters. I Pole-zero placement method for IIR filter design I Band stop filter design I IIR filter design from analog filters using bilinear transformation method