TExcelTAD
Home › Fourier Analysis & Signals › IIR Filters and Feedback

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

y[n]=∑k=0Mbk x[n−k]−∑k=1Nak y[n−k]y[n] = \sum_{k=0}^{M} b_k\, x[n-k] - \sum_{k=1}^{N} a_k\, y[n-k]

The simplest is the one-pole low-pass (exponential smoothing), y[n]=(1−a)x[n]+a y[n−1]y[n] = (1 - a)x[n] + a\,y[n-1], with impulse response (1−a)an(1 - a)a^n and frequency response H=(1−a)/(1−ae−iω)H = (1-a)/(1 - a e^{-i\omega}).

python · try it yourself
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 ω0\omega_0, put zeros on the unit circle at e±iω0e^{\pm i\omega_0} (exact cancellation) and poles just inside at re±iω0r e^{\pm i\omega_0} (to restore the gain elsewhere). The closer rr is to 1, the narrower the notch (Poles, Zeros and Stability).

python · try it yourself
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 ∣a∣<1\lvert a \rvert < 1; 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, iirnotch design IIR filters; use output="sos" and sosfilt for stability; filtfilt/sosfiltfilt run 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 y[n]=(1−a)x[n]+a y[n−1]y[n] = (1 - a)x[n] + a\,y[n-1] at fs=100f_s = 100 Hz, what aa gives a time constant of 0.5 s (the impulse response falls by 1/e1/e in 0.5 s)?

Answer

a=e−1/50≈0.9802a = e^{-1/50} \approx 0.9802.

Solution

0.5 s is 50 samples, and a50=e−1a^{50} = e^{-1} gives a=e−0.02≈0.9802a = e^{-0.02} \approx 0.9802.

Exercise 2: Unstable feedback

What happens to y[n]=x[n]+1.1 y[n−1]y[n] = x[n] + 1.1\,y[n-1] when the input is a single impulse?

Answer

The output grows like 1.1n1.1^n without bound.

Solution

The impulse response is 1.1nu[n]1.1^n u[n]; 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 a=0.8a = 0.8 to a unit step and print the first 6 outputs.

Answer

0.2,0.36,0.488,0.5904,0.6723,0.73790.2, 0.36, 0.488, 0.5904, 0.6723, 0.7379, that is 1−0.8n+11 - 0.8^{n+1}.

Solution
python · try it yourself
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…

  1. long coefficient lists
  2. feedback of past outputs
  3. windowing
  4. zero padding

A causal IIR filter is stable when…

  1. all zeros are inside the unit circle
  2. all poles are inside the unit circle
  3. it has linear phase
  4. b0=1b_0 = 1

Why are high-order IIR filters implemented as second-order sections?

  1. it is faster to type
  2. for numerical robustness: rounding errors move poles less
  3. to get linear phase
  4. it removes the feedback