Control systems
Low-pass filter (moving average and EMA)
Also called: smoothing · moving average · exponential moving average · EMA · noise filter
Definition
A low-pass filter lets slow changes through and attenuates fast ones. In firmware the simplest are the moving average and the exponential moving average.
Picture it
Averaging the last few readings cancels random jitter but also delays real changes. An exponential moving average keeps a running estimate and nudges it a fraction α towards each new reading: small α is smooth but sluggish.
The maths
moving average: y = (x_k + … + x_(k−N+1)) / N EMA: y_k = α x_k + (1 − α) y_(k−1)
| Quantity | Unit | Symbol |
|---|---|---|
| ratio | dimensionless | 1 |
Worked example
An EMA with α = 0.2 starts at y = 0 and receives a steady input of 10. What are the first three outputs?
- y1 = 0.2 × 10 + 0.8 × 0 = 2
- y2 = 2 + 0.8 × 2 = 3.6
- y3 = 2 + 0.8 × 3.6 = 4.88
Answer2, 3.6 and 4.88, creeping towards 10.
✓ The 3 numbers in this answer are recomputed from scratch by an automated test.
In Python
alpha = 0.2
y = 0.0
for x in readings:
y = alpha * x + (1 - alpha) * yCommon misconceptions
- Misconception: Filtering always adds lag; a heavily smoothed sensor makes a control loop slower and less stable.
- Misconception: Filtering cannot recover information that was never measured or was aliased.