IIR Filters and Feedback
Feed part of the output back into the input and a filter's impulse response becomes infinitely long. Such IIR (infinite impulse response) filters achieve sharp responses with a handful of coefficients, where an FIR filter would need hundreds. The price: they can be unstable, and their phase is not linear. Most analog-inspired filters (Butterworth, Chebyshev, elliptic) and audio equalizers are IIR.
The difference equation
A general causal IIR filter is
The simplest is the one-pole low-pass (exponential smoothing), , with impulse response and frequency response .
import numpy as np
def lfilter(b, a, x): # direct implementation of the difference equation
y = np.zeros(len(x))
for n in range(len(x)):
acc = sum(b[k] * x[n - k] for k in range(len(b)) if n - k >= 0)
acc -= sum(a[k] * y[n - k] for k in range(1, len(a)) if n - k >= 0)
y[n] = acc / a[0]
return y
alpha = 0.9
impulse = np.zeros(10); impulse[0] = 1
h = lfilter([1 - alpha], [1, -alpha], impulse)
print("impulse response:", np.round(h, 4).tolist())
print("formula (1-a)a^n:", np.round((1 - alpha) * alpha ** np.arange(10), 4).tolist())impulse response: [0.1, 0.09, 0.081, 0.0729, 0.0656, 0.059, 0.0531, 0.0478, 0.043, 0.0387]
formula (1-a)a^n: [0.1, 0.09, 0.081, 0.0729, 0.0656, 0.059, 0.0531, 0.0478, 0.043, 0.0387]
Two coefficients produce an impulse response that never ends.
A notch filter with two poles and two zeros
To remove a tone at normalized frequency , put zeros on the unit circle at (exact cancellation) and poles just inside at (to restore the gain elsewhere). The closer is to 1, the narrower the notch (Poles, Zeros and Stability).
import numpy as np
fs, f0, r = 500.0, 50.0, 0.98
w0 = 2 * np.pi * f0 / fs
b = np.array([1, -2 * np.cos(w0), 1])
a = np.array([1, -2 * r * np.cos(w0), r * r])
def H(f):
z = np.exp(1j * 2 * np.pi * f / fs)
return np.polyval(b[::-1], 1 / z) / np.polyval(a[::-1], 1 / z)
for f in (10, 40, 48, 50, 52, 60, 150):
print(f"{f:3d} Hz: gain {abs(H(f)):.4f}")
t = np.arange(0, 2, 1 / fs)
x = np.sin(2 * np.pi * 7 * t) + np.sin(2 * np.pi * 50 * t)
y = np.zeros_like(x)
for n in range(len(x)):
y[n] = (b[0] * x[n] + (b[1] * x[n - 1] if n >= 1 else 0) + (b[2] * x[n - 2] if n >= 2 else 0)
- (a[1] * y[n - 1] if n >= 1 else 0) - (a[2] * y[n - 2] if n >= 2 else 0))
print("after the transient, max error vs the 7 Hz part:", round(float(np.max(np.abs(y[500:] - np.sin(2 * np.pi * 7 * t[500:])))), 3)) 10 Hz: gain 1.0192
40 Hz: gain 1.0073
48 Hz: gain 0.7952
50 Hz: gain 0.0000
52 Hz: gain 0.7952
60 Hz: gain 1.0073
150 Hz: gain 1.0202
after the transient, max error vs the 7 Hz part: 0.022
Five coefficients remove the 50 Hz tone while leaving 7 Hz nearly untouched; an FIR filter with a notch this narrow would need hundreds of taps.
Stability and numerical care
The one-pole filter is stable only for ; in general all poles must lie inside the unit circle. High-order IIR filters written as one big difference equation are numerically fragile (rounding moves the poles), so they are implemented as cascades of second-order sections ("biquads").
In software
scipy.signal.butter,cheby1,ellip,iirnotchdesign IIR filters; useoutput="sos"andsosfiltfor stability;filtfilt/sosfiltfiltrun the filter forward and backward for zero phase offline.- The "RBJ cookbook" biquad formulas power most audio equalizers and synthesizer filters.
Exercises
Exercise 1: Time constant
For at Hz, what gives a time constant of 0.5 s (the impulse response falls by in 0.5 s)?
Answer
.
Solution
0.5 s is 50 samples, and gives .
Exercise 2: Unstable feedback
What happens to when the input is a single impulse?
Answer
The output grows like without bound.
Solution
The impulse response is ; the pole at 1.1 lies outside the unit circle, so the filter is unstable.
Exercise 3: Exponential smoothing of a step (code)
Apply the one-pole low-pass with to a unit step and print the first 6 outputs.
Answer
, that is .
Solution
import numpy as np
a = 0.8
y, out = 0.0, []
for x in np.ones(6):
y = (1 - a) * x + a * y
out.append(round(float(y), 4))
print(out)[0.2, 0.36, 0.488, 0.5904, 0.6723, 0.7379]
Test yourself
IIR filters get their infinite impulse response from…
- long coefficient lists
- feedback of past outputs
- windowing
- zero padding
A causal IIR filter is stable when…
- all zeros are inside the unit circle
- all poles are inside the unit circle
- it has linear phase
Why are high-order IIR filters implemented as second-order sections?
- it is faster to type
- for numerical robustness: rounding errors move poles less
- to get linear phase
- it removes the feedback