Sensors & Signal Processing
How a median filter removes spikes from a sensor
A median filter replaces each reading with the middle value of the last few readings. Sort three readings, 409, 1023 and 412, and the middle one is 412: the spike of 1023 sorts to the end and never gets a say. An average of the same three would be 615, dragged far off by one bad reading.
Averages smear spikes
A loose wire or a burst of interference can make a sensor return a wild value now and then, like 1023 once in every eight readings. Averaging doesn't remove such a spike; it spreads it out. With the true value at 409, two spikes in a window of sixteen raise the average to about 486:
If anything is switched on a line above 480, it now flickers, even though the real value is nowhere near.
The median ignores them
A median looks only at the middle of the sorted list. One spike, however huge, lands at an end. Here's the median of the last three readings, with no sorting:
int median3(int a, int b, int c) {
return max(min(a, b), min(max(a, b), c));
}
For a = 409, b = 1023, c = 415: min(a, b) is 409, max(a, b) is 1023, capped at c that's 415, and the bigger of 409 and 415 is 415, the middle value.
Live experiment
Throw the spike out
A loose wire makes one reading in eight a spike to 1023, and the lamp lights above 480. With the knob at 40 %, the raw reading flashes on every spike; the average of 16 smears them in and hovers near 490; the median of three ignores a lone spike, and the glitch filter throws every spike out.
“Ignore the spikes” is lesson 5 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 4 before it first.
Its limits
A median of three survives one bad reading in three. If two spikes land in the same three, the middle one is a spike and it gets through. With spikes one reading in eight, that happens to roughly one median in twenty-five. A median of five survives two bad readings, at the cost of a little more delay. A median also doesn't reduce ordinary random noise as well as an average does, so many designs use a median of three to remove spikes, then an average to smooth what's left.
Where you'll meet it
Ultrasonic distance sensors miss echoes and report wild ranges, and a median of three or five is the standard cure. Image processing uses a two-dimensional median to remove salt-and-pepper noise from photos. GPS receivers, heart-rate monitors and industrial sensors all reject outliers the same way: sort, and trust the middle.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Ignore the spikes” is lesson 5 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 4 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: About 486. Two of sixteen are spikes: (14 × 409 + 2 × 1023) ÷ 16 is about 486. The spike didn't vanish; it moved the average 77 up.
- Why not “1023”? 1023 is the spike itself. Fourteen good readings out of sixteen keep the average far below it.
- Why not “716”? 716 is halfway between 409 and 1023, which would need half the readings to be spikes.
- Why not “409”? 409 is what the average would be with no spikes. Two spikes of 1023 in sixteen readings pull it up.
Show the answer
Answer: Rejects it: 614 away. 1023 − 409 is 614, far over 300. The accepted reading stays at 409 and the lamp stays off.
- Why not “Halves it”? Nothing is halved. A reading too far from the last good one is simply rejected.
- Why not “Averages it in”? This filter doesn't average. It either accepts a reading whole or throws it out.
- Why not “Accepts it”? 1023 is 614 away from the accepted 409, more than double the 300 the filter allows.
Show the answer
Answer: The spike lands at the end of the sorted list and is ignored. The average lets the spike pull the result up a lot. The median only looks at the middle value.
- Why not “It's faster”? Speed isn't why: a median of three is about as quick as an average of three.
- Why not “It doesn't”? It does: an average lets a spike pull the result up, while the median ignores it.
- Why not “It uses more readings”? It uses fewer readings than a typical average, not more. It wins by ignoring the extreme one.
“Ignore the spikes” is lesson 5 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 4 before it first.
Questions people ask
What is a median filter?
A filter that outputs the middle value of the last few readings, once they're sorted. A wild reading always sorts to one end, so it never becomes the output, as long as fewer than half the readings in the window are wild.
Median filter or moving average?
Use a median for spikes and glitches: occasional readings that are completely wrong. Use an average for steady random noise, which it reduces more efficiently. Many sensors use a median of three first, then an average.
How do you find the median of three numbers without sorting?
max(min(a, b), min(max(a, b), c)). The smaller of a and b is one candidate; the larger of a and b, capped at c, is the other; the bigger of the two candidates is the middle value.
Related topics
- How a moving average filter smooths a noisy sensorAverage 16 readings for a quarter of the noise, and know what it costs.
- How an exponential moving average filter worksNudge a running value a fraction of the way toward each reading.
- How to calibrate a sensor with two known pointsTwo known readings fix a straight line: where it starts and how steep it is.