ENع
→The moving average
الدرس 39 · Digital filter design

The moving average: your first FIR filter

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

15 دقيقة11 خطوة5 أسئلةAdvanced
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 39 · تفاعلي
01 / 11

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.83
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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.83
  2. A 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 tick
  3. Weigh 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 taps
  4. A 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]
  5. 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 ...
  6. 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 unchanged
  7. The 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·ω
  8. 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/sample
  9. Zeros 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π
  10. 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)
  11. 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

اختبار يتاح بعد الخطوة الأخيرة.