|
| 1 | +# Exponential Moving Average |
| 2 | + |
| 3 | +## Overview & Motivation |
| 4 | + |
| 5 | +Sensor data in embedded systems is noisy. A full moving-average buffer costs O(N) RAM and O(N) |
| 6 | +computation per sample. The exponential moving average (EMA), also called a one-pole recursive |
| 7 | +low-pass filter or leaky integrator, achieves useful smoothing with a single state word and one |
| 8 | +multiply-accumulate per sample. It is the minimal-cost filter that tracks a slowly varying DC |
| 9 | +level while suppressing high-frequency noise. |
| 10 | + |
| 11 | +## Mathematical Theory |
| 12 | + |
| 13 | +### Core Definition |
| 14 | + |
| 15 | +Given a smoothing factor $\alpha \in (0, 1]$ and a discrete input sequence $x[n]$, the EMA |
| 16 | +output $y[n]$ obeys the first-order linear recurrence: |
| 17 | + |
| 18 | +$$y[n] = \alpha \, x[n] + (1 - \alpha) \, y[n-1]$$ |
| 19 | + |
| 20 | +with $y[-1] = y_0$ (the initial state, often zero). |
| 21 | + |
| 22 | +### Rearranged Update Form |
| 23 | + |
| 24 | +Expanding the recurrence: |
| 25 | + |
| 26 | +$$y[n] = y[n-1] + \alpha \bigl(x[n] - y[n-1]\bigr)$$ |
| 27 | + |
| 28 | +This form has one subtraction and one multiply-accumulate; it avoids computing $(1-\alpha)$ |
| 29 | +explicitly and keeps the error term $(x[n] - y[n-1])$ small, which is numerically preferable in |
| 30 | +fixed-point arithmetic. |
| 31 | + |
| 32 | +### Pole Location and Stability |
| 33 | + |
| 34 | +The transfer function in the $z$-domain is: |
| 35 | + |
| 36 | +$$H(z) = \frac{\alpha}{1 - (1-\alpha)\,z^{-1}}$$ |
| 37 | + |
| 38 | +The single pole is at $z = 1 - \alpha$. For $\alpha \in (0, 1]$ the pole lies in $[0, 1)$, which |
| 39 | +is strictly inside the unit circle, so the filter is unconditionally stable. |
| 40 | + |
| 41 | +### DC Gain |
| 42 | + |
| 43 | +Setting $z = 1$ (DC, $\omega = 0$): |
| 44 | + |
| 45 | +$$H(1) = \frac{\alpha}{1 - (1-\alpha)} = 1$$ |
| 46 | + |
| 47 | +Unity DC gain: a constant input converges exactly to that constant. |
| 48 | + |
| 49 | +### Cutoff Frequency |
| 50 | + |
| 51 | +Matching the EMA pole to a first-order RC low-pass filter with time constant $\tau = 1/(2\pi f_c)$ |
| 52 | +and sample period $T_s = 1/f_s$: |
| 53 | + |
| 54 | +$$\alpha = \frac{T_s}{\tau + T_s} = \frac{T_s}{\frac{1}{2\pi f_c} + T_s}$$ |
| 55 | + |
| 56 | +### Impulse Response |
| 57 | + |
| 58 | +The impulse response is $h[n] = \alpha (1-\alpha)^n \, u[n]$, which decays geometrically — |
| 59 | +hence the name "exponential" moving average. |
| 60 | + |
| 61 | +## Complexity Analysis |
| 62 | + |
| 63 | +| Case | Time | Space | Notes | |
| 64 | +|---------|------|-------|----------------------------------------| |
| 65 | +| Best | O(1) | O(1) | Single MAC per sample, one state word | |
| 66 | +| Average | O(1) | O(1) | Identical regardless of input | |
| 67 | +| Worst | O(1) | O(1) | No data-dependent branches on hot path | |
| 68 | + |
| 69 | +## Step-by-Step Walkthrough |
| 70 | + |
| 71 | +Suppose $\alpha = 0.5$, $y[-1] = 0$, and the input is the unit impulse $x = [1, 0, 0, 0, \ldots]$. |
| 72 | + |
| 73 | +| $n$ | $x[n]$ | $x[n] - y[n-1]$ | $\alpha \cdot \Delta$ | $y[n]$ | |
| 74 | +|-----|--------|----------------------|-----------------------|----------| |
| 75 | +| 0 | 1 | $1 - 0 = 1$ | $0.5$ | $0.5$ | |
| 76 | +| 1 | 0 | $0 - 0.5 = -0.5$ | $-0.25$ | $0.25$ | |
| 77 | +| 2 | 0 | $0 - 0.25 = -0.25$ | $-0.125$ | $0.125$ | |
| 78 | +| 3 | 0 | $0 - 0.125 = -0.125$ | $-0.0625$ | $0.0625$ | |
| 79 | + |
| 80 | +Each output is half the previous, confirming $h[n] = (0.5)^{n+1}$. |
| 81 | + |
| 82 | +## Pitfalls & Edge Cases |
| 83 | + |
| 84 | +- $\alpha \to 0$: The filter barely reacts; the time constant grows toward infinity. At exactly |
| 85 | + zero the state never updates (division by zero in the cutoff formula). |
| 86 | +- $\alpha = 1$: Pure passthrough — no smoothing at all; the output equals the input exactly. |
| 87 | +- Initial transient: If the initial state differs significantly from the true signal level, the |
| 88 | + output will have a startup transient lasting approximately $1/\alpha$ samples. Seeding the |
| 89 | + state with the first sample eliminates this. |
| 90 | +- Floating-point accumulation: Long runs of constant input may show residual drift at the level |
| 91 | + of floating-point epsilon relative to the signal magnitude; this is negligible for float32. |
| 92 | + |
| 93 | +## Variants & Generalizations |
| 94 | + |
| 95 | +- **Double EMA (DEMA)**: Applies the EMA twice and corrects for lag: $2\,y_1[n] - y_2[n]$. |
| 96 | + Reduces lag at the cost of one extra state word and multiply. |
| 97 | +- **Kaufman Adaptive Moving Average (KAMA)**: Adjusts $\alpha$ dynamically based on market |
| 98 | + efficiency ratio; the same one-pole structure with a time-varying coefficient. |
| 99 | +- **Exponentially Weighted Moving Variance**: Tracks variance alongside the mean using a second |
| 100 | + recurrence on the squared error. |
| 101 | +- **Higher-order cascades**: Cascading $k$ EMA stages produces a Butterworth-like response with |
| 102 | + $k$ poles and $-20k$ dB/decade roll-off. |
| 103 | + |
| 104 | +## Applications |
| 105 | + |
| 106 | +- Sensor de-noising (IMU, ADC, temperature) where buffer RAM is scarce. |
| 107 | +- DC-offset tracking and removal. |
| 108 | +- Smoothed feedback signals in PID control loops. |
| 109 | +- Battery voltage estimation. |
| 110 | +- Signal envelope detection. |
| 111 | + |
| 112 | +## Connections to Other Algorithms |
| 113 | + |
| 114 | +- **FIR moving average**: Linear-phase alternative; symmetric impulse response gives zero group |
| 115 | + delay distortion but requires O(N) RAM and O(N) MACs. |
| 116 | +- **Biquad cascade (IIR)**: Higher-order roll-off at the cost of two state words per section and |
| 117 | + potential instability if coefficients are not quantised carefully. |
| 118 | +- **Kalman filter (scalar)**: The steady-state scalar Kalman gain is equivalent to an EMA with |
| 119 | + $\alpha = K$ determined by the noise ratio; EMA is the optimal estimator for a random-walk |
| 120 | + signal in Gaussian noise. |
| 121 | + |
| 122 | +## References & Further Reading |
| 123 | + |
| 124 | +- S. W. Smith, *The Scientist and Engineer's Guide to Digital Signal Processing*, Ch. 19 |
| 125 | + (Recursive / single-pole filters). Available free at dspguide.com. |
| 126 | +- A. V. Oppenheim & R. W. Schafer, *Discrete-Time Signal Processing*, 3rd ed., Ch. 5 (IIR filter |
| 127 | + design). |
| 128 | +- R. G. Lyons, *Understanding Digital Signal Processing*, 3rd ed., Ch. 6 (recursive filters). |
0 commit comments