The moving average: your first FIR filter
About this lesson
The moving average replaces each reading by the average of the last M readings. Watch a 4-point moving average at work: a delay line holds the last four samples, each is weighed by 1/4, an adder sums them, and a fast wobble that rides on a slow wave vanishes. Its impulse response is four samples long, which makes it the simplest finite impulse response (FIR) filter. You will read its frequency response, see why its zeros remove the wobble, measure its 1.5-sample lag and run it in Python with numpy. It builds on the Signals lessons on discrete convolution and frequency response and on the Circuits lessons on filters and Bode plots; the Embedded lesson "Smoothing noisy readings" uses it on a real sensor.
A noisy reading
A sensor sends one reading x[n] per tick. Inside is a slow wave, but a fast wobble rides on top: +3, −1, −1, −1, repeating every 4 samples. We want the slow wave back.
x[n] = s[n] + w[n], n = 0, 1, 2 ... 39
s[n] = 20 + 4·sin(2π·n/40) slow wave
w[n] = 2·cos(π·n/2) + (−1)ⁿ wobble
= +3, −1, −1, −1, +3, −1 ...
x[0..5] = 23.00, 19.63, 20.24, 20.82, 25.35, 21.83Read the lesson step by step
A noisy reading
A sensor sends one reading x[n] per tick. Inside is a slow wave, but a fast wobble rides on top: +3, −1, −1, −1, repeating every 4 samples. We want the slow wave back.
x[n] = s[n] + w[n], n = 0, 1, 2 ... 39 s[n] = 20 + 4·sin(2π·n/40) slow wave w[n] = 2·cos(π·n/2) + (−1)ⁿ wobble = +3, −1, −1, −1, +3, −1 ... x[0..5] = 23.00, 19.63, 20.24, 20.82, 25.35, 21.83A delay line keeps the last four
Each new sample enters on the left, and every stored sample moves one place right. The oldest falls off the end. So the line always holds the last four readings: x[n], x[n−1], x[n−2], x[n−3].
a delay line (shift register) of 4 cells tick n: new x[n] -> cell 1 x[n−1] -> cell 2 x[n−2] -> cell 3 x[n−3] -> cell 4 x[n−4] falls off each cell delays a sample by one tickWeigh by 1/4 and add
Each stored sample is multiplied by its weight, 1/4, and the adder sums the four products: y[3] = (20.82 + 20.24 + 19.63 + 23.00) / 4 = 20.92. That is the average of the last four readings.
y[n] = (x[n] + x[n−1] + x[n−2] + x[n−3]) / 4 y[3] = (20.82 + 20.24 + 19.63 + 23.00) / 4 = 83.68 / 4 = 20.92 the weights h[k] = 1/4 are the tapsA new output every tick
The window slides one sample each tick and gives a new average. Any four samples in a row hold one whole wobble, +3 − 1 − 1 − 1 = 0, so the wobble cancels exactly. A smooth curve comes out.
y[3] = 20.92 y[4] = 21.51 (the window moved by one) ... wobble over any 4 in a row: +3 −1 −1 −1 = 0 so y[n] keeps only the slow wave the first 3 outputs need samples before n = 0, so the plot starts at y[3]Feed it a single 1
Now feed in one 1 and then only zeros: an impulse. The 1 walks through the four cells, so the output is 1/4, 1/4, 1/4, 1/4 and then 0 for ever. This output is the impulse response h[n].
input δ[n] = 1, 0, 0, 0, 0, 0 ... output h[n]: h[0] = 1/4 = 0.25 h[1] = 0.25 h[2] = 0.25 h[3] = 0.25 h[4] = 0 the 1 has fallen off h[5] = 0 ...Finite impulse response: FIR
The impulse response stops after 4 samples: it is finite. A filter like this is an FIR filter. Every FIR filter computes y[n] = Σ h[k]·x[n−k], a convolution. The moving average is the simplest: all M weights equal 1/M.
FIR = finite impulse response y[n] = Σ h[k]·x[n − k], k = 0 .. M−1 (a convolution of x with h) moving average: h[k] = 1/M for all k here M = 4 weights add to 1, so a constant input comes out unchangedThe price: a lag
Compare y[n] with the clean slow wave: same shape, 1.5% smaller and 1.5 samples late. A 4-point average describes the middle of its window, so it lags by (M − 1)/2 = (4 − 1)/2 = 1.5 samples.
clean wave s[n] = 20 + 4·sin(ω0·n) output y[n] = 20 + 4·0.985·sin(ω0·(n − 1.5)) ω0 = 2π/40 rad per sample smaller by 1 − 0.985 = 1.5% later by (M − 1)/2 = 1.5 samples the same delay for every frequency: linear phase, −1.5·ωWhat it does to each frequency
Feed in a sine of frequency ω and a sine of the same ω comes out, scaled by the gain |H(ω)| = |sin(2ω) / (4·sin(ω/2))|. Slow sines pass with gain near 1; fast ones shrink. It is a low-pass filter.
H(ω) = (1/4)·(1 + e^−jω + e^−2jω + e^−3jω) = e^(−j1.5ω) · sin(2ω) / (4·sin(ω/2)) ω in radians per sample, 0 .. π (π = half the sample rate) |H(0)| = 1 (a constant passes) |H(π/20)| = 0.985 (our slow wave) −3 dB at ω ≈ 0.72 rad/sampleZeros right on the wobble
The gain is exactly 0 at ω = π/2 and π, a quarter and half of the sample rate. The wobble lives exactly there, so it vanishes. In between the gain climbs back to 0.27: a cheap but leaky low-pass.
zeros of the gain at ω = 2πk/M: k = 1 -> ω = π/2 (repeats every 4) k = 2 -> ω = π (repeats every 2) wobble = 2·cos(π·n/2) + 1·cos(π·n) both parts sit on a zero -> removed leaky band: peak |H| = 0.27 (−11.3 dB) at ω ≈ 0.73πThe same filter in Python
With numpy, np.ones(4)/4 makes the four weights and np.convolve slides them along x. Mode "valid" keeps only outputs whose window holds four real samples, so y[0] here is y[3] above: 20.92.
import numpy as np n = np.arange(40) x = 20 + 4*np.sin(np.pi*n/20) x += 2*np.cos(np.pi*n/2) + (-1)**n h = np.ones(4) / 4 y = np.convolve(x, h, "valid") >>> x.round(2)[:6] [23. 19.63 20.24 20.82 25.35 21.83] >>> y.round(2)[:5] [20.92 21.51 22.06 22.56 22.99] >>> len(y) 37 (Python 3 with numpy)Choosing the length M
A longer average smooths more but lags more: (M − 1)/2 samples. On random noise, averaging M readings shrinks the noise by √M, so M = 4 halves it. For sharper filters, see FIR design by windowing.
M = 4 delay 1.5 samples noise ÷ 2 M = 16 delay 7.5 samples noise ÷ 4 M = 100 delay 49.5 samples noise ÷ 10 random (white) noise: shrinks by √M a wobble on the zeros: removed exactly zeros at ω = 2πk/M move closer to 0 as M grows: a narrower low-pass
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A 3-point moving average sees the inputs x[n] = 6, x[n−1] = 3, x[n−2] = 9. What is y[n]?
y[n] = (6 + 3 + 9) / 3 = 18 / 3 = 6. Each of the three stored samples is weighed by 1/3 and the products are added.
02Why is the moving average called a finite impulse response (FIR) filter?
Feed in an impulse and the output is 1/M for M samples, then 0 for ever: the 1 falls off the end of the delay line. A response that ends is finite.
03How many samples does an 8-point moving average lag behind its input?
An M-point average describes the middle of its window, so the delay is (M − 1)/2 = (8 − 1)/2 = 3.5 samples, the same for every frequency (linear phase).
04At which frequencies does a 4-point moving average have zero gain?
The zeros sit at ω = 2πk/M for k = 1 .. M − 1 inside 0 .. π: here π/2 and π. At ω = 0 the gain is 1, so a constant passes unchanged.
05A sensor's random white noise has a size of 2 mV. Roughly how big is it after a 16-point moving average?
Averaging M samples of white noise shrinks it by √M. √16 = 4, so 2 mV / 4 = 0.5 mV. A pattern that sits exactly on the zeros is removed completely, but random noise is only reduced.
Glossary
- Moving average
- A filter whose output is the average of the last M inputs: y[n] = (1/M)·(x[n] + x[n−1] + ... + x[n−M+1]).
- Delay line
- A chain of storage cells; each tick every sample moves one cell along, so the chain holds the last M samples.
- Tap (weight)
- The number each stored sample is multiplied by. A moving average has M equal taps of 1/M.
- Impulse response h[n]
- The output when the input is a single 1 at n = 0 and zero elsewhere. For an LTI filter it fully describes the filter.
- FIR filter
- Finite impulse response: the impulse response becomes exactly zero after a finite number of samples. Output y[n] = Σ h[k]·x[n − k].
- Frequency response H(ω)
- How much a sine of frequency ω (radians per sample) is scaled and shifted on its way through; |H(ω)| is the gain.
- Low-pass filter
- A filter that passes slow (low-frequency) signals and shrinks fast ones.
- Linear phase, group delay
- Every frequency is delayed by the same number of samples; for the moving average (M − 1)/2.
Notes and sources
- The slow wave and the wobble are a chosen example: the wobble repeats every 4 samples and adds to zero over any 4 in a row, so a 4-point average removes it exactly. Every number in the figure is computed from this model.
- Real sensor noise is random and is only reduced, not removed: averaging M samples of white noise cuts its size by √M (Smith, ch. 15). The moving average is the best linear filter for that task at a given step sharpness, but poor at separating frequency bands.
- The filter starts from rest: before n = 0 the delay line holds zeros, so y[0], y[1] and y[2] are warm-up values and are not plotted. numpy's "valid" mode drops them too.
- A. V. Oppenheim, R. W. Schafer, J. R. Buck, Discrete-Time Signal Processing (required text listed on MIT OCW 6.341 readings)
- A. V. Oppenheim, A. S. Willsky, S. H. Nawab, Signals and Systems, 2nd ed., Pearson
- MIT OpenCourseWare RES.6-008 Digital Signal Processing (A. V. Oppenheim): nonrecursive (FIR) digital filters
- MIT OpenCourseWare 6.003 Signals and Systems (D. Freeman, Fall 2011): convolution and frequency response
- S. W. Smith, The Scientist and Engineer's Guide to DSP, ch. 15 Moving Average Filters (noise falls by the square root of the number of points)
- Finite impulse response (Wikipedia): definition, y[n] = Σ b_i·x[n − i], moving average example
- NumPy documentation: numpy.convolve (modes full, same, valid)
- NumPy documentation: numpy.ones
Quiz Unlocks after the last step.