SimpleKalmanFilter
#SimpleKalmanFilter
A SimpleKalmanFilter smooths a noisy measurement of one quantity. It moves the estimate toward each new measurement by a gain that is recalculated on every update from how uncertain the estimate is compared with the measurement.
This is a simplified single-variable filter. It does not model the motion of the system, so it works best on a value that changes slowly compared with the noise, such as a distance to a wall or a battery voltage.
#Creating a SimpleKalmanFilter
SimpleKalmanFilter filter = new SimpleKalmanFilter(2.0, 2.0, 0.01);| Parameter | Description |
|---|---|
measurementError | How noisy the sensor is, on the same scale as estimateError. A larger value makes the filter trust each measurement less and gives a smoother output. |
estimateError | How uncertain the starting estimate is. A common starting point is the same value as measurementError. |
processNoise | How fast the estimate is allowed to become uncertain again when it moves. A larger value makes the filter react faster. |
The gains and parameters are illustrative and must be tuned for your sensor.
The filter only uses the ratio between estimateError and measurementError, so the units of those two values do not matter as long as they match. None of the three values can be negative, otherwise the constructor throws an IllegalArgumentException.
#Updating the Estimate
double estimate = filter.update(sensor.getDistance(DistanceUnit.CM));The first call returns the measurement unchanged and uses it as the starting estimate. Each later call calculates:
gain = estimateError / (estimateError + measurementError)
estimate = previous + gain x (measurement - previous)
estimateError -- (1 - gain) x estimateError + previous - estimate x processNoiseWhile the measurement stays constant, estimateError shrinks and the gain falls toward zero. The filter then reacts slowly to the next change. A larger processNoise lets the gain recover faster after the estimate moves.
| Method | Description |
|---|---|
update(measurement) | Adds a measurement and returns the new estimate. |
getEstimate() | The last estimate. It is 0.0 before the first update. |
reset() | Makes the next update() start again from its measurement, and restores the starting estimateError. |
After reset(), the filter behaves like a new one, because the starting estimateError is restored.
Última atualização 2026-11-16