C9 · Lesson 19 · Simple digital filters
Moving averages, exponential smoothing and differences
A temperature logger's readings jitter, and a few lines of code fix it: average the last twenty samples, or blend each new reading into a running estimate. A strain gauge drifts, and subtracting each sample from the last removes the drift. These filters are so simple that they're often written without anyone calling them filters, yet each has a frequency response that says exactly what it keeps and what it throws away. Knowing it turns "average a few samples" into a design: pick the length that cancels mains hum exactly, or the smoothing factor that gives the cutoff you need.
- Two low-pass filters, plotted from ω = 0 to π (half the sample rate): an M-point moving average, and an exponential smoother .
- The moving average has exact nulls at multiples of 2π/M, and large side lobes between them. Change M and watch the nulls move.
- The smoother has no nulls but falls smoothly and needs only one multiply and one stored number. Smaller α smooths more.
Predict
Data sampled at 1 kHz carries 50 Hz hum. How many samples should a moving average span to remove the hum completely?
20: exactly one period of 50 Hz at 1 kHz. Averaging over a whole cycle of a sinusoid gives zero, so the null at (in hertz, = 50 Hz) lands right on the hum, and its harmonics at 100, 150 Hz… fall on the other nulls.
Name it
Moving average
, an FIR filter with
Gain 1 at DC, nulls at , or in hertz. It's the best filter for reducing random noise while keeping a step sharp, and a poor one for separating frequencies: its first side lobe is only about 13 dB down.
Exponential smoother
, with , the discrete RC low-pass:
DC gain 1, gain at ω = π, and an impulse response that falls by 1/e in samples, about 1/α for small α. One multiply-add and one stored value per sample.
First difference
has : 0 at DC, 2 at ω = π. It removes offsets and slow drift, and approximates a derivative (divided by the sample period).
Hertz and radians
A sample rate maps ω to ; ω = π is , the highest frequency the samples can hold.
Worked example
A 20-point moving average at 1 kHz removes 50 Hz. How much does it reduce a 10 Hz signal?
Show the solution
- Digital frequency.
- Gain.
- Result. So the 10 Hz signal keeps 94% of its size while the hum is gone.
Answer it passes 10 Hz with a gain of 0.936, keeping 94% of its size.
Worked example
An exponential smoother uses α = 0.1. Describe it.
Show the solution
- Gain at DC. It's 1.
- Gain at half the sample rate. So it cuts the fastest jitter by about 19 times.
- Memory. The impulse response falls by 1/e in
Answer it's a low-pass with gain 1 at DC and 0.053 at half the sample rate, and a memory of about 9.5 samples.
Worked example
A first difference, , runs at 1 kHz. What is its gain at 10 Hz and at 250 Hz?
Show the solution
- Gain of a first difference.
- At 10 Hz. Here ω = 0.0628 and the gain is 0.0628: low frequencies almost vanish.
- At 250 Hz. Here ω = π/2, so the gain is
- What it is. It's a crude high-pass, and at low frequencies a differentiator: the gain grows in proportion to frequency.
Answer a gain of 0.0628 at 10 Hz and 1.414 at 250 Hz.
Check
A 10-point moving average runs on data sampled at 8000 Hz. At what frequency is its first null?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
What is the gain of an 8-point moving average at ω = 0.2 rad?
Check
An exponential smoother has α = 0.2.
(a)
What is its gain at ω = π?
(b)
In how many samples does its impulse response fall by 1/e?
Check
What is the gain of the first difference at ω = 1 rad?
Check
At a sample rate of 8000 Hz, what ω corresponds to 1000 Hz?
Write units the way you normally would: 1.5 rad and 1500 mrad are the same answer.
Check
Data sampled at 1200 Hz carries 50 Hz hum. How many points should a moving average span to null it exactly?
Check
A pressure sensor's samples have a slowly drifting offset on top of the fast pulses you care about. Which simple filter removes the offset?
Check
A 10-point moving average runs at 1000 Hz.
(a)
What is its gain at 30 Hz?
(b)
At what lowest frequency is its gain zero?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
An exponential smoother uses α = 0.1.
(a)
What is its gain at ω = π?
(b)
In how many samples does its impulse response fall by 1/e?
Explain why
Why does a moving average over exactly one period of a sinusoid remove it completely?
The average of a sinusoid over a whole number of cycles is zero, because its positive and negative halves cancel exactly. A moving average over M samples averages whatever M consecutive samples are in its window, and if M samples span exactly one cycle, the window always holds a complete cycle no matter where it starts, so the sinusoid contributes nothing to any output. In the frequency response that's the null at , and the same argument puts nulls at every harmonic, which complete two, three or more whole cycles in the window.
Practice
Keep practicing
11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.