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

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?

Often used to remove some frequencies from a X(Ω) and to allow other frequencies to pass through to the output Y (Ω). | ) Ω ( H |

Ω Low Pass

Low Frequencies π Ω Output, Y( ) | ) Ω ( H |

Band Pass

Middle Frequencies π Ω Ω H( ), Linear Time Invariant System | ) Ω ( H |

Ω High Pass

π Ω Input, X( ) High Frequencies Recursive digital filters

Linear Time Input, x[n] Output, y[n] Invariant System

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 M X X a[k]y[n − k] = b[k]x[n − k] k=0 k=0 If a[0] = 1, this can then be changed to:

M N X X y[n] = b[k]x[n − k] − a[k]y[n − k]. k=0 k=1 The (see Lecture 03) of such a system can be described by: M M P b[k] exp(−jkΩ) P b[k] exp(−jkΩ) H(Ω) = k=0 = k=0 . N N exp(0) + P a[k] exp(−jkΩ) 1 + P a[k] exp(−jkΩ) k=1 k=1 z-transform Representation

Fourier based frequency representation:

M P b[k] exp(−jkΩ) H(Ω) = k=0 . N 1 + P a[k] exp(−jkΩ) k=1 Can also be represented in the z-domain (z-transform):

M P b[k]z−k H(z) = k=0 . N 1 + P 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. FIR Filter IIR Filter

x[n]x[n−1] x[n−2] x[n−3] x[n]x[n−1] x[n−2] x[n−3] TTT TTT

x1/4 x1/4 x1/4 x1/4 x1/4 x1/4 x1/4 x1/4

+ x(−1/10) +

y[n] y[n] y[n−1] T

y[n] = y[n] = 1 (x[n] + x[n − 1] + x[n − 2] + x[n − 3]) (x[n] + x[n − 1] + x[n − 2] + x[n − 3]) 4 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] x[n−2] x[n−3] TTT

x1/4 x1/4 x1/4 x1/4

x(−1/10) +

y[n] y[n−1] T 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] x[n−2] x[n−3] z−1 z−1 z−1

x1/4 x1/4 x1/4 x1/4

x(−1/10) +

−1 y[n] y[n−1] z Recursive Digital Filters

Recursive digital filters are often known as

I Infinite (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 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)

δ s

Stop Band Pass Band Stop Band

Transition Width TransitionWidth Ω Ω s1 p1 Ωp2 Ωs2 Filter Specification

Frequency domain parameters - for filter description.

1+δ p passband ripple 1

δ 1− p I δp passband ripple I δs stopband ripple I Ωs1 lower stop band edge frequency

I Ωp1 lower pass band edge frequency

I Ωp2 upper pass band

δ s edge frequency I Ωs2 upper stop band

Stop Band Pass Band Stop Band edge frequency

Transition Width TransitionWidth Ω Ω s1 p1 Ωp2 Ωs2 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):

Y (z) K(z − z )(z − z )(z − z )... zeros H(z) = = 1 2 3 = 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):

Y (z) K(z − z )(z − z )(z − z )... zeros H(z) = = 1 2 3 = X(z) (z − p1)(z − p2)(z − p3)... poles

I Poles (X) close to unit circle

I make large peaks

I 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 1 ±Ω0 radians in the z-plane .

I Complete attenuation at two frequencies, Ωr1 = 0 radians and Ωr2 = π radians can have two zeros at0 and π radians.

I The radius of the poles can be | )

calculated with: Ω ( H | r =∼ 1 − Ωcf

or Band Pass Ω r =∼ 1 − bw 2 Ω Ω0−Ωcf Ω0+Ωcf π where Ωbw = 2Ωcf is the -3dB 0 Ω0 bandwidth of the filter. (DC)

1 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.

zeros H(z) = K ∴ (z − r exp(jπ/2))(z − r exp(−jπ/2))

The radii of the poles are given by:

Ωbw π/8 r =∼ 1 − = 1 − = 0.80365; 2 2

and the zeros are at Ωr1 = 0 and Ωr2 = π, so that

(z − exp(jΩ ))(z − exp(jΩ )) H(z) = K r1 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:

(z − 1)(z + 1) H(z) = K . (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

(z − 1)(z + 1) H(z) = K . (z − 0.80365j)(z + 0.80365j)

the pole zero diagram can then be plotted. Pole-Zero Placement Method: Example cont’d.

Y (z) Recall that H(z) = X(z) ,

Y (z) (z − 1)(z + 1) z2 − 1 H(z) = = K = K . X(z) (z − 0.80365j)(z + 0.80365j) z2 + 0.64585

Then Y (z)(z2 + 0.64585) = X(z)K(z2 − 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 b[k] exp(−jkΩ) Magnitude Frequency Response H(Ω) = k=0 . N 1 + P a[k] exp(−jkΩ) k=1

So that (using Euler’s identity):

K(1 − cos(2Ω) + j sin(2Ω)) H(Ω) = 1 + 0.64585(cos(2Ω) − j sin(2Ω))

which has magnitude frequency response: where K = 0.17708. K((1 − cos(2Ω))2 + sin2(2Ω)) Mag(H(Ω))2 = . (1 + 0.64585 cos(2Ω))2 + sin2(2Ω) 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 π/2 f0 = 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

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 | ) Ω ( H |

Band Stop

No Attenuation No Attenuation Attenuation Ω Ω0−Ωcf Ω0+Ωcf π 0 Ω0 (DC) Pole-Zero Placement Method, Example 2: Band Stop Filter

I Complete attenuation at Ω0 = π/10, ∴ x2 zeros (complex-conjugate pair) at ±Ω0 = ±π/10: (z − exp(jπ/10))(z − exp(−jπ/10)) H(z) = K poles

I Centre frequency at Ω0 = π/10 radians, ∴ x2 poles (complex-conjugate pair) at ±Ω0 = ±π/10, (z − exp(jπ/10))(z − exp(−jπ/10)) H(z) = K (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 r =∼ 1 − w = 1 − = 0.92146 2 2 I resulting in: (z − exp(jπ/10))(z − exp(−jπ/10)) H(z) = K (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): z2 − 1.9021z + 1 H(z) = K z2 − 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 b[k] exp(−jkΩ) K(1 − 1.9021 exp(−jΩ) + exp(−j2Ω)) H(Ω) = k=0 = . N 1 − 1.7527 exp(−jΩ) + 0.84909 exp(−j2Ω) 1 + P a[k] exp(−jkΩ) k=1 Pole-Zero Placement Method, Example 2: Band Stop Filter cont’d.

Using Euler’s identity: K(1 − 1.9021(cos Ω − j sin Ω) + cos 2Ω − j sin 2Ω) H(z) = . 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).

I Can be used to analyse stability of analogue filters,

I Similar to the z-transform for digital filters. Discrete (z-plane) ⇐⇒ Analogue (s-plane)

UNSTABLE REGION j ω Imaginary Axis

UNSTABLE REGION

UNSTABLE REGION Real Axis σ STABLE REGION STABLE REGION UNSTABLE REGION

UNSTABLE REGION 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).

I Can be used to analyse stability of analogue filters,

I Similar to the z-transform for digital filters. Discrete ⇐⇒ Analogue z-transform Z(h[n]) ⇐⇒ Laplace transform L(h(t)) z-plane H[z] ⇐⇒ s-plane H(s) Difference equation, h[n] ⇐⇒ 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):

1 − z−1  s = jω = 2f . s 1 + z−1 Bilinear Transformation IIR Filter Design

Example: Given an analog filter with:

K(jω − z1)(jω − z2)... H(ω) = H(s)|s=jω = (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)| “ 1−z−1 ” s=2fs 1+z−1  1−z−1   1−z−1  K(2fs 1+z−1 − z1)(2fs 1+z−1 − z2)... =  1−z−1   1−z−1  (2fs 1+z−1 − p1)(2fs 1+z−1 − p2)... 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π) = 2fsA

2. Therefore analogue transfer function: 2f A H(s) = s s + 2fsA

1−z−1 3. Apply bilinear transformation: s = 2fs 1+z−1 :

„ « −1 2fsA 2fs A(1 + z ) H(z) = −1 = −1 −1 1−z 2fs (1 − z ) + A(1 + z ) 2fs 1+z−1 + 2fsA Bilinear Transformation Example cont’d.

The z-transform transfer function of the filter is then: Y (z) A + Az−1 H(z) = = (1) X(z) 1 + A + (A − 1)z−1 Stability Analysis

Rearranging to determine the poles for stability analysis gives: A z + 1 H(z) = . 1 + A A−1 z + 1+A

A−1 A−1 I So there is 1 pole at z + 1+A = 0 or z = − 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. 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 A A − 1 y[n] = (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: 2f A H(s) = s s + 2fsA The s-plane variable s can be replaced by the Fourier complex frequency variable jω, ˛ 2f A H(ω) = H(s)˛ = s . ˛s=jω jω + 2fsA The Fourier frequency can then be converted to the digitial frequency Ω using ` Ω ´ ω = 2fs tan 2 (see earlier slide): ˛ 2f A A ˛ s H(Ω) = H(ω) = Ω = Ω ˛ω=2f tan Ω ` ´ ` ´ s ( 2 ) j2fs tan 2 + 2fsA j tan 2 + A Bilinear Transformation Example cont’d.

So the magnitude frequency response calculated directly from the Bilinear transformation is: s s A2 (tan(0.1π))2 |H(Ω)| = = ` ` Ω ´´2 2 ` ` Ω ´´2 2 tan 2 + A tan 2 + (tan(0.1π))

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)): Y (z) A + Az−1 H(z) = = . 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 b[k] exp(−jkΩ) ˛ H(Ω) = H(z)˛ = k=0 ˛z=exp(−jkΩ) N P 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): A + A(cos(Ω) − j sin(Ω)) H(Ω) = (1 + A) + (A − 1)(cos(Ω) − j sin(Ω)). Bilinear Transformation Example cont’d.

So the magnitude is given by: s (A + A cos(Ω))2 + (A sin(Ω))2 |H(Ω)| = ((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 1 I Magnitude frequency response: |H(ω)| =  “ ”2nff1/2 1+ ω ωcf Chebyshev 1 I Magnitude frequency response: |H(ω)| = 1/2 n 2 2 “ ω ”o 1+ Cn ωcf

|H ( ω) | Ideal 1.0 −1/2 2 Chebyshev (1+ ε )

0.707 Butterworth

magnitude response ω ω cf frequency Butterworth and Chebyshev Analog Filters

Bilinear transformation: jω = j2fs tan(Ω/2), therefore:

Magnitude Frequency Response (Analogue)(Digital) |H(ω)| |H(Ω)|

Butterworth 1 1 » “ ”2n–1/2 » “ ”2n–1/2 1+ ω 1+ tan(Ω/2) ωcf tan(Ωcf /2)

1 1 Chebyshev 1/2 1/2 h 2 2 “ ω ”i h 2 2 “ tan(Ω/2) ”i 1+ Cn 1+ Cn ωcf tan(Ωcf /2) Comparison of IIR and FIR filters

Characteristic IIR FIR Multiplications least most Coefficient quantifi- can be high very low cation sensitivity Overflow errors can be high very low Stability by design always no always Simulate analog fil- yes no ter Coefficient memory least most Design complexity moderate simple adapted from “Understanding digital ” 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