Skip to main content

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 α\alpha, a number between 0 and 1. Each loop:

x^k=x^k−1+α (zk−x^k−1)\hat{x}_k = \hat{x}_{k-1} + \alpha\,(z_k - \hat{x}_{k-1})

where zkz_k is this loop's reading and x^\hat{x} 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.

αBehaviourCharacter
near 0tiny steps → very smooth, slow, laggya heavy flywheel
near 1big steps → snappy, barely filtersa twitchy needle

Where does "exponential" come from? With α=0.2\alpha = 0.2, the newest reading gets a weight of 0.2. The reading before it has been multiplied by 0.80.8 once since then, so its weight is 0.2×0.8=0.160.2 \times 0.8 = 0.16. The one before that gets 0.2×0.82=0.1280.2 \times 0.8^2 = 0.128, and so on. Every old reading is still in there, with a weight that keeps shrinking by the same factor:

x^k=α zk+α(1−α) zk−1+⋯+(1−α)k+1x^−1\hat{x}_k = \alpha\,z_k + \alpha(1-\alpha)\,z_{k-1} + \cdots + (1-\alpha)^{k+1}\hat{x}_{-1}

The last term is the starting value, x^−1\hat{x}_{-1}, 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 τ\tau, in seconds. Matching the loop-by-loop filter exactly to a smooth exponential gives

α=1−e−Δt/τ,τ=−Δtln⁡(1−α).\alpha = 1 - e^{-\Delta t/\tau}, \qquad \tau = -\frac{\Delta t}{\ln(1-\alpha)}.

After one time constant the filter has covered about 63% of a sudden change. The useful part is that τ\tau is in seconds. A fixed α\alpha of 0.2 means something different at 50 loops per second than at 25, but a τ\tau of 0.08 s means the same thing at any loop rate, as long as you recompute α\alpha from the measured Δt\Delta t every loop.

You'll also see α=Δt/(τ+Δt)\alpha = \Delta t/(\tau+\Delta t). It's an approximation that works fine when the loop is much faster than τ\tau, but the exponential version is exact.

Math explorerWhat α really is: fading weights and a time constant
weight of each past readingα·(1−α)^k — never zero, always fadingnewestoldest63.2 %τresponse to a sudden stepreaches 63 % after τ — the lag you buysamples, newest → oldest · exact relation: α = 1 − exp(−Δt/τ)
time constant τ: 70 mssamples to 63 %: 3.5samples to ~95 % (3τ): 10.4
EMA weights / step responsetime constant τfinal value

The left panel shows how much weight each past reading still has, α(1−α)k\alpha(1-\alpha)^k. The right panel shows the cost: after a sudden step, the output reaches 63% of the change after τ\tau and about 95% after 3τ3\tau. Lower α\alpha and both panels change together. The memory reaches further back, so the output is smoother, and the step response takes longer. Then change Δt\Delta t with α\alpha fixed and watch τ\tau move, which is why the code picks τ\tau and computes α\alpha.

The live demo runs the filter on a noisy signal. Move α\alpha from heavy smoothing to almost none, and use the step test to see the lag.

Live demoExponential low-pass filter
True signalNoisy sensorFiltered output
Left = smoother & slower · Right = snappier & noisier
Time constant τ: —Noise left in output: —
Math
Smoothness costs 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 α\alpha from a chosen time constant and the measured loop time, so the filter behaves the same whatever the loop rate is.

FTCLow-pass a noisy sensor before using it
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
}
ExerciseTen loops by hand

Take α=0.2\alpha = 0.2, 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.

ExerciseWhy alpha is recomputed every loop

A fixed α\alpha like 0.2 makes the filter faster or slower whenever the loop time changes. What does α=1−e−Δt/τ\alpha = 1 - e^{-\Delta t/\tau} give when Δt=τ\Delta t = \tau, and what does it approach as Δt\Delta t gets large? Roughly how much lag does τ=0.08\tau = 0.08 s add?

Show solution

At Δt=τ\Delta t = \tau it gives 1−e−1≈0.631 - e^{-1} \approx 0.63, the 63% from the step test. As Δt\Delta t 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 τ\tau, so about 80 ms here. That's usually fine for a heading hold and usually too much for a derivative term.

Tip
A high-pass filter for free

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.

Sources & references
  • 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.