FIR vs IIR Filters for Real-Time Sensor Data: A Practical Comparison

FIR vs IIR Filters for Real-Time Sensor Data: A Practical Comparison

You replace a three-line moving average with a properly designed filter, the trace on the scope cleans up, and then the machine starts hunting. Or the step response arrives beautifully smooth and about twenty milliseconds late. That is the moment the choice between a finite impulse response (FIR) filter and an infinite impulse response (IIR) filter stops being a textbook exercise. Both are linear, both are just multiply-accumulate arithmetic, and they behave very differently once latency, memory and fixed-point maths enter the room.

Two structures, two philosophies

A FIR filter computes each output as a weighted sum of the current and previous inputs: y[n] = b0·x[n] + b1·x[n−1] + … + bN−1·x[n−N+1]. Nothing feeds back, so the impulse response lasts exactly N samples and then stops. Any coefficient set you can type in is stable by construction.

An IIR filter adds feedback, so each output also depends on previous outputs. The impulse response never truly ends; it decays. That feedback is what buys the efficiency. A sixth-order elliptic low-pass can be built from three second-order sections — roughly fifteen multiplications per sample — and still achieve a transition band that a three-hundred-tap FIR would struggle to match.

Latency: the group delay you cannot design away

If a FIR filter's coefficients are symmetric, its phase response is exactly linear and every frequency is delayed by the same amount: (N−1)/2 samples. That is not a rounding error, it is a law. A 201-tap filter running at 10 kHz adds 10 ms to every measurement, and a 101-tap filter at 1 kHz adds 50 ms. For slow temperature or pressure logging, nobody notices. For a current loop or a motor controller, that delay is phase margin evaporating.

IIR filters look better here, and mostly are — but not uniformly. Group delay in the middle of the passband might be only a sample or two, then climb steeply as you approach the cutoff. If your signal of interest sits just below a low cutoff frequency, an IIR filter can be slower than you expected.

You can shorten FIR latency by using fewer taps, or by switching to a minimum-phase design. That abandons linear phase in exchange for a lower delay, which is a good trade in control loops and a poor one in measurement.

Phase distortion, and when it matters

Linear phase preserves waveform shape: a pulse goes in, a delayed pulse of the same shape comes out. Peak detection, event timing, edge location, cross-correlation between two sensors, and anything that compares a filtered channel against a threshold all benefit from that.

IIR phase is non-linear. A pulse emerges asymmetric and slightly smeared, and sharp designs — Chebyshev, elliptic — ring after a step. If your metric is a magnitude rather than a shape, none of this matters. RMS vibration level, broadband noise power, a slow temperature trend: take the efficiency and move on.

One offline trick is worth knowing. Running a filter forwards and then backwards over stored data cancels the phase entirely, at the cost of doubling the effective order and squaring the magnitude response. It is not available in real time, because it needs the future.

Memory, arithmetic and the serial problem

FIR costs scale with tap count. Two hundred and fifty-six taps need 256 coefficients and a delay line of 255 samples; at 32-bit floats that is roughly 2 kB of RAM and 256 multiply-accumulates per output. On a small microcontroller, that is real money.

IIR keeps only a handful of state variables per second-order section and processes a sample in a few dozen cycles. The catch is recursion: each output depends on the previous one, so you cannot pipeline or vectorise an IIR section the way you can a FIR tapped delay line. On a DSP with a hardware MAC unit, a long FIR often runs faster than its tap count suggests, while a short IIR cannot use those resources at all. Count cycles on the target, not on paper.

Two shortcuts repay the effort. A boxcar or moving average is a FIR with all coefficients equal, and it can be implemented recursively with one accumulator and one subtraction — watch the accumulating rounding error and reset the accumulator periodically. And if you need a cutoff far below the sample rate, decimate first with a cheap CIC or half-band stage, then filter gently at the lower rate. That is usually far cheaper than one heroic filter running flat out.

Numerical stability in fixed point

FIR filters cannot go unstable. Round-off noise accumulates but stays bounded, and a sensibly scaled implementation behaves predictably for years.

IIR filters derive their poles from the coefficients, and small coefficient errors move those poles. On a 16-bit fixed-point target, a narrow low-pass at a very low cutoff pushes poles so close to z = 1 that quantisation can shove them outside the unit circle. The filter then rings forever or blows up. A few habits keep you out of trouble:

  • Implement IIR filters as cascaded second-order sections, never as one high-order polynomial. High-order coefficients are numerically hostile.
  • Use transposed direct form II, and keep coefficient word lengths generous at low cutoffs and high Q.
  • Scale each section so intermediate values cannot overflow, and enable saturating arithmetic on the accumulator.
  • Prototype in double precision, then compare the quantised build against it with a step and a sine sweep. If they diverge, you need more coefficient bits or a different section ordering.
  • Watch for denormal values slowing down floating-point MCUs, and for limit cycles where a fixed-point filter settles into a small persistent tone instead of silence.

A short decision checklist

  1. Does waveform shape or absolute timing matter? Choose a linear-phase FIR.
  2. Do you only need amplitude in a band, on a tight budget? Choose IIR.
  3. Is latency critical in a closed loop? Use a minimum-phase FIR or a low-order IIR, and budget the delay explicitly in the loop design.
  4. Is the cutoff extremely low relative to the sample rate? Decimate, then filter.
  5. Is the target 16-bit fixed point? Cascade sections, and test the quantised build, not the float model.
  6. Do you need the sharpest transition for a given order? Elliptic IIR, and accept the ringing.

Prove it on the bench

Whatever you pick, do three things before you trust the numbers. Apply a step and measure rise time, settling and overshoot. Sweep a sine through the band and log gain and group delay, not just the −3 dB point. Run the filter for a few million samples with a constant input and check the output stays where theory says it should.

Then write the numbers down next to the filter definition: tap count or section order, sample rate, group delay in milliseconds, cycles per sample. A filter is part of the measurement chain, and if you cannot say what it does to a step, you do not yet know what it does to your data.

Photo: Ivan Chumak / Pexels