Skip to content

Choosing FIR or IIR

Compare FIR and IIR filters for one spec by cost, delay and phase, run a filter forwards and backwards, and choose with a checklist.

Before this19.3 · 20.2 · 7 more
Chapter 20 · Lesson 6 of 6

First, the picture

One spec can be met by an FIR filter or by an IIR filter. Below, both are drawn against the same spec, and then their delays. Watch the delay panel: one line stays flat, and the other climbs at the band edge.

One spec, two filters

The running spec: 19.3's 26-tap equiripple FIR and an elliptic IIR of order 5.

The running spec as zones: two filters will meet it.

multiplies per sample
not yet
delay at 250 Hz
not yet
delay at 1000 Hz
not yet
0.00 / 14.00 s
Describe this picture

Two stacked panels for the running spec, 19.3’s 26-tap equiripple FIR and an elliptic IIR of order 5. The first is drawn like the spec drawings of 18.1: the gain from −80 to 5 dB against frequency from 0 to 4000 Hz, with the running spec’s zones. The second shows the group delay τg\tau_g of 12.4 across the pass band, from 0 to 25 samples against frequency from 0 to 1000 Hz. In both panels the FIR filter is the dashed curve and the IIR filter the solid curve. The readouts are the multiplies per sample and the delay at 250 Hz and at 1000 Hz, each in samples to one decimal, in the form “FIR 12.5, IIR 3.6”. Before a filter is drawn, a readout shows “not yet”.

The clip lasts 14 s. At the start only the zones are drawn. Then the FIR’s curves draw and hold: 26 taps, it passes, and it delays every frequency by the same 12.5 samples, 1.6 ms. Then the IIR draws. It also passes, with 11 multiplies a sample; its delay is 3.6 samples at 250 Hz but 23.1 at the band edge. After the clip a dotted cursor named “Frequency” appears on the delay panel and moves from 0 to 1000 Hz in steps of 10 Hz. The arrow keys move it by 10 Hz, Page Up and Page Down by 100 Hz, and Home and End jump to the two ends. While it moves, the two delay readouts become one, the delay at the cursor, and the caption reads like “750 Hz: FIR 12.5 samples, IIR 6.7.”; at 1000 Hz it is the end caption again. The cursor’s place is kept in the link, as two.f.

One spec, two filters

You now know two ways to build a low-pass filter. Chapter 19 built FIR filters, with a finite list of taps. Chapter 20 built IIR filters, which feed their own outputs back. Both can meet the same spec, so which one should you use? This page puts the two side by side, on the spec you know best.

That is the running spec of Filter specifications (18.1). The sample rate is 8 kHz. From 0 to 1 kHz the gain must stay within 1±0.051\pm0.05, and from 1.5 kHz to 4 kHz it must stay at or below 0.01, which is 40 dB down.

The FIR candidate is the shortest design of Optimal FIR design (19.3): the equiripple filter with 26 taps and stop-band weight 5. Its pass band strays from 0.9565 to 1.0436, and its stop band reaches −41.08 dB.

The IIR candidate is the elliptic filter of Analog prototype filters (20.1), turned digital by IIR design by the bilinear transform (20.2). Analog frequency is ω (rad/s), digital frequency is Ω (rad/sample); Oppenheim and Schafer use the opposite letters. An IIR filter’s gain peaks at 1, so its pass band is written as a loss: at most −20log⁡100.95=0.4455-20\log_{10}0.95=0.4455 dB. SciPy’s ellipord asks for order 5, and ellip(5, 0.4455, 40, 1000, fs=8000) designs it.

How do we compare their cost? Each output sample takes a fixed number of multiplications, so I count multiplies per sample. The FIR filter computes y[n]=∑k=025h[k] x[n−k]y[n]=\sum_{k=0}^{25}h[k]\,x[n-k], so it needs 26.

Its taps are symmetric, h[k]=h[25−k]h[k]=h[25-k] (17.3). So you can add each pair of inputs first and multiply once:

y[n]=∑k=012h[k] (x[n−k]+x[n−25+k]).\begin{aligned} y[n]&=\sum_{k=0}^{12}h[k]\,\big(x[n-k]\\ &\qquad+x[n-25+k]\big). \end{aligned}

That is 13 multiplies.

The IIR filter is a difference equation, as in Difference equations (6.1). With bkb_k the numbers that multiply inputs and aka_k the numbers that multiply past outputs,

y[n]=∑k=05bk x[n−k]−∑k=15ak y[n−k].\begin{aligned} y[n]&=\sum_{k=0}^{5}b_k\,{x[n-k]}\\ &\quad-\sum_{k=1}^{5}a_k\,{y[n-k]}. \end{aligned}

There are six bkb_k and five aka_k (a0=1a_0=1 needs no multiply), so 11 multiplies. Computed this way, straight from the difference equation, the filter is in what Filter structures (21.1) calls direct form.

So the IIR filter costs 11 multiplies, against 26 for the FIR, or 13 if you use the symmetry. That is less than half. What does it cost in other ways? The picture at the top of the page draws both filters against the running spec, and then their delays.

After the clip, a dotted cursor on the delay panel reads both delays; drag it, or use the arrow keys. Move it from 0 Hz to the band edge. The dashed FIR line never moves from 12.5. The solid IIR curve starts at 3.7 samples, dips a little, passes 4.2 at 500 Hz and 9.4 at 900 Hz, then climbs fast: 14.0 at 950 Hz and 23.1 at 1000 Hz.

Notice the band edge. There the IIR filter is the slower of the two, by more than 10 samples. Below about 940 Hz the solid curve is the lower one, and above it the dashed line is.

Think of two ways to drive across a city. The short cut through town is faster on average, but its time depends on the traffic. The motorway always takes the same time. The FIR filter is the motorway: every frequency arrives 12.5 samples late. The IIR filter is the short cut.

Why does the delay climb at the edge? In 20.1 you saw that a steep edge bends the delay near the cutoff. The elliptic filter has the steepest edge for its order, and it buys it with a pole pair at radius 0.9527, close to the unit circle. Their angle, ±45.64°, is the frequency 1014 Hz, just past the pass-band edge.

What does a changing delay do to a signal? Linear-phase systems (17.3) showed it: a bent phase delays the slow parts of a pulse by one amount and the fast parts by another, so the pulse comes apart. A straight phase only moves it.

So the IIR filter is cheaper and, at low frequencies, quicker, but it changes the shape of a waveform. The FIR filter keeps the shape and is late by the same amount everywhere.

Run it forwards, then backwards

Is there a way to have the IIR filter’s low cost and still keep the shape? If the whole signal is already stored, yes. Run the filter over the signal, then run it again over the result, from the last sample back to the first.

The backwards run is the reversal of Shifting, reversing and scaling time (2.1). I reverse the signal, filter it as usual, and reverse the output.

Call the first output v[n]v[n], so V(ejΩ)=H(ejΩ) X(ejΩ)V(e^{j\Omega})=H(e^{j\Omega})\,X(e^{j\Omega}). Reversing vv gives V(e−jΩ)V(e^{-j\Omega}), as in Properties of the DTFT (12.3). Filtering multiplies by H(ejΩ)H(e^{j\Omega}), and the last reversal replaces Ω\Omega by −Ω-\Omega again:

Y(ejΩ)=H(e−jΩ) V(ejΩ)=H(e−jΩ) H(ejΩ) X(ejΩ).\begin{aligned} Y(e^{j\Omega})&=H(e^{-j\Omega})\,V(e^{j\Omega})\\ &=H(e^{-j\Omega})\,H(e^{j\Omega})\,X(e^{j\Omega}). \end{aligned}

The impulse response h[n]h[n] is real, so by 12.3, H(e−jΩ)=H∗(ejΩ)H(e^{-j\Omega})=H^*(e^{j\Omega}). A number times its conjugate is its size squared:

Y(ejΩ)=∣H(ejΩ)∣2 X(ejΩ).Y(e^{j\Omega})=\big\lvert H(e^{j\Omega})\big\rvert^2\,X(e^{j\Omega}).

The gain ∣H∣2\lvert H\rvert^2 is real and never negative, so it adds no phase at all. I call this forward–backward filtering, or zero-phase filtering. Every frequency is delayed by zero samples, and nothing changes shape through the phase. The price is that the gain is squared: the pass band now lies between 0.952=0.90250.95^2=0.9025 and 1, and the stop band at 0.0120.01^2, 80 dB down.

In SciPy this is scipy.signal.filtfilt(b, a, x). The instrument below does it by hand on a pulse, with the elliptic filter of the first instrument. It marks each output’s centre with a dot; by centre I mean 17.3’s balance point, ∑nn y[n]/∑ny[n]\sum_n n\,y[n]\big/\sum_n y[n]. Watch where the dot sits after each pass.

Run it forwards, then backwards

A 24-sample pulse through the elliptic filter of clip 1: forwards, then the result backwards.

A 24-sample pulse, centred at n = 199.5.

centre moved by
not yet
stop band
not yet
0.00 / 13.00 s
Describe this picture

Two stacked panels for a 24-sample pulse through the elliptic filter of the first instrument: forwards, then the result backwards. They share the sample axis, nn from 170 to 240, and a vertical axis from −0.2 to 1.25. The first, “forwards”, shows the input as a thin dashed outline and the forward output as a solid curve. The second, “forwards, then backwards”, shows the same input outline and the final output as a solid curve. Each output has a dot at its centre. The readouts are how far the centre moved, in samples to two decimals, and the stop band, in dB to one decimal. There is no control.

The clip lasts 13 s. At the start only the input is drawn, a 24-sample pulse centred at n=199.5n=199.5. Then the forward output builds from the first sample to the last. When it holds, the pulse arrives 3.72 samples late, overshoots to 1.154 and rings only after its edges, and the stop band reads −40.0 dB. Next the forward output is filtered again from the end backwards, and the second output builds from the last sample to the first. At the end it is centred on the input (moved 0.00), symmetric, ringing equally before and after, and the stop band is now −80.0 dB; the caption adds that this is only possible when the whole signal is stored.

Look at the first panel first. The forward output is the pulse with its corners rounded and a ripple after each edge. Before n=188n=188 it is exactly zero: a filter that runs in real time cannot react to an edge before the edge arrives.

The centre moved by 3.72 samples, and that is no accident. The centre of a filter’s output moves by the centre of its impulse response, ∑nn h[n]/∑nh[n]\sum_n n\,h[n]\big/\sum_n h[n], and for a real hh that equals the group delay at 0 Hz, 3.717 samples.

Now the second panel. The output is symmetric about n=199.5n=199.5, the centre of the input. Its ripples dip to −0.047 before the pulse and again after it, and its peak, 1.065, is lower than the forward peak.

That ripple before the pulse is the thing to notice. No real-time filter can produce it, because it is a reaction to an edge that has not arrived yet. The backward pass saw the future, because the whole signal was stored.

Think of combing tangled hair. Comb only from the left, and the hair ends up pushed to the right. Comb from the left and then from the right, and no side is favoured.

So forward–backward filtering suits recordings: a stored ECG, offline audio, a column of data in a data-science notebook. It never suits a live stream, because the backward pass needs samples that have not arrived yet.

FIR or IIR: a checklist

Here is how I decide. Each row names a need, the filter it points to, and the reason.

Your task needsChooseWhy
linear phase, or a waveform’s exact shapeFIR, or IIR forwards and backwards if the data are storeda constant delay keeps the shape (17.3)
a tight delay budget at low frequencies, a steep edge, little computationIIRfewer multiplies and a short delay where most of the signal lies
a filter that adapts or is redesigned oftenFIRalways stable and simple to update (Adaptive filters: LMS, 26.3)
fixed point with few bitsFIR, or IIR as second-order sections, with carerounded IIR coefficients can move poles (Second-order sections, 21.2; Finite word-length effects, 21.3)
a very long or very narrow FIR filterFFT convolution or multirateFast convolution (14.3); Downsampling and decimation (22.1)

For the running spec, in one line each: the FIR filter costs 26 multiplies and delays every frequency by 12.5 samples. The IIR filter, order 5, costs 11 multiplies and delays from 3.5 to 23.1 samples across the pass band.

A word on stability. An FIR filter has no feedback, so it cannot become unstable. The IIR filter is stable because all its poles are inside the unit circle, as Stability and causality (16.4) showed. The largest radius is 0.9527. Rounding the coefficients in fixed point moves the poles, and a pole that close to the circle has little room. 21.2 and 21.3 show how to keep it inside.

One more choice sits between the two columns. A minimum-phase FIR filter, as in Minimum phase (17.2), has a shorter delay than a linear-phase one, but it gives up the constant delay.

Worked example

Let’s write out both designs for the running spec and check the numbers of this page.

1. The elliptic design. ellip(5, 0.4455, 40, 1000, fs=8000) gives these coefficients, to six decimals:

  • bkb_k: 0.022446, −0.012274, 0.021538, 0.021538, −0.012274, 0.022446
  • aka_k: 1, −3.317488, 5.080257, −4.269604, 1.962663, −0.392408

Its five poles are 0.6659, 0.8058∠±35.05°0.8058\angle\pm35.05° and 0.9527∠±45.64°0.9527\angle\pm45.64°. All five lie inside the unit circle, so the filter is stable. Its five zeros lie on the circle, at ±57.44°\pm57.44°, ±76.40°\pm76.40° and at −1-1. As frequencies, those zeros are 1276 Hz, 1698 Hz and 4000 Hz, where the gain is exactly zero.

2. Other IIR families on the running spec. SciPy’s order functions give Butterworth 12, Chebyshev I 7 and Chebyshev II 7. Their delays in the pass band differ a lot:

Family and orderDelay at 0 HzLargest delay in the pass band
Butterworth, 128.4 samples17.7 samples
Chebyshev I, 77.5 samples28.7 samples
Chebyshev II, 73.1 samples10.3 samples
elliptic, 53.7 samples23.1 samples

The Chebyshev II filter is interesting: its delay never reaches 12.5 samples in the pass band, so it is quicker than the FIR filter at every frequency there. It costs 15 multiplies in direct form, against the elliptic filter’s 11.

3. Cost at 8 kHz. The FIR filter needs 26×8000=208 00026\times8000=208\,000 multiplies a second, or 104 000 with the symmetry. The IIR filter needs 11×8000=88 00011\times8000=88\,000.

4. Delay in time. At 8 kHz one sample is 0.125 ms. The FIR filter’s 12.5 samples are 1.5625 ms. The IIR filter’s 3.717 samples at 0 Hz are 0.46 ms, and its 23.14 samples at 1000 Hz are 2.89 ms.

5. Forwards and backwards. The gain is squared. The pass band then lies between 0.9025 and 1, which is −0.89 dB at worst, and the stop band is at −80.00 dB. The worst pass-band loss doubles, from 0.4455 dB to 0.8911 dB, because squaring a gain doubles its dB value.

Where you’ll meet this

Audio plug-ins often offer both kinds. An equaliser may have a “linear phase” mode, which keeps the shape of drum hits at the cost of delay, and a “minimum phase” mode with almost no delay, for live use.

Biomedical toolkits filter stored recordings forwards and backwards. An ECG’s waves keep their timing and shape, so a doctor can still measure them. Real-time control loops, such as a motor controller, use IIR filters for their short delay, because every sample of delay in a feedback loop makes it harder to keep stable.

How do you actually run an IIR filter, and how do you keep it stable with rounded numbers? That is chapter 21: Filter structures (21.1), Second-order sections (21.2) and Finite word-length effects (21.3).

The maths behind it · symmetric matrices

Write filtering a stored signal as a matrix: y=Hx\mathbf{y}=\mathbf{H}\mathbf{x}, with H\mathbf{H} lower triangular. Reversing is the exchange matrix J\mathbf{J} of 17.3’s bridge, so forwards then backwards is JHJH\mathbf{J}\mathbf{H}\mathbf{J}\mathbf{H}. Flipping a Toeplitz matrix end to end transposes it, so this equals the symmetric matrix H⊤H\mathbf{H}^\top\mathbf{H}. A symmetric operator cannot shift anything one way.

The maths behind it · centred moving averages

A centred moving average smooths a time series without lag, and a trailing one lags (17.3’s bridge). Forward–backward filtering is how time-series software, for example scipy.signal.filtfilt, gets the centred version of any filter.

Reference card

PropertyFIR (equiripple, 26 taps)IIR (elliptic, order 5)
Multiplies per sample26 (13 with symmetry)11
Delay12.5 samples, every frequency3.7 at 0 Hz, 23.1 at 1 kHz
Phaselinear: shapes keptbent: shapes change
Stabilityalwayspoles must stay inside (21.2–21.3)
Offline zero phasenot needed: the delay is already constantforwards then backwards: ∣H∣2\lvert H\rvert^2
QuantityFormulaNotes
Multiplies per sample, FIRNhN_h, or Nh/2N_h/2 with symmetric taps26 or 13 here
Multiplies per sample, IIRnumber of bkb_k plus number of aka_k after a0a_06+5=116+5=11 here
Forwards then backwardsY(ejΩ)=∣H(ejΩ)∣2X(ejΩ)Y(e^{j\Omega})=\lvert H(e^{j\Omega})\rvert^2X(e^{j\Omega})zero phase, gain squared, offline only
Centre of an outputmoves by τg(0)\tau_g(0)3.72 samples forwards, 0.00 forwards and backwards

End of lesson 20.6

Where to go next.

Phasorium
LibraryEvery lesson, in order

Parts

About Phasorium
Look