C12 · Lesson 19 · Filtering in firmware
Moving averages, exponential smoothing and medians in integer arithmetic
Raw ADC readings jitter by a few codes, sensors pick up hum, and a loose connector throws in the odd wild spike. Most embedded programs filter their inputs, and almost all of them use one of three filters: a moving average, an exponential smoother, or a median. Each fits in a handful of integer instructions, each has a delay and a noise reduction you can calculate, and each has a classic integer bug worth knowing before it bites.
- The stems are the step responses of two filters: a moving average of N samples and an exponential smoother with .
- The moving average reaches the new value in exactly N samples, in a straight line. The smoother approaches it gradually and never quite arrives.
- The readouts compare their noise reduction: how much of the input's random noise survives each filter.
Predict
A program smooths ADC readings with y = y + ((x - y) >> 4); in integers. The input sits steady at 1000 and y starts at 0. Where does y end up?
Once is below 16, shifting it right by 4 gives 0, and y stops moving. Here it stalls at 985, 15 codes short. The fix is to keep the smoother's state with extra fraction bits: store and update s += x - (s >> 4), reading y = s >> 4.
Name it
Moving average
. Keep a running sum: add the new sample, subtract the one leaving the window (stored in a ring buffer). It reduces random noise by , delays the signal by samples, and has nulls at multiples of : choosing N so that = 50 or 60 Hz removes mains hum completely. The sum needs bits.
Exponential smoother
, a one-pole low-pass. With the multiply is a shift. Its time constant is samples, it reaches within 1% after samples, and it passes a fraction of white noise's rms. It needs no buffer, only one variable, kept with k extra fraction bits to avoid the stall.
Median
The middle of the last 3 or 5 samples. It ignores an isolated spike completely, where an average smears it over N samples, and it preserves sharp steps. It's not linear, so it has no frequency response to speak of; it's often followed by a smoother.
Worked example
ADC readings have 4 codes of rms noise and arrive at 1 kHz. What do a 16-sample moving average and a k = 4 smoother do to the noise, and what delays do they add?
Show the solution
- Moving average noise.
- Moving average delay. 7.5 samples, which is 7.5 ms at 1 kHz.
- Smoother noise. With it passes a fraction of the noise, so 0.72 codes.
- Smoother time constant. 15.5 samples.
- Compare them. The smoother reduces noise more because its memory is longer: matching its noise needs a moving average of 31 samples.
Answer the moving average leaves 1 code of noise and delays by 7.5 ms; the smoother leaves 0.72 codes, with a time constant of 15.5 samples.
Worked example
Readings arrive at 1 kHz with 50 Hz hum. What moving-average length removes the hum entirely?
Show the solution
- Where the nulls fall. A moving average has nulls at multiples of .
- Put one on the hum.
- Why it works. The average then spans exactly one hum cycle, whose average is zero.
- 60 Hz hum. At 1 kHz, isn't whole; sampling at 1.2 kHz with N = 20 works instead.
Answer a 20-sample moving average.
Worked example
How long must the running sum be for a 64-sample average of 12-bit readings?
Show the solution
- Largest sum.
- Bits needed. That needs 18 bits, so it fits easily in 32 bits.
- A 16-bit sum. It would overflow at a sum of 65,535, which is reached with a reading of about 1024 on every sample.
Answer 18 bits, so a 32-bit running sum holds it easily.
Check
A moving average of 16 samples runs on readings with 2 codes of rms noise, at 1000 Hz.
(a)
How much noise is left?
(b)
How much delay does it add?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
Readings arrive at 1000 Hz with hum at 50 Hz.
(a)
What moving-average length puts its first null on the hum?
(b)
What delay does that average add?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
An exponential smoother uses .
(a)
What is its time constant?
(b)
After how many samples is a step within 1% (whole samples)?
(c)
What fraction of white noise's rms passes?
Check
A running sum averages 64 samples of a 10-bit unsigned ADC.
(a)
What is the largest sum?
(b)
How many bits does the sum need?
Check
A smoother y = y + ((x - y) >> 4) without extra fraction bits starts at 0 with a steady input of 700.
(a)
What is the largest gap at which it stops moving?
(b)
How many fraction bits must the state carry to fix it?
Check
A filter costs 60 cycles per sample on the 10 MHz board and runs at 1 kHz.
(a)
What share of the processor does it use?
(b)
How many such filters could run, at most?
Check
A pressure sensor is read at 100 Hz and occasionally returns a single wild value from a loose connector. Which filter suits it best before display?
Check
Write integer-only filters: moving_average(xs, n) with a running sum (output the floor of the sum over n once n samples have arrived, nothing before), and smoother(xs, k) keeping s = 2^k y (output s >> k after each sample, s starting at 0).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does an exponential smoother written as y += (x - y) >> 4 in integers settle short of a steady input, and how does keeping extra fraction bits fix it?
The shift divides the gap between the input and the output by 16 and throws away the remainder. While the gap is large that's fine, but once it falls below 16 the shifted correction is zero, and the output stops moving, up to 15 codes short of the input. Keeping the state multiplied by 16, as s = 16y, means the update adds the full gap x − y without dividing it first, so even a gap of one code changes s. The division only happens when the output is read, and in steady state s settles where s/16 equals the input.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.