|
| 1 | +# Moving Average (Running-Sum Boxcar) |
| 2 | + |
| 3 | +## Overview & Motivation |
| 4 | + |
| 5 | +Sensor streams in embedded systems are corrupted by zero-mean white noise. The simplest way to |
| 6 | +attenuate that noise is to average consecutive samples. The moving average does this with a sliding |
| 7 | +window of length N, treating every sample inside the window equally. Its critical property for |
| 8 | +embedded use is that the recursive running-sum form requires exactly one addition, one subtraction, |
| 9 | +and one multiply per output sample — constant work regardless of how large the window is. |
| 10 | + |
| 11 | +## Mathematical Theory |
| 12 | + |
| 13 | +### Core Definitions |
| 14 | + |
| 15 | +Let $x[n]$ be the input sequence. The length-$N$ boxcar output is: |
| 16 | + |
| 17 | +$$y[n] = \frac{1}{N} \sum_{k=0}^{N-1} x[n-k]$$ |
| 18 | + |
| 19 | +### Recursive Form |
| 20 | + |
| 21 | +Expanding $y[n]$ in terms of $y[n-1]$: |
| 22 | + |
| 23 | +$$y[n] = y[n-1] + \frac{x[n] - x[n-N]}{N}$$ |
| 24 | + |
| 25 | +Equivalently, maintain a running sum $S[n] = \sum_{k=0}^{N-1} x[n-k]$: |
| 26 | + |
| 27 | +$$S[n] = S[n-1] + x[n] - x[n-N], \qquad y[n] = \frac{S[n]}{N}$$ |
| 28 | + |
| 29 | +### Frequency Response |
| 30 | + |
| 31 | +The discrete-time transfer function is: |
| 32 | + |
| 33 | +$$H(z) = \frac{1}{N} \sum_{k=0}^{N-1} z^{-k} = \frac{1}{N} \cdot \frac{1 - z^{-N}}{1 - z^{-1}}$$ |
| 34 | + |
| 35 | +The magnitude response is a $\mathrm{sinc}$-shaped envelope: |
| 36 | + |
| 37 | +$$|H(e^{j\omega})| = \frac{1}{N} \left| \frac{\sin(N\omega/2)}{\sin(\omega/2)} \right|$$ |
| 38 | + |
| 39 | +DC gain is exactly 1. Group delay is constant at $(N-1)/2$ samples — the filter is linear phase. |
| 40 | + |
| 41 | +## Complexity Analysis |
| 42 | + |
| 43 | +| Case | Time | Space | Notes | |
| 44 | +|------|--------|--------|--------------------------------| |
| 45 | +| All | $O(1)$ | $O(N)$ | One add, one sub, one multiply | |
| 46 | + |
| 47 | +The $O(1)$ time cost is the key advantage over the naive $O(N)$ direct-form sum. |
| 48 | + |
| 49 | +## Step-by-Step Walkthrough |
| 50 | + |
| 51 | +Input: unit step $x[n] = 1$ for $n \ge 0$, $N = 4$, initial state all zeros. |
| 52 | + |
| 53 | +| $n$ | $x[n]$ | $x[n-4]$ | $S[n]$ | $y[n]$ | |
| 54 | +|-----|--------|----------|--------|--------| |
| 55 | +| 0 | 1 | 0 | 1 | 0.25 | |
| 56 | +| 1 | 1 | 0 | 2 | 0.50 | |
| 57 | +| 2 | 1 | 0 | 3 | 0.75 | |
| 58 | +| 3 | 1 | 0 | 4 | 1.00 | |
| 59 | +| 4 | 1 | 1 | 4 | 1.00 | |
| 60 | + |
| 61 | +The output ramps up linearly and then holds at 1 once the window is fully filled. |
| 62 | + |
| 63 | +## Pitfalls & Edge Cases |
| 64 | + |
| 65 | +- **Running-sum drift**: floating-point rounding accumulates in $S[n]$ over long constant runs. |
| 66 | + For very long sequences, periodically recompute the sum directly from the window contents. |
| 67 | +- **Startup transient**: the first $N-1$ outputs are averages of fewer than $N$ samples unless the |
| 68 | + window is pre-filled with an initial value. |
| 69 | +- **Window length 1**: reduces to pass-through — no smoothing occurs. |
| 70 | +- **Poor frequency selectivity**: sidelobes in the stopband are only $-13\,\mathrm{dB}$ below the |
| 71 | + passband; use a Biquad cascade when sharp roll-off is needed. |
| 72 | + |
| 73 | +## Variants & Generalizations |
| 74 | + |
| 75 | +- **Weighted moving average** (triangular, Gaussian): tap weights are no longer uniform; loses the |
| 76 | + $O(1)$ recursive property. |
| 77 | +- **Exponential moving average (EMA)**: an IIR approximation requiring only one multiply-add and no |
| 78 | + delay line; trades linear phase for lower memory cost. |
| 79 | +- **Savitzky-Golay filter**: fits a polynomial to the window, preserving peak shapes while |
| 80 | + smoothing — higher computational cost. |
| 81 | +- **CIC filter**: cascaded integrator-comb, a multi-stage generalization used for decimation. |
| 82 | + |
| 83 | +## Applications |
| 84 | + |
| 85 | +- ADC noise reduction on sensor streams (accelerometers, temperature, pressure). |
| 86 | +- Decimation pre-filter before down-sampling. |
| 87 | +- Smoothing control-loop feedback signals to suppress high-frequency switching artifacts. |
| 88 | +- Baseline/trend estimation when a flat passband and linear phase are required. |
| 89 | + |
| 90 | +## Connections to Other Algorithms |
| 91 | + |
| 92 | +- The FIR filter with uniform coefficients $b_k = 1/N$ is mathematically identical; this |
| 93 | + implementation exploits the uniformity for $O(1)$ computation. |
| 94 | +- `ExponentialMovingAverage` solves the same smoothing problem with $O(1)$ memory at the cost of |
| 95 | + non-linear phase and an infinite impulse response. |
| 96 | +- `SavitzkyGolayFilter` generalizes the boxcar by fitting a polynomial, preserving higher-order |
| 97 | + signal features. |
| 98 | + |
| 99 | +## References & Further Reading |
| 100 | + |
| 101 | +- S. W. Smith, *The Scientist and Engineer's Guide to Digital Signal Processing*, Ch. 15 — |
| 102 | + Moving Average Filters (recursive running-sum form). Available at dspguide.com. |
| 103 | +- A. V. Oppenheim & R. W. Schafer, *Discrete-Time Signal Processing*, 3rd ed., §7.5. |
0 commit comments