Signals & Noise
Easydifficulty
Before building a filter it helps to know what's wrong with the data, because different problems need different fixes.
Every sensor reports the truth plus garbage
A signal is a value that changes over time: an arm angle, the distance to a wall, a wheel's speed. You never see it directly. You see a sensor reading, which is the true value plus some error. Feed the raw reading to a controller and the error comes along with it. A PID derivative term turns jitter into motor chatter, a single spike from a distance sensor triggers an autonomous transition too early, and a small bias slowly steers odometry off course.
measurement = true value + noise
Say a distance sensor is pointed at a wall exactly 50.0 cm away. Here are four ways its readings can go wrong:
- Jitter: 49.8, 50.1, 50.0, 50.2, 49.9. Small, fast, random wobble around the truth. This is the common case, and averaging helps.
- Spikes: 50.0, 50.1, 212.0, 49.9. One wild reading from a loose connector, a reflection, or a glitch.
- Bias: 51.5, 51.4, 51.6, 51.5. Steady, but off by the same amount every time. Averaging can't see it, only calibration can.
- Drift: 50.0, then 50.4 a minute later, then 51.1. The error grows as small mistakes add up. Averaging doesn't help here either. You need a second sensor that doesn't drift.
A healthy encoder's position count is exact. It can miss or double-count edges if the wiring is bad, but it isn't noisy the way an analog sensor is, and filtering it just adds lag. Encoder velocity is different: computing it from the difference between two counts turns one tick of rounding into a visible jump. The signals really worth filtering have roughly bell-shaped noise: analog sensors like distance sensors, analog absolute encoders, and current sensors, and vision, where a tag's reported position jitters from frame to frame. Those are the examples this module uses.
The graph gives jitter, drift, and spikes each a slider so you can see their shapes separately. Jitter scatters the amber readings evenly on both sides of the truth, so averaging cancels it. Drift doesn't scatter anything. It moves the center itself, so the dashed line walks away from the truth and no amount of averaging brings it back. A spike is one absurd reading that an average swallows and stays wrong about for a while. That's why the lessons ahead are a set of tools rather than one filter: smoothing handles jitter, combining two sensors handles drift, and rejecting impossible readings handles spikes. (Bias you fix by calibrating.)
Sampling, and what noise looks like
A Control Hub doesn't read sensors continuously. It samples them, tens to hundreds of times a second (see From Math to a Loop for why the rate matters). Every filter in this module works on that stream of samples and tries to answer one question: given the readings so far, what's the best guess at the true value?
The demo shows a known true signal in green and a noisy sensor reading it in amber. Turn the noise up until you can't see the green line in the dots, then imagine trying to control a mechanism from the dots alone.
You can still make out the green wave through the scatter because your eyes average the dots together. The next lessons give the Control Hub a way to do the same, in a line or two of code.
Measure your sensor before you filter it
Start by measuring how noisy the sensor actually is. Hold the mechanism still, take a batch of readings, and work out how far they typically sit from their average. That number, the standard deviation, tells you how much filtering you need.
Try it by hand on the jitter readings from above: 49.8, 50.1, 50.0, 50.2, 49.9 cm. The average is
50.0. The distances from it are −0.2, 0.1, 0, 0.2, and −0.1. Square them, average the squares
(), and take the square root: about 0.14 cm. The code below does the same thing
one reading at a time, without storing the readings.
private static final int N = 200; // samples to collect
private int count;
private double mean;
private double m2;
// Hold the mechanism perfectly still and call this once per loop. Anything
// the sensor reports now is noise, because the truth is not moving.
void update(double x) {
if (count >= N) return; // done; never block waiting for more
count++;
double delta = x - mean;
mean += delta / count;
double delta2 = x - mean; // recomputed AFTER the mean update
m2 += delta * delta2;
if (count == N) {
double std = Math.sqrt(m2 / count);
report(mean, std);
}
}
This is Welford's algorithm. The obvious shortcut is to keep a running and and
compute the variance as (average of the squares) minus (square of the average). Try that with readings
around 3000 that vary by about 0.5, in both Both terms come out around 9,000,000, and the answer you want, about 0.25, is their tiny difference. A
float and double. What goes wrong?Show solution
double keeps about 15 to 16 digits, so it still gets this one right. A float keeps about 7, so the
difference can vanish entirely. Even a double runs into trouble when the average is much bigger than
the spread. Welford never subtracts two big, nearly equal numbers, so it doesn't have the problem.
delta uses the average from before the update and delta2 the average from after. That's
deliberate. Check it by running three readings through by hand and comparing with the ordinary variance
formula.
A large standard deviation means heavier smoothing, or a Kalman filter, is worth it. A small one means a light touch will do. It's also the number a Kalman filter's is built from. Measure it for each sensor as it's actually mounted, rather than copying a number from somewhere. Next up is the simplest filter that works well: the low-pass.
- WPILib: State Observers and Kalman Filters - robotics context for noisy measurements, variance, and estimation.
- Tyler Veness, Controls Engineering in FRC - practical controls background for sensor noise and filtering.
- From Math to a Loop - Control Lab's sampling and loop-time prerequisite for every filter in this module.