IIR and FIR digital filters
An eight-sample average and an exponential with α = 0.22 reduce noise equally. The first needs eight samples in memory; the second, a single variable.
01Two families of digital filters
A digital filter is a difference equation that a processor computes at every sample. If it uses only inputs, its impulse response is finite (FIR); if it feeds back previous outputs, it is infinite (IIR). The choice between the two is the first design decision:
| FIR | IIR | |
|---|---|---|
| Stability | Always stable: it has no poles away from the origin | It has to be ensured: the poles inside the unit circle |
| Phase | Can have exactly linear phase | Nonlinear phase, like analog filters |
| Order for a steep transition | High: tens or hundreds of coefficients | Low: a few poles are enough |
| Cost per sample | One multiplication per coefficient | Few multiplications |
| Sensitivity to quantization | Low | High: rounding a coefficient moves the poles |
02Linear phase and group delay
The group delay \( \tau(\Omega) = -d\varphi/d\Omega \) says how many samples each frequency takes to pass through. If it is the same for all of them, the waveform comes out intact, just delayed. An FIR with symmetric coefficients has exactly linear phase:
That is why FIR filters dominate in digital communications, professional audio and instrumentation, where the shape of the pulses matters, and IIR filters where cost or latency matters: control, tone detection, low-power equalizers.
03The two simplest filters
The moving average needs to store \( M \) samples and, computed recursively, two additions per sample; the exponential needs a single variable and one multiplication. They are the filters in almost all measurement firmware.
At the top, the magnitude of the frequency response of the two filters from 0 to π. Below, their response to a step with white noise (the same noise sequence for both). You adjust the length of the average and the coefficient of the exponential.
04Structures and implementation
The same transfer function can be computed with different structures: direct form, transposed direct form, cascade of second-order sections (biquads) or lattice. Mathematically they are equivalent; numerically they are not. A high-order IIR in direct form is very sensitive to the rounding of its coefficients, and in practice it is always implemented as a cascade of biquads, just as active filters are built with second-order sections.
On a microcontroller without floating point, work is done in fixed point, with scaled integers. You have to watch out for overflow in the sums, for rounding, which adds noise, and for limit cycles: small oscillations that an IIR sustains through rounding even when the input is zero. Signal processors multiply and accumulate in one cycle, with wide accumulators to avoid overflow, as seen in processor architecture.
05In the lab
Read a noisy sensor with a microcontroller, apply a moving average and an exponential with the same noise reduction, and compare their response to an abrupt change.
Filter a square wave with a symmetric FIR and with an IIR of the same cutoff frequency, and compare the shape of the edges on the oscilloscope.
Implement the exponential in 16-bit fixed point and find the smallest α that still converges to the final value.
06Common mistakes
- Forgetting the filter delay. A 32-sample average delays by 15.5 samples: in a control loop it can destabilize it.
- Implementing a high-order IIR in direct form. The rounding of the coefficients makes it unstable.
- Using a very small α in fixed point. The product is rounded to zero and the output stalls short of the final value.
- Believing that the moving average is a good lowpass filter. Its sidelobes let quite a lot through; it is good against noise, not for separating frequencies.
07Self-assessment
How much does a symmetric 31-coefficient FIR delay at 48 kHz?
15 samples: 0.31 ms.
What α gives the same noise reduction as an 8-sample average?
\( \alpha/(2-\alpha) = 1/8 \Rightarrow \alpha = 2/9 \approx 0.22 \).
Where is the pole of the exponential with α = 0.1?
At \( z = 0.9 \): inside the circle, stable.
Why is an FIR always stable?
Because all its poles are at the origin: the output is a finite sum of bounded inputs.
08Further reading
- Richard G. Lyons. Understanding Digital Signal Processing. 3rd ed., Prentice Hall, 2010. FIR and IIR filters explained intuitively, including structures and fixed point.
- Alan V. Oppenheim and Ronald W. Schafer. Discrete-Time Signal Processing. 3rd ed., Pearson, 2010. The complete theory of structures and of the effects of quantization.
- Steven W. Smith. The Scientist and Engineer's Guide to Digital Signal Processing. California Technical Publishing, 1997. The moving-average chapter is the best analysis of why it is so useful against noise.