Digital filter design

1 OF FILTERS

x(t) - G - y(t)

For input x(n) = cos(ωn) the output is

y(n) = AG(ω) cos(ωn + φG(ω))

AG(ω): filter gain or magnitude

φG(ω): filter phase

2 Reason: the output can always be written as

y(n) = h(0) cos(ωn) + h(1) cos(ωn − ω) + ··· h(k) cos(ωn − ωk) + ···

= AG(ω) cos(ωn + φG(ω))

The functions AG(ω), φG(ω) form the frequency response of a filter

Matlab routine: freqz(B,A,W) where B= [b0 a2 . . . bM ], B= [1 a1 a2 . . . aN ], W: vector with frequencies (if specified)

3 Relation between and frequency response

Let

x(n) = ejωn = cos(ωn) + j sin(ωn)

Then:

y(n) = h(0)ejωn + h(1)ejω(n−1) + ··· h(k)ejω(n−k) + ··· ( ) = h(0) + h(1)e−jω + ··· h(k)e−jωk + ··· ejωn = H(ejω) ejωn where H(z) is the transfer function of the system.

4 H(ejω) is the frequency response: determines how a frequency component ω is affected in magnitude and phase. H(ejω) is a complex number which can be written in polar form:

jω H(ejω) = |H(ejω)| ej arg(H(e ))

Then for input x(n) = ejωn = cos(ωn) + j sin(ωn) we have:

y(n) = H(ejω) ejωn jω = |H(ejω)| ej arg(H(e )) ejωn jω = |H(ejω)| ej(ωn+arg(H(e ))) ( ) = |H(ejω)| cos(ωn + arg(H(ejω))) + j sin(ωn + arg(H(ejω))) or

y(n) = AH(ω) cos(ωn + φH(ω)) + jAH(ω) sin(ωn + φH(ω)) where jω AH(ω) = |H(e )| (filter gain) and jω φH(ω) = arg(H(e )) (phase shift of filter)

5 FREQUENCY RESPONSE OF SERIES CONNECTION

y1(n) - - - x(n) H1 H2 y(n)

For x[n] = cos(ωn) we have:

y1(n) = A1(ω) cos(ωn + φ1(ω))

y(n) = A2(ω)A1(ω) cos(ωn + φ1(ω) + φ2(ω))

⇒ Magnitude of series connection is multiplicative:

A(ω) = A2(ω) · A1(ω)

Phase of series connection is additive:

φ(ω) = φ2(ω) + φ1(ω)

6

Example: Low-pass filter magnitude specs

1.2

1

0.8

0.6

0.4

0.2

0 0 0.1 0.2 0.3 0.4 0.5

- maximum deviation in passband, 1 − δp ≤ A(f) ≤ 1 + δp - damping in stopband, A(f) ≤ δs - width of transition band (normalized)

7 Filter magnitude specs often defined in dB (decibel) units:

- maximum deviation in passband: Ap = 20 log(1 + δp) (dB) Note: Ap ≈ 8.7δp for small δp

- damping in stopband: As = −20 log δs (dB)

8 FUNDAMENTAL RELATIONSHIP BETWEEN AMPLITUDE AND PHASE

∫ 1 π d log(A(ω′)) sin((ω′ + ω)/2) dω′ ≤ · φ(ω) ′ log ′ − ′ π 0 d log(ω ) sin((ω ω)/2) ω

′ sin((ω +ω)/2) where log sin((ω′−ω)/2) > 0

Implication:

d log(A(ω)) Low-pass filter with large magnitude slope, d log(ω) << 0 results in large negative phase shifts, φ(ω) << 0!

9 EFFECT OF PHASE SHIFT - FILTERS

Observation: Phase shift in passband may cause distortion.

Illustrative Example

Consider a signal consisting of two sinusoidal components

s(n) = s1(n) + s2(n) where

s1(n) = sin(ω1n)

s2(n) = sin(ω2n)

10 1

0.5 s 1 0

−0.5

−1

1

0.5 s 2 0

−0.5

−1

2

1 s 0

−1

−2

Top: s1(n) Middle: s2(n) Below: s(n)

11 Add a high-frequency disturbance e(n),

x(n) = s(n) + e(n) where e[n] = sin(ωen), ωe > ω1, ω2

2

1 s 0

−1

−2

1

0.5 e 0

−0.5

−1

2

x 0

−2

12 Use low-pass filtering to suppress the disturbance

1 1

0.5 0.5 s ,y s ,y 1 1 0 1 1 0

−0.5 −0.5

−1 −1

1 1

0.5 0.5 s ,y s ,y 2 2 0 2 2 0

−0.5 −0.5

−1 −1

2 2

1 1 s,y s,y 0 0

−1 −1

−2 −2

Left: Nonlinear phase filter gives passband signal distortion

Right: Linear phase filter reconstructs delayed version of passband signal

13 Linear phase property:

x(t) - G - y(t)

For input x(n) = cos(ωn) we have the output

y(n) = A(ω) cos(ωn + φ(ω))

To avoid phase distortion we require that all frequencies in passband delayed are by the same time, i.e.,

y(n) = A(ω) cos(ω(n − τ)) in passband.

This is achieved if the filter gives phase shift

φ(ω) = −ω · τ

14 PHASE SHOULD BE A LINEAR FUNCTION OF FREQUENCY IN PASSBAND!

Implication for frequency response:

jω We should have φH(ω) = arg(H(e )) = −ωτ or H(ejω) = |H(ejω)|e−jωτ in passband.

• Linear phase property can be achieved with symmetric FIR filters

• Linear property can be achieved only approximately with IIR filters

15 LINEAR PHASE FIR FILTERS Linear phase property is achieved by symmetric FIR filters:

y(n) = h(0)x(n) + h(1)x(n − 1) + ··· +h(1)x(n + 2 − N) + h(0)x(n + 1 − N), n = 0, 1, 2,...,M where h(k) = h(N − 1 − k), k = 0, 1,...,N − 1

For input x(n) = cos(ωn) the output is

y(n) = A(ω) cos(ωn − ω(N − 1)/2) = A(ω) cos(ω(n − (N − 1)/2))

⇒ Each frequency component delayed by (N − 1)/2 time steps.

16 REASON: Pairwise combination of terms k and N − 1 − k gives ( ) h(k) x(n − k) + x(n + 1 − N + k) = ( ) = h(k) cos(ω(n − k)) + cos(ω(n + 1 − N + k)) ( N−1 N−1 = h(k) cos(ω(n − ) + ω( − k)) 2 2 ) N−1 N−1 + cos(ω(n − ) − ω( − k)) 2 2 N−1 = h(k)2 cos(ω( − k)) · cos(ω(n − (N − 1)/2)) 2 where we have used cos(α ± β) = cos α · cos β ∓ sin α · sin β ⇒

All frequency components x(n) = cos(ωn) are delayed by (N −1)/2 samples

17 From above, we have (for N even)

y(n) = AH(ω) cos(ωn + φH(ω)) N/∑2−1 N−1 = h(k)2 cos(ω( − k)) · cos(ω(n − (N − 1)/2)) 2 k=0

⇒ Filter has frequency response with:

• Linear phase:

φH(ω) = −ω(N − 1)/2

• Gain: N/∑2−1 N−1 A (ω) = 2 h(k) cos(ω( − k)) H 2 k=0

Similarly if N is odd-valued (see lecture notes)

18 FIR FILTER DESIGN

We consider two important FIR filter design methods:

• Windowed-Sinc filters

• Optimization-based FIR filters

19 Windowed-Sinc filters

The idea is to modify the ideal filter. The ideal low-pass filter with bandwidth fc has

hD(0) = 2fc sin(2πf k) h (k) = c , k = ±1, ±2,... D πk

Cannot be implemented, because:

• impulse response hD(k) cannot be realized by any finite-dimensional difference equation

• not causal (output depends on future inputs)

20 Practical low-pass FIR filter:

• cut the ideal filter response to finite length N = 2M + 1 (hD(k), k = −M, −M + 1,...,M),

y(n) = hD(−M)x(n + M) + ··· + hD(−1)x(n + 1) +

+hD(0)x(n) + hD(1)x(n − 1) + ··· + hD(M)x(n − M)

• make it causal by introducing a delay M:

y(n) = hD(−M)x(n) + ··· + hD(−1)x(n − M + 1) +

+hD(0)x(n − M) + ··· + hD(M)x(n − 2M − 1)

• multiply impulse response coefficients by a w(k) in order to smooth out end effects:

y(n) = hw(−M)x(n) + ··· + hw(0)x(n − M) + ··· + hw(M)x(n − N),

where hw(k) = w(k)hD(k), N = 2M + 1.

21 Some common window functions:

• Rectangular window (trivial), w(k) = 1, |k| ≤ (N − 1)/2

• Hanning window, w(k) = 0.5 + 0.5 cos(2πk)/N), |k| ≤ (N − 1)/2

• Hamming window, w(k) = 0.54 + 0.46 cos(2πk/N), |k| ≤ (N − 1)/2

• Blackman window, w(k) = 0.42 + 0.5 cos(2πk/N) + 0.08 cos(4πk/N), |k| ≤ (N − 1)/2

22 Observe that:

• Length of windowed filter is N = 2M + 1 (odd)

• Windowed filters are linear phase, as they satisfy symmetry property: hD(−k) = hD(k) and w(−k) = w(k) ⇒ hw(−k) = hw(k)

Selection of window function is a trade-off between performance and complexity:

• Performance (deviation in passband and damping in stopband) is determined by the window function.

• Filter length N is determined by width ∆f of transition band: for each window function, ∆f = cw/N, where cw = window-specific constant.

• Better passband and stopband performance implies increased filter length for a given width of transtion band.

23 Summary of windowed filter properties:

Window Width of Maximal passband Damping in function transition band deviation stopband (Hz, normed) (dB) (dB)

Rectangular 0.9/N 0.7416 21 Hanning 3.1/N 0.0546 44 Hamming 3.3/N 0.0194 53 Blackman 5.5/N 0.0017 74 Kaiser 2.93/N (β = 4.54) 0.0274 50 4.32/N (β = 6.76) 0.00275 70 5.71/N (β = 8.96) 0.000275 90

24 IIR FILTERS

Good filter performance requires a sufficiently long memory in order to allow distinct response to different frequency components. In FIR filters this implies a large number of taps.

IIR filters have infinite memory due to recursiveness ⇒ simpler filter for given magnitude performance specifications

Standard IIR filters:

- Butterworth

- Chebyshev

- Elliptic

25 Butterworth filters

Digital low-pass Butterworth filter with cut-off frequency ωc = 2πfc has frequency response magnitude

[ ]1/2 | jω | 1 BN (e ) = 2N (1) 1 + [tan(ω/2)/ tan(ωc/2)]

- monotonically varying gain in both passband and stopband

Matlab routines:

[B,A]=butter(N,Wn,’ftype’) designs filter

[N,Wn]=buttord(Wp,Ws,Rp,Rs) computes required minimum order

26 Chebyshev filters

• Chebyshev filter type I has equiripple gain in passband and monotonically varying gain in stopband.

• Chebyshev filter type II has monotonically varying gain in passband and equiripple gain in stopband.

27 Elliptic filters

• Equiripple frequency response in both passband and stopband

• Achieve a given set of magnitude specifications with the lowest frequency order

• However, elliptic filters have less linear phase response the the passband than Butterworth or Chebyshev filters

28 IMPLEMENTATION OF IIR FILTERS

Recursive structure:

y(n) = a1y(n − 1) + aN y(n − N) =

= b0x(n) + b1x(n − 1) + ··· + bM x(n − M)

Makes IIR filters sensitive to round-off errors if implemented naively!

IIR FILTERS SHOULD ALWAYS BE IMPLEMENTED AS A CASCADE STRUCTURE CONSISTING OF A SERIES COUPLING OF SECOND ORDER SYSTEMS (SOS):

Y (z) = H(z)X(z) = HL(z) · HL−1(z) ··· H1(z)X(z)

29 or

Y1(z) = H1(z)X(z)

Y2(z) = H2(z)Y1(z) .

Y (z) = HL(z)YL−1(z)

where Hk(z) are second-order systems:

H1(z): y1(n) + a11y1(n − 1) + a12y1(n − 2) = b10x(n) + b11x(n − 1)

H2(z): y2(n) + a21y2(n − 1) + a22y2(n − 2) = b20y1(n) + b21y1(n − 1) .

Hk(z): yk(n) + ak1yk(n − 1) + ak2yk(n − 2) = bk0yk−1(n) + bk1yk−1(n − 1) .

HL(z): yL(n) + aL1yL(n − 1) + aL2yL(n − 2) = bL0yL−1(n) + bL1yL−1(n − 1)

30 FILTER DESIGN BY POLE AND ZERO PLACEMENT

Some special filter operations may be realized by specifying the poles (denominator zeros) and zeros of the filter transfer function H(z).

EXAMPLE: NOTCH FILTER

Suppose we want to eliminate the frequency component ω0 = 2πf0, while not affecting other frequencies too much.

It is straightforward to see that the frequency component ω0 is eliminated by the FIR filter

y(n) = x(n) − 2 cos(ω0)x(n − 1) + x(n − 2)

[because for x(n) = cos(ω0n + φ): ] cos(ω0n + φ) − 2 cos(ω0) cos(ω0(n − 1) + φ) + cos(ω0(n − 2) + φ) = 0

31 However, it has an overly broad stopband and suppresses other frequencies as well.

In order to design a simple filter with a narrow stopband at ω0, we can introduce the IIR filter (notch filter)

y(n) − 2r cos(ω )y(n − 1) + r2y(n − 2) = ( 0 ) = K x(n) − 2 cos(ω0)x(n − 1) + x(n − 2)

It can be shown that it has a stopband with 3dB width

1 − r B ≈ (Hz, normalized) π

It is the width of the frequency√ band in which the damping is > 3 dB, or the gain is < −3 dB ≈ 1/ 2 ≈ 0.707.

The constant K should be selected so that desired magnitude far from ω0 is achieved.

32 EXAMPLE

Construct a digital notch filter to eliminate a 50 Hz frequency component. The stopband should have 3 dB width 50 ± 5 Hz. The sampling frequency is 500 Hz.

Solution:

Normed notch frequency: f0 = 50/500 = 0.1 Normed stopband width: B = 10/500 = 0.02

Then r = 1 − Bπ = 1 − 0.02π = 0.937, cos(ω0) = cos(2πf0) = 0.8090 and we obtain the notch filter y(n) − 1.516y(n − 1) + 0.878y(n − 2) = x(n) − 1.618x(n − 1) + x(n − 2) with frequency response shown in the figure:

33 1.4

1.2

1

0.8

0.6

0.4

0.2

0 0 10 20 30 40 50 60 70 80 90 100 Frequency response magnitude of notch filter

34 High-pass filter from low-pass filter

- Spectral reversal

If y(n) = h0x(n) + h1x(n − 1) + ··· + hN x(n − N) is a low-pass filter with magnitude ALP (f), then changing the sign of odd-valued coefficients,

y(n) = h0x(n) − h1x(n − 1) + · · · − hN x(n − N)

gives a high-pass filter with symmetric magnitude, AHP (f) = ALP (1/2 − f)

35 - Spectral inversion

Ideal high-pass filter impulse response:

hD,HP (0) = 1 − hD(0)

hD,HP (k) = −hD(k), k = ±1, ±2,...

Windowed-sinc high-pass filter of length N is obtained from windowed-sinc low-pass filter:

hHP (M) = 1 − h(M)

hHP (k) = −h(k), k = 0, 1,...,M − 1,M + 1,...,N − 1

36 ARBITRARY FILTER RESPONSES

We can construct arbitrary filter responses by series couplings of the building block considered above.

Recall that a series coupling of two filters H1 and H2 has frequency response magnitude A(ω) = A2(ω) · A2(ω) This allows designing a desired magnitude response (band-pass, band-stop) by series coupling of low-pass and high-pass filters.

y1(n) - - - x(n) H1 H2 y(n)

37