The idea: a filter is a small sum, run once per sample

A digital filter gets one new sample x[n] at a time and gives back one output y[n]. The page samples at fs = 100 Hz, so a 5 Hz sine has 20 samples per period, and 50 Hz is the fastest wave the samples can show. The input is that sine plus noise. The job: keep the 5 Hz wave, lose the noise.

There are two kinds of recipe. An FIR filter (finite impulse response) uses only the last few inputs. An IIR filter (infinite impulse response) also uses its own last output.

Left: the 5-tap FIR. Boxes x[n] to x[n−4] joined by z⁻¹ delays, each multiplied by 0.2, all five products added into y[n]. Right: the IIR. x[n] times 0.2 goes into an adder; the adder gives y[n]; y[n] goes through a z⁻¹ delay, is multiplied by 0.8 and fed back into the adder.
FIR: a row of delays, a weight on each, one sum. IIR: one weight in, one weight on the fed-back output.

FIR: the tap-delay line

An FIR filter stores the last L inputs in a tap-delay line. Each box is one sample of delay, written z⁻¹. When a new sample comes in, every stored value moves one box along and the oldest drops out. Then each box (a tap) is multiplied by its coefficient h[k] and the products are added:

y[n] = h[0]·x[n] + h[1]·x[n−1] + … + h[L−1]·x[n−L+1]

The 5-tap moving average uses h[k] = 0.2 for all five taps: y[n] is the mean of the last 5 inputs (Demo 1). The filter starts at rest, with zeros in the line, so the first outputs are small: y[0] = 0.2·(−0.40) = −0.08, y[1] = 0.2·(0.33 − 0.40) = −0.014. From n = 4 on the line is full. Over n = 20…59 the noise rms falls from 0.231 to 0.105.

This is the convolution sum with a short h, worked out on discrete-time LTI systems and the convolution sum. This page looks at what such sums do to frequencies.

Frequency response and nulls

Put a sine of frequency f into an LTI filter and a sine of the same f comes out. Only two things change: its size is multiplied by |H(f)|, and it comes out later. For an FIR filter, with ω = 2πf / fs,

H(f) = Σ h[k]·e−jωk

For the moving average, |H| = 1 at 0 Hz, 0.904 at 5 Hz and 0.647 at 10 Hz. At 20 Hz it is exactly 0, a null. A 20 Hz wave repeats every 5 samples, so the 5 taps always hold one full period, and the samples of a full period add to 0. The same happens at 40 Hz, with two periods in the line. Feed it a 5 Hz sine plus a 20 Hz tone: from n = 4 on the output is exactly 0.904·sin(2π·5(n−2)/100), and the tone is gone (Demo 2). Between the nulls the side lobes still pass up to 0.25 of the input (0.2 at 25 Hz), so the moving average is a weak low-pass.

Designing a low-pass: the windowed sinc

An ideal low-pass keeps everything below the cutoff fc and nothing above it. Its impulse response is a sinc: h[k] = (2fc/fs)·sinc(2fc/fs·(k − 10)), centred on k = 10. With fc = 12.5 Hz that is h[10] = 0.25 and h[10 ± m] = sin(πm/4)/(πm). The sinc goes on forever in both directions, so a real filter keeps only some of it, here 21 taps (k = 0…20). How you cut it is the window.

  • Rectangular window: keep the 21 values, drop the rest (Demo 3). The hard edge gives ripple: |H| swings between 0.966 and 1.073 in the pass band (the Gibbs effect) and leaks up to 0.063 (−24 dB) above 20 Hz. The drop is steep: |H| goes from 0.9 to 0.1 between 10.5 and 14.6 Hz.
  • Hamming window: first multiply each tap by w[k] = 0.54 − 0.46·cos(2πk/20), which fades to 0.08 at both ends (Demo 4). The ripple almost goes: the pass band peaks at 1.004 and nothing above 20 Hz passes more than 0.012 (−38.6 dB). The price is a wider transition: from 0.9 to 0.1 takes 7.9 to 17.0 Hz, about twice as wide.
Left: the 21 coefficients h[k]. The rectangular-window sinc has bigger tails; the Hamming version tapers towards zero at k = 0 and k = 20. Right: |H(f)| from 0 to 50 Hz. The rectangular curve overshoots to 1.073, drops from 0.9 to 0.1 between 10.5 and 14.6 Hz and ripples up to 0.063 in the stop band. The Hamming curve is smooth, drops between 7.9 and 17.0 Hz and stays under 0.012 above 20 Hz.
Same 21 taps, two windows: a hard cut gives a steep edge with ripple, a smooth taper gives low ripple with a wider edge.
21 taps, fc = 12.5 HzrectangularHamming
pass band 0–10 Hz0.966 … 1.0730.756 … 1.004
largest |H| above 20 Hz0.063 (−24 dB)0.012 (−38.6 dB)
|H| from 0.9 to 0.110.5 → 14.6 Hz (4.1 Hz)7.9 → 17.0 Hz (9.1 Hz)
|H| at 5 Hz0.9830.988

More taps make the transition narrower for either window; the window decides how much ripple you pay for it. The window trade-off comes back when you cut a signal into blocks for the DFT; see DFT leakage and windows. On the noisy sine the Hamming filter leaves noise rms 0.118 and keeps the sine at 0.988 (Demo 5). Its output is 10 samples late. That is 100 ms, half a 5 Hz period, so the output looks upside down next to the input.

IIR: feedback and the pole

The one-pole IIR filter y[n] = 0.8·y[n−1] + 0.2·x[n] keeps 80 % of its last output and mixes in 20 % of the new input (Demo 6). It needs 2 multiplies and 1 add per sample, and one stored number: the feedback path carries y[n−1] back. Each input is still felt later, shrinking by 0.8 per sample, so its impulse response never quite ends. That is why it is called "infinite".

Its transfer function is H(z) = 0.2 / (1 − 0.8z⁻¹), with a pole at z = 0.8. The pole is inside the unit circle, so the filter is stable, and the closer it gets to 1, the stronger the smoothing (see z-transform, poles and zeros for reading |H| off the pole). |H| = 1 at 0 Hz, 0.707 (−3 dB) at 3.57 Hz, 0.581 at 5 Hz, 0.187 at 20 Hz and 0.111 at 50 Hz. On the noisy sine it gives the lowest noise of the four filters, rms 0.073, but it also cuts the 5 Hz sine to 58 %, and |H| has no nulls.

Phase: linear phase vs phase lag

Every filter here makes its output late. What matters is whether all frequencies are late by the same time. A filter whose h is symmetric, h[k] = h[L−1−k], delays every frequency by exactly (L−1)/2 samples. Its phase is −ω·(L−1)/2, a straight line in f: linear phase. The moving average delays everything by 2 samples, the 21-tap filters by 10.

The IIR has no such symmetry. Its phase delay −arg H / ω is 4 samples near 0 Hz, 3.89 at 1 Hz, 2.55 at 5 Hz, 0.63 at 20 Hz and 0 at 50 Hz. On a clean 5 Hz sine the input peaks at n = 25, the moving average peaks at n = 27, and the IIR peaks at n = 27.55, a phase lag of 45.9° (Demo 7). If a signal has several frequencies, each one comes out of the IIR shifted by a different time, so the wave shape changes. The FIR keeps it.

Delay in samples against frequency from 0 to 50 Hz. The 5-tap moving average is a flat line at 2. The one-pole IIR starts at 4 at 0 Hz, is 2.55 at 5 Hz and 0.63 at 20 Hz, and falls to 0 at 50 Hz.
A symmetric FIR delays every frequency the same; the IIR delays slow waves more than fast ones.

FIR or IIR?

FIRIIR
work per sampleone multiply-add per tap (5, 21, often hundreds)a few multiply-adds per pole (here 2)
phaselinear if h is symmetricnot linear: the delay depends on f
stabilityalways stable (no feedback)stable only if every pole is inside the unit circle
designwindow a sinc, or optimise the tapsstart from an analog filter (Butterworth, …) or place poles
typical useaudio, images, data that must keep its shapesensor smoothing on a small microcontroller, tone controls

The samples these filters run on usually come from an ADC; see ADC and PWM on a microcontroller. The same idea in two dimensions, done by multiplying spectra instead of summing taps, is frequency-domain filtering of images. Why 50 Hz is the top of every plot is on sampling and aliasing, and long FIR filters are often run with the FFT.

See also DFT leakage and windows for the main-lobe and side-lobe trade-off of rectangular, Hann and Hamming windows.

See also PID control, where the derivative term is usually passed through a small low-pass filter like these.

What the page leaves out

Only four fixed filters are shown. The page leaves out optimal FIR design (Parks–McClellan), other windows (Hann, Blackman, Kaiser), high-pass and band-pass designs, higher-order IIR filters (Butterworth, Chebyshev, the bilinear transform), second-order sections, fixed-point rounding, forward-backward filtering for zero phase, and group delay as distinct from the phase delay plotted here. The noise is a fixed pseudo-random sequence, and the first samples after rest are part of the output, not trimmed.

On the mind map

Open the Geometric Algorithms mind mapGeometricAlgorithmsTransform matrices: multiply each vertex by a matrixTransform matricesCoordinate frames: same point, different framesCoordinate framesHulls and curves: cross products and lerpsHulls and curvesRendering: which pixel shows whatRenderingFourier and DCT: a signal as a sum of wavesFourier and DCTTriangulations: mesh a point setTriangulationsSegments and polygons: lines, crossings, regionsSegments and polygonsFourier series and FFTFourier series and FFTcircles add up; FFT is O(n log n)Z-Transform Poles and ZerosZ-Transform Poles and ZerosStable: every pole inside the unit circleFIR and IIR filtersFIR: taps, linear phase; IIR: feedback, poleDiscrete LTI systemsDiscrete LTI systemsy = x * h: flip, slide, multiply, addDFT Leakage and WindowsDFT Leakage and WindowsWhole cycles: one bin; else leakageJPEG DCTJPEG DCTcosine coefficients, round most to zeroSampling and aliasingSampling and aliasingabove fs/2 a wave folds into a slower aliasOpen the Geometric Algorithms mind mapGeometric Algorithmsa signal as a sum of wavesFourier and DCTFourier series and FFTFourier series and FFTcircles add up; FFT is O(n log n)Z-Transform Poles and ZerosZ-Transform Poles and ZerosStable: every pole inside the unit circleFIR and IIR filtersFIR: taps, linear phase; IIR: feedback, poleDiscrete LTI systemsDiscrete LTI systemsy = x * h: flip, slide, multiply, addDFT Leakage and WindowsDFT Leakage and WindowsWhole cycles: one bin; else leakageJPEG DCTJPEG DCTcosine coefficients, round most to zeroSampling and aliasingSampling and aliasingabove fs/2 a wave folds into a slower alias
This page on the Geometric Algorithms mind map · 32 pages in 7 branches · open it to see the whole map and test yourself in recall mode.