minikalman lab

Kalman filters on the bench

Every trace on this page is computed by minikalman, the Rust filter library for microcontrollers, compiled to WebAssembly and running in your browser. Turn the knobs, watch the estimates, read the numbers.

Smoothing a noisy position

regular::RegularKalman, 2 states, 1 measurement

A linear filter tracks position and velocity from noisy readings. Drag across the plot to draw your own signal; the filter reruns as you draw.

What you're seeing: low process noise smooths more but lags behind sudden changes; high process noise follows quickly but lets more of the sensor noise through.

Read the filter in lib.rs, and the docs of the regular module.

position (m)
  • true position
  • measurement
  • estimate ±2σ
Measurement RMS error
—
Estimate RMS error
—

EKF vs UKF: tracking with range and bearing

extended::ExtendedKalman and unscented::UnscentedKalman, 4 states, 2 measurements

Radars measure range and bearing to a moving target. Range noise spreads a measurement along the line of sight; bearing noise spreads it sideways, more so the further away the target is. Together they make a crescent. The extended filter approximates it with a Gaussian by linearizing at its estimate, the unscented filter by pushing sigma points through the measurement function. Between measurements both only predict and their ellipse grows; each correction (a dot on the track) shrinks it again.

Behind a radar the bearing jumps from +180° to −180° (the dashed line). With wrap bearing residuals on, both filters use angles::wrap_pi to see that as the small step it is.

What you're seeing: far from the radar the crescent is strongly curved, and that is where the two Gaussian approximations differ most. Turn off wrapping and restart, and both filters are thrown off when the target passes behind the radar. A second radar adds a crescent from another angle; where the two intersect, the ellipses shrink.

Read the experiment in radar.rs, and the radar_2d and radar_2d_ukf examples.

extended Kalman filter
unscented Kalman filter
  • true path
  • radar 1 measurement
  • radar 2 measurement
  • EKF, 2σ ellipse
  • UKF, 2σ ellipse

Target

Outliers: trust, reject or down-weight

correct, set_gate, correct_robust with RobustWeight::StudentT

A sensor reads a moving position with Gaussian noise, and some readings are gross outliers. Three filters with the same model see the same readings. The plain one believes every reading. The gated one rejects readings whose normalized innovation squared (NIS) exceeds the gate. The robust one models the noise as Student-t and down-weights a reading the further it is from the prediction.

All three assume constant velocity with random acceleration. The true path is a smooth swing that this model can follow, plus, if you turn up target jump, a sudden step at 15 s that no motion model predicts.

What you're seeing: with the default knobs, both the gate and the Student-t weight cut the error of the plain filter to about a third, and the gate rejects almost exactly the injected outliers. Now set target jump to 3 m. The plain filter follows the step (and keeps flinching at every outlier). The robust filter distrusts the first readings after the step, its uncertainty grows, and within about a second it follows too. The gated filter sees a NIS far above its gate for every reading after the step, rejects them all and coasts on its model, drifting away: a gate lockout, visible in the NIS trace below. Turn on re-acquire: after that many rejections in a row it inflates its covariance 100-fold, the next reading passes, and it snaps back. Set it too low with many outliers and bursts of outliers trigger it too.

Read the experiment in outliers.rs, and the docs of correct_robust and set_gate.

position (m)
  • truth
  • reading
  • plain
  • gated
  • robust
  • outlier injected
  • rejected by gate
gated filter: NIS of each reading against the gate (log scale)
  • NIS, accepted
  • NIS, rejected
  • gate
  • re-acquired
robust filter: Student-t weight w of each reading (1 = plain Kalman update)
Plain RMS error
—
Gated RMS error
—
Robust RMS error
—
Clean-reading RMS error
—
Rejected by gate / true outliers
—
Mean weight: outliers / clean
—
Gate re-acquisitions
—

Numerics: when the variance drops below one bit

CovarianceUpdate::Standard vs Joseph, in f32, I16F16 and I32F32

Ten precise position sensors at 18° steps update a 2D tracker one after another. That drives the position variance in P towards about 10⁻⁵, below the smallest step of Q16.16 fixed point (2⁻¹⁶ ≈ 1.5·10⁻⁵). The same run is computed six times: three number types, each with the short update P − K·H·P (solid) and the Joseph form (dashed).

What you're seeing: f32 and I32F32 stay positive definite with either form; the Joseph form keeps P exactly symmetric, the short form leaves rounding asymmetry. In I16F16 the short form loses definiteness in a few steps but keeps tracking, and the Joseph form does not rescue it: it diverges. The cause is resolution, not the form. Raise R to 10⁻² and Q16.16 is fine again; otherwise use a type with more fractional bits.

Read the experiment in numerics.rs, the crate's numerical robustness bench, and the docs of CovarianceUpdate.

  • f32
  • I16F16
  • I32F32
  • standard
  • Joseph
  • I16F16 P not positive definite
smallest diagonal entry of P (log scale)
asymmetry of P after correction
position error (m)
Measured over the run
VariantP not positive definiteLargest asymmetryFinal error

Smoothing: hindsight through a gap

smoother::RtsSmoother, smoother::FixedLagSmoother

A sensor goes quiet for a few seconds while the target keeps turning. The filter knows only the past, so in the gap it coasts in a straight line and its uncertainty grows. The fixed-lag smoother refines each step with the next 1.5 s of readings, online. The RTS smoother runs backwards over the recorded run, so every step also sees the readings after the gap.

What you're seeing: in a 3 s gap the filter misses the target by several metres; the RTS smoother bends its estimate through the gap to meet the readings on the other side, with a fraction of the error and a much tighter ±2σ band. Outside the gap, smoothing still roughly halves the error. A smoother needs the future, so use it for logs and calibration runs, or accept the fixed lag.

Read the experiment in smoothing.rs, and the docs of the smoother module.

position (m)
  • truth
  • reading
  • filter ±2σ
  • fixed lag (1.5 s)
  • RTS smoother ±2σ
estimate error (m)
Filter RMS error
—
Fixed-lag RMS error
—
RTS RMS error
—
Filter error in the gap
—
RTS error in the gap
—
Largest σ in the gap: filter / RTS
—

Parameter estimation: two estimators or one

rls::RecursiveLeastSquares + regular::RegularKalman vs extended::ExtendedKalman

A motor's speed follows xₖ₊₁ = a·xₖ + b·uₖ plus noise, and at step 400 the load changes: (a, b) jumps from (0.9, 0.5) to (0.7, 1.0). The dual estimator identifies (a, b) with recursive least squares and feeds them to a one-state Kalman filter. The joint estimator appends a and b to the state and runs one extended Kalman filter on [x, a, b], whose covariance links every speed residual to the parameters.

What you're seeing: after the load change the joint filter re-identifies the plant in 25 steps, the dual one in 82, and with this seed the joint one also settles closer. The dual estimator has the lower speed error while the model holds. Both have a memory knob: set q to 10⁻⁸ and the joint filter stops learning; set μ to 1 and the dual one does.

Read the experiment in parameters.rs, the augmented_state and rls_adaptive examples, and the docs of rls.

  • true value
  • dual RLS + KF ±2σ
  • joint EKF ±2σ
pole â
gain b̂
speed error x̂ − x
Dual: steps to re-identify
—
Joint: steps to re-identify
—
Dual: â, b̂ RMS error, settled
—
Joint: â, b̂ RMS error, settled
—
Dual: speed RMSE before / after
—
Joint: speed RMSE before / after
—

Attitude: when the accelerometer lies

extended::ExtendedKalman as an error-state filter on a quaternion, set_gate

An IMU's attitude as a small flight controller would estimate it: the gyro rotates a nominal quaternion at 200 Hz, and a six-state f32 filter estimates only the small error in attitude and gyro bias. The accelerometer corrects the tilt, because at rest it measures gravity; a heading source corrects the yaw.

A minute of motion: rest, a tumble, a 15 s turn with 3 m/s² sideways acceleration, a shake, slow drift. While the body accelerates, the accelerometer points the wrong way. The gated filter rejects tilt updates whose NIS exceeds the gate and then waits a moment. The window filter uses the common rule instead: accept when |a| is within ±10 % of g.

What you're seeing: in the turn, |g + a| is only 4.6 % above g, so the window accepts every sample while their direction is 17° off vertical; the window filter is dragged tens of degrees off and does not recover. The gate sees the direction: it skips nearly all dynamic samples and keeps the error around a degree. Scale the acceleration up to 2 and the window starts to work, since |a| now leaves it. Open the gate wide and the gated filter suffers too. Turn the heading off and the yaw drifts with the gyro bias.

Read the experiment in attitude.rs, the full attitude_eskf example, and the docs on filtering on manifolds.

truth
gated
window
attitude error (°)
roll (°)
true linear acceleration |a| (m/s²) and the tilt-update decisions
  • truth
  • gated
  • window
  • gate rejected
  • held off
  • window accepted a moving sample
Gated: RMS / largest error
—
Window: RMS / largest error
—
Moving samples the gate skipped
—
Still samples the gate rejected
—
Moving samples the window took
—
Mean NIS of used tilt updates (expect 3)
—