Low-Pass Filter
Mediumdifficulty
If a sensor reading is too jumpy to act on, this is the first thing to try. It's one line of code and it removes most of the jitter.
Fast noise rides on top of a slow signal
Real signals usually change slowly and noise changes quickly. A distance-sensor reading or an AprilTag bearing has both, and the fast part causes the trouble, especially for a PID derivative term, which reacts to how fast things change and turns jitter into motor chatter. You want to keep the slow, real changes and drop the fast, noisy ones. Letting the low frequencies pass is where the name comes from.
One step toward each new reading
Start with numbers. Your estimate is 50.0 cm and a new reading comes in at 52.0 cm. Instead of jumping to 52, move 20% of the way there: the gap is 2.0, 20% of it is 0.4, so the new estimate is 50.4. Next loop the reading is 49.0. The gap is now −1.4, 20% of that is −0.28, and the estimate becomes 50.12. One noisy reading only moves the estimate a little, but if the readings really do stay at 52, the estimate keeps closing 20% of the gap every loop and gets there.
That 20% is called , a number between 0 and 1. Each loop:
where is this loop's reading and is the estimate (the hat means "estimated, not measured"). The gap in brackets is sometimes called the innovation: how different the new reading is from what you expected.
| α | Behaviour | Character |
|---|---|---|
| near 0 | tiny steps → very smooth, slow, laggy | a heavy flywheel |
| near 1 | big steps → snappy, barely filters | a twitchy needle |
Where does "exponential" come from? With , the newest reading gets a weight of 0.2. The reading before it has been multiplied by once since then, so its weight is . The one before that gets , and so on. Every old reading is still in there, with a weight that keeps shrinking by the same factor:
The last term is the starting value, , and its weight fades too. That's why the filter should start from the first real reading. Starting from zero makes it spend its first second or so crawling up from a value that was never measured.
Picking α from a time constant
The same fade sets how long the filter takes to catch up with a change. That time is its time constant , in seconds. Matching the loop-by-loop filter exactly to a smooth exponential gives
After one time constant the filter has covered about 63% of a sudden change. The useful part is that is in seconds. A fixed of 0.2 means something different at 50 loops per second than at 25, but a of 0.08 s means the same thing at any loop rate, as long as you recompute from the measured every loop.
You'll also see . It's an approximation that works fine when the loop is much faster than , but the exponential version is exact.
The left panel shows how much weight each past reading still has, . The right panel shows the cost: after a sudden step, the output reaches 63% of the change after and about 95% after . Lower and both panels change together. The memory reaches further back, so the output is smoother, and the step response takes longer. Then change with fixed and watch move, which is why the code picks and computes .
The live demo runs the filter on a noisy signal. Move from heavy smoothing to almost none, and use the step test to see the lag.
More smoothing always means more delay. A low α rejects noise but lags behind, and a high
α responds quickly but lets jitter through. You're choosing how much to trust the newest
reading compared with everything before it, and no setting gets you both.
One line in the loop, tuned to dt
The code is short. The important part is computing from a chosen time constant and the measured loop time, so the filter behaves the same whatever the loop rate is.
private static final double TAU = 0.08; // time constant, seconds
private double filtered;
private boolean seeded;
double update(double raw, double dt) {
if (!Double.isFinite(raw)) return filtered; // never seed on garbage
if (!seeded) {
// The first good sample becomes the state outright, or the filter
// spends a second crawling up from a fake zero.
filtered = raw;
seeded = true;
} else if (Double.isFinite(dt) && dt > 0) {
double alpha = 1 - Math.exp(-dt / TAU);
filtered += alpha * (raw - filtered);
}
return filtered; // hand this to the controller, never raw
}
Take , start at 0, and feed in a constant reading of 10. Write out ten loops of
filtered += alpha * (raw - filtered). How many loops does it take to pass 9?Show solution
2.00, 3.60, 4.88, 5.90, 6.72, 7.38, 7.90, 8.32, 8.66, 8.93. After ten loops it still hasn't reached 9;
loop 11 gets there. At 50 loops a second that's 220 ms before the filter mostly believes a change that
already happened.
A fixed like 0.2 makes the filter faster or slower whenever the loop time changes. What does
give when , and what does it approach as
gets large? Roughly how much lag does s add? At it gives , the 63% from the step test. As gets
large it approaches 1: so much time has passed that you should just take the new reading. The filtered
value trails the truth by roughly , so about 80 ms here. That's usually fine for a heading hold
and usually too much for a derivative term.Show solution
Subtract the filtered value from the raw one, raw − filtered, and you're left with only
the fast changes. That's a high-pass filter, useful for detecting bumps, impacts, or edges.
The low-pass filter weights all past readings with a fading factor. The next one, the moving average, weights the last few readings equally and ignores everything older.
- CTRL ALT FTC - FTC-oriented filtering and controller-tuning context.
- WPILib: Filters - competition-robotics reference for smoothing noisy measurements.
- From Math to a Loop - why the smoothing factor should be chosen with measured loop time in mind.