ADCx[n]–x[n−1]–x[n−2]–x[n−3]–× 1/4× 1/4× 1/4× 1/4Σy[n]–1import numpy as np2n = np.arange(40)3x = 20 + 4*np.sin(np.pi*n/20)4x += 2*np.cos(np.pi*n/2) + (-1)**n5h = np.ones(4) / 46y = np.convolve(x, h, "valid")x[0] = 23.00
01020303951015202530n (sample)valueTIME nx[n]slow s[n]y[n]wobble gone1.5 late, 1.5% smaller-10123456700.10.20.3nh[n]IMPULSE h[n]1/4, four times0π/4π/23π/4π00.250.50.751ω (radians per sample)gainGAIN |H(ω)||H(π/20)| = 0.985leak 0.27zeros at π/2 and π
Coloured parts: what the current step is about; everything else rests in whiteBlocks: sensor chip (ADC), delay line, weights × 1/4, adder Σ, output cell and a Python code cardPlots: x[n] and y[n] over time, the impulse response h[n] and the gain |H(ω)|Tokens carry samples, products and sums between the parts, one tick at a time, in slow motionA 4-point moving average y[n] = (x[n] + x[n−1] + x[n−2] + x[n−3]) / 4 removes a wobble that repeats every 4 samples and passes a slow wave, 1.5 samples late. 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.

A noisy reading

  1. 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.

The averaging machine

Finite impulse response

What it does to frequencies

Code and design

The working

# step 1/11 · noisy

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.83

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. 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.