---
title: IIR filters
heading: Filters that feed their own output back, the one-pole smoother with its exponential memory, time constant and corner frequency, why a few feedback coefficients do the work of hundreds of FIR taps, the biquad section, designing from analog prototypes with the bilinear transform, and the stability and phase costs
eyebrow: D5 · Lesson 5 · IIR filters
xp: 40
---

::: hook
An FIR filter remembers only its last N inputs. Let a filter also remember its own past outputs and it can remember forever: an impulse sets off a response that decays but never quite ends, so it's called **infinite impulse response**. The simplest one, "new output = 0.9 × old output + 0.1 × new input", is a digital RC low-pass in one line of code, and it's in every sensor-smoothing routine ever written. Feedback makes IIR filters extraordinarily efficient, but it also brings the analog world's problems back: phase distortion and the possibility of instability.
:::

::: play
- The frequency response of the one-pole smoother $y_n = ay_{n-1} + (1 - a)x_n$ at a sampling rate of 1 kHz: $H(f) = \frac{1 - a}{1 - ae^{-j2\pi f/f_s}}$.
- As a approaches 1 the filter remembers longer and its corner moves down. Its DC gain is always 1.
- It behaves like an RC low-pass with a time constant of $-1/\ln a$ samples, until the response flattens near the Nyquist frequency.
:::

```sim plot
view: graph
x: f = 1..500 log
y: H = -40..3
slider: a = 0.5..0.995 step 0.005 = 0.9
plot: 20*log(abs((1 - a)/(1 - a*exp(-j*2*pi*f/1000)))) | samples: 500 | label: one-pole response, dB (f in Hz)
marker: x = 1000/(2*pi)*acos(1 - (1 - a)^2/(2*a)) | label: −3 dB
readout: time constant, samples = -1/ln(a) | digits: 4
readout: −3 dB frequency, Hz = 1000/(2*pi)*acos(1 - (1 - a)^2/(2*a)) | digits: 4
```

::: predict
## A sensor reading is smoothed by $y_n = 0.9y_{n-1} + 0.1x_n$ at 1000 samples per second. After a sudden step in the reading, about how long does the output take to cover 63% of the step?

- [ ] 0.1 ms
- [ ] 1 ms
- [x] About 9.5 ms
- [ ] About 90 ms

::: reveal
After n samples the remaining error is $0.9^n$ of the step, which falls to 1/e when n = −1/ln 0.9 = 9.49 samples, 9.5 ms at 1 kHz. The handy approximation $1/(1 - a)$ = 10 samples is close when a is near 1. Reaching 99% takes ln 0.01/ln 0.9 = 44 samples.
:::
:::

::: nameit
## The IIR filter

$$y_n = \sum_{k=0}^{M}b_kx_{n-k} - \sum_{k=1}^{N}a_ky_{n-k}, \qquad H(z) = \frac{\sum b_kz^{-k}}{1 + \sum a_kz^{-k}}$$

The feedback terms give the transfer function poles (lesson 6). A filter is stable only if every pole lies inside the unit circle; quantizing the coefficients can push a pole outside.

## The one-pole smoother

$y_n = ay_{n-1} + (1 - a)x_n$ has DC gain 1, a time constant of $-1/\ln a$ samples, and a −3 dB frequency where

$$\cos\frac{2\pi f}{f_s} = 1 - \frac{(1 - a)^2}{2a}$$

To get a time constant of τ samples, use $a = e^{-1/\tau}$.

## Efficiency

A fourth-order IIR elliptic filter can match the sharpness of a 100-tap FIR filter with about 10 multiplications per sample. In practice, high-order IIR filters are built as cascades of second-order **biquad** sections, each $b_0 + b_1z^{-1} + b_2z^{-2}$ over $1 + a_1z^{-1} + a_2z^{-2}$, because one big high-order polynomial is too sensitive to coefficient rounding.

## Designing from analog prototypes

Butterworth, Chebyshev and elliptic designs (C4) carry over through the **bilinear transform** $s = \frac{2}{T}\frac{1 - z^{-1}}{1 + z^{-1}}$, which maps the whole analog frequency axis onto the digital one up to the Nyquist frequency. That squeezes frequencies, so the analog prototype is designed at the **prewarped** frequency

$$f_a = \frac{f_s}{\pi}\tan\frac{\pi f_d}{f_s}$$

for each critical digital frequency $f_d$.

## The cost

IIR filters can't have exactly linear phase, so they distort waveform shapes, and their feedback makes them sensitive to rounding (lesson 12).
:::

::: example
## For a = 0.9 at 1 kHz, where's the −3 dB point? What a gives a 100-sample time constant?

::: solution
1. **Cosine of the corner.** Put $a = 0.9$ in $\cos\omega = 1 - (1 - a)^2/(2a)$:
   $$\begin{aligned} \cos\omega &= 1 - \frac{0.01}{1.8} \\ &= 0.99444 \end{aligned}$$
2. **Corner angle.**
   $$\omega = 0.1055\ \text{rad/sample}$$
3. **Corner in hertz.**
   $$\begin{aligned} f &= \frac{0.1055 \times 1000}{2\pi}\ \text{Hz} \\ &= 16.8\ \text{Hz} \end{aligned}$$
4. **Pole for τ = 100 samples.**
   $$\begin{aligned} a &= e^{-1/\tau} \\ &= e^{-0.01} \\ &= 0.99005 \end{aligned}$$

**Answer:** the −3 dB point is at 16.8 Hz, and a = 0.99005 gives a 100-sample time constant.
:::
:::

::: example
## A digital low-pass at 8 kHz needs its corner at 1 kHz. At what frequency should the analog prototype be designed?

::: solution
1. **Prewarp the corner.**
   $$\begin{aligned} f_a &= \frac{8000}{\pi}\tan\frac{\pi}{8} \\ &= 2546.5 \times 0.4142 \\ &= 1054.8\ \text{Hz} \end{aligned}$$
2. **Check.** The bilinear transform's squeeze then lands the corner exactly at 1 kHz.

**Answer:** design the analog prototype with its corner at 1054.8 Hz.
:::
:::

::: example
## A one-pole smoother at 1 kHz should have a 10 ms time constant. What pole a does it need?

::: solution
1. **Pole.** Each sample should keep this fraction of the previous output:
   $$\begin{aligned} a &= e^{-T/\tau} \\ &= e^{-1/10} \\ &= 0.905 \end{aligned}$$
2. **Difference equation.** The input gets the rest, 0.095:
   $$y_n = 0.905\,y_{n-1} + 0.095\,x_n$$
3. **Analog equivalent.** It's the digital equivalent of an RC filter with RC = 10 ms.

**Answer:** a = 0.905, so $y_n = 0.905\,y_{n-1} + 0.095\,x_n$.
:::
:::

::: check
given: a = 0.8 | 0.9 | 0.95; s = 1 | 10
answer: -1/ln(a)/s
unit: ms
tolerance: 0.5%

## A one-pole smoother with a = {a} runs at {s} kHz. What's its time constant?

::: right
Right. $-1/\ln a$ samples = {answer} ms.
:::

::: misses
- [1/(1 - a)/s] That's the approximation; use $-1/\ln a$.
- [any] Not yet. Solve $a^n = 1/e$.
:::
:::

::: check
given: t = 2 | 5 | 10 | 20
answer: exp(-1/t)
tolerance: 0.05%

## What coefficient a gives a one-pole smoother a time constant of {t} samples?

::: right
Right. $e^{-1/\tau}$ = {answer}.
:::

::: misses
- [any] Not yet. Invert the time constant.
:::
:::

::: check
given: a = 0.8 | 0.9 | 0.98; s = 1 | 8 | 48
answer: s*1000/(2*pi)*acos(1 - (1 - a)^2/(2*a))
unit: Hz
tolerance: 0.5%

## Where is the −3 dB point of a one-pole smoother with a = {a} at {s} kHz?

::: right
Right. {answer} Hz.
:::

::: misses
- [any] Not yet. Solve $|H|^2 = 1/2$ for the frequency.
:::
:::

::: check
given: d = 3 | 6 | 10; s = 32 | 44.1 | 48
answer: s/pi*tan(pi*d/s)
unit: kHz
tolerance: 0.5%

## A digital filter at {s} kHz needs a critical frequency of {d} kHz. At what frequency should its analog prototype be designed for the bilinear transform?

::: right
Right. $(f_s/\pi)\tan(\pi f_d/f_s)$ = {answer} kHz.
:::

::: misses
- [d] Prewarp it: the bilinear transform compresses frequencies.
- [any] Not yet. The prewarping formula.
:::
:::

::: check
kind: parts
given: t = 5 | 10 | 50; fs = 1 | 8

## A one-pole smoother at {fs} kHz should have a {t} ms time constant.

::: part
answer: exp(-1/(t*fs))
tolerance: 0.5%

### What pole a?

::: misses
- [any] $e^{-T/\tau}$.
:::
:::

::: part
answer: 1 - exp(-1/(t*fs))
tolerance: 0.5%

### What input weight $1 - a$?

::: misses
- [any] So the DC gain is 1.
:::
:::
:::

::: check
kind: parts
given: fc = 0.5 | 1 | 3; fs = 8 | 16 | 48

## A bilinear-transform low-pass at {fs} kHz needs a digital corner at {fc} kHz.

::: part
answer: fs/pi*sin(pi*fc/fs)/cos(pi*fc/fs)
unit: kHz
tolerance: 0.5%

### Where should the analog prototype's corner be (prewarped)?

::: misses
- [any] $(f_s/\pi)\tan(\pi f_c/f_s)$.
:::
:::

::: part
answer: (fs/pi*sin(pi*fc/fs)/cos(pi*fc/fs) - fc)/fc*100
unit: %
tolerance: 1%

### How far above the target is that, in percent?

::: misses
- [any] (prewarped − target)/target.
:::
:::
:::

::: explain
## How can a few feedback coefficients replace hundreds of FIR taps?

::: model
An FIR filter can only shape its output from the inputs within its window, so a sharp transition or a long memory needs a long window and many multiplications. An IIR filter feeds its own past outputs back in, so each output already summarizes the whole history of the input, weighted by an exponentially decaying memory. That recursion lets a handful of coefficients produce resonant peaks and steep slopes, poles in the transfer function, that an FIR filter can only approximate with hundreds of zeros. The price is that the phase response can't be exactly linear and the feedback can turn unstable if a pole is pushed outside the unit circle.
:::
:::

::: practice

::: check
given: t = 1.5 | 2 | 5 | 10 | 20 | 30 | 100
answer: exp(-1/t)
tolerance: 0.05%

## a for τ = {t} samples?

::: misses
- [any] $e^{-1/\tau}$.
:::
:::

::: check
given: d = 0.1 | 15; s = 32 | 96
answer: s/pi*tan(pi*d/s)
unit: kHz
tolerance: 0.5%

## Prewarped frequency for {d} kHz at {s} kHz?

::: misses
- [any] $(f_s/\pi)\tan(\pi f_d/f_s)$.
:::
:::
:::
