# Module 5.3: Kalman and Extended Kalman Filters (EKF)

- Course: [Course 5: Sensors, State Estimation & Localization (SLAM)](https://roboticsconcepts.dev/courses/sensors-state-estimation-slam.md)
- HTML Page: https://roboticsconcepts.dev/courses/sensors-state-estimation-slam/kalman-and-extended-kalman-filters/
- Key Concepts: Gaussian covariance ellipses, Kalman gain & sensor weighting, Nonlinear motion linearization (EKF)

Tracking a full arbitrary probability map over a 3D room would melt a small onboard computer in milliseconds. The Kalman filter makes a clever practical bargain: what if we assume our belief is always a clean bell curve — a Gaussian — described completely by a mean pose and a covariance matrix?

## Ellipses as a compact language for uncertainty

In 2D, a Gaussian belief over (x, y) draws as an ellipse. The center of the ellipse is our mean estimate. A tiny tight circle means we're confident to within a couple centimeters; a long, stretched-out oval means we know roughly which lane we're in, just not how far forward we've rolled.

That ellipse is encoded by a small covariance matrix P. When we fuse two noisy sensors — say, shaky wheel odometry and a range-bearing beacon — the Kalman filter computes an optimal blend called the Kalman Gain K.

- When process noise Q is large and measurement noise R is tiny, Kalman Gain K leans toward 1: trust the new landmark sighting and snap the ellipse onto it.
- When the sensor is noisy (R is large) and our motion model is solid (Q is small), K stays small: nudge the estimate gently instead of chasing sensor jitter.

## Handling curves with the Extended Kalman Filter

The textbook linear Kalman filter only works when motion and sensors are straight-line matrix multiplications. Real mobile robots steer in arcs using sines and cosines, and range-bearing beacons involve square roots and arctangents. Push a Gaussian through a curve and it bends into a banana shape instead of a clean ellipse.

The Extended Kalman Filter (EKF) solves this pragmatically: at each step, it takes the local tangent plane — the Jacobian matrix — around the current mean pose, propagates the ellipse along that local linear approximation, and runs the exact same Kalman Gain math.

## Interactive Sandbox: 2D Extended Kalman Filter Uncertainty Ellipse Visualizer

Click Step Both a few times to trace the curved path, then drag the Process Noise (Q) and Measurement Noise (R) sliders. Watch how the Kalman Gain matrix K shifts weight between motion and beacons, and watch the 2σ ellipse stretch and snap.

## Where the EKF shines — and where it cracks

Try cranking Process Noise Q way up while turning Landmark dropout ON, then turning dropout back OFF. Notice how quickly the filter recovers once a single clean beacon reading arrives — that's the magic of tracking cross-covariances between x, y, and heading θ.

The catch is that an EKF can only represent one unimodal bell curve at a time. If your robot genuinely has no idea whether it's in the north hallway or the identical south hallway, a single Gaussian ellipse will awkwardly average the two and land in the solid wall between them.

## EKF Predict, Kalman Gain, and Update

```text
P̄ = F P Fᵀ + Q   |   K = P̄ Hᵀ (H P̄ Hᵀ + R)⁻¹   |   x = x̄ + K(z − h(x̄))
```

F and H are the local Jacobians of motion and sensing. K automatically balances our predicted covariance P̄ against sensor noise R.

## Check Your Intuition

**Question:** Your robot's laser beacon rangefinder gets covered in dust, making its measurement noise R ten times larger than usual. How does the Kalman filter respond?

- [✓] Kalman Gain K drops closer to zero, relying more on the motion prediction and ignoring wild laser spikes
- [ ] Kalman Gain K jumps to 1.0 to trust the dusty sensor more
- [ ] The filter deletes the motion model completely

**Explanation:** Because K = P̄ Hᵀ (H P̄ Hᵀ + R)⁻¹, making R huge shrinks K, automatically down-weighting noisy measurements.

## Where You'll See This in Modern Robotics

Error-State EKFs (ES-EKF) and factor-graph smoothers are still workhorses in drones, legged robots, and autonomous cars whenever you need rock-solid 200+ Hz state estimates from IMU, encoders, and GPS/vision.

## Companion Python Notebook: 03 · Extended Kalman Filter (EKF) Sensor Fusion Lab

Code the EKF Predict/Update loop with motion and measurement Jacobians and plot 2σ covariance ellipses around landmark beacons.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-5/03_kalman_and_ekf_fusion.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-5/03_kalman_and_ekf_fusion.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-5/03_kalman_and_ekf_fusion.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-5/03_kalman_and_ekf_fusion.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-5/03_kalman_and_ekf_fusion.py`
