EKF (Extended Kalman Filter): the algorithm behind sensor fusion
Atlasbot · 17/8/2026 · 11 min read
The Sensor Fusion article mentions robot_localization running "an EKF" but does not explain the internal mechanism. EKF (Extended Kalman Filter) is a specific algorithm that answers the question “how to weight data sources” — by tracking not only the position estimate, but also the uncertainty of the estimate itself.
Key concepts
Unlike just storing a position number (x, y, θ), Kalman Filter stores an additional matrix covariance — representing how "confident" the robot is about the current estimate. Visualization: instead of a single dot on the map, the estimate is a ellipse — small ellipses mean very confident, large ellipses mean uncertain.
How it works
PREDICT (prediction):
Use a motion model (odometry/IMU) to estimate the new position
→ uncertainty ellipse INCREASES further (dead reckoning always adds error)
UPDATE (update):
have new measurements (e.g. matching LiDAR to maps)
→ combining the new measurement with the prediction, the uncertainty ellipsoid CONTRACTS
The two steps repeat continuously: each time new motion data becomes available (predict), the uncertainty increases; Every time there is a new absolute measurement (updated), the uncertainty decreases — this expansion-contraction loop is how the Kalman Filter "auto-weights".
In short: The weight given to the new measurement (called Kalman Gain) is not fixed — it is calculated dynamically based on the ratio between the uncertainty of the prediction and the uncertainty of the new measurement itself.
The original Kalman Filter shows perfect mathematics with a linear system. But the actual motion of the robot has sin(θ), cos(θ) — nonlinearity. EKF is solved by local linearization around the current estimate point at each step, using the Jacobian matrix to approximate the nonlinear system with a "sufficiently approximate" linear system:
Original Kalman Filter: only valid for completely linear systems
EKF: local linear approximation (Jacobian) per step — usable for nonlinear systems
UKF (Unscented KF): another approximation, without needing to calculate derivatives, often more accurate than EKF
Technical details
Formulas
Simplified 1-dimensional form of Kalman Gain — the exact formula behind the "dynamic weighting" explanation in the Principles of Operation section:
K = P / (P + R)
P = prediction uncertainty
R = noise of new measurement (measurement)
For example: P = 0.5 (fairly uncertain prediction), R = 0.1 (fairly reliable measurement)
K = 0.5 / (0.5 + 0.1) ≈ 0.83
Estimated_month = Number + K × (Phep_do − Number)
→ K close to 1: almost complete confidence in the new measurement
→ K close to 0: almost keeping the prediction, little confidence in the measurement
Parameters
The full state vector that robot_localization tracks in the EKF matches exactly the 15 elements in array _config seen in the Sensor Fusion article:
[x, y, z, roll, pitch, yaw, vx, vy, vz, vroll, vpitch, vyaw, ax, ay, az]
→ 15 state dimensions, each with its own uncertainty in the covariance matrix
Practical applications
In addition to fusion odometry/IMU, this same predict-update algorithm is also the basis of EKF-SLAM — a SLAM approach different from the more common method (particle-filter-based like gmapping): instead of just tracking the robot position, the EKF-SLAM state vector also contains the position of each landmark (landmark point). detected, and gradually developed as the robot discovers new landmarks — each time the robot sees an old landmark again, the update step not only corrects the robot's position but also corrects the estimated position of that landmark, gradually improving both the map and the robot's position at the same time in the same filter.
Practical example
def kalman_1d(prediction, P, measurement, R):
K = P / (P + R)
estimate = prediction + K * (measurement - prediction)
P_new = (1 - K) * P
return estimate, P_new
# Predict: predicted position = 10.0, uncertainty P = 0.5
prediction, P = 10.0, 0.5
# Update: measurement 10.3, measurement noise R = 0.1 (fairly reliable)
measurement, R = 10.3, 0.1
estimate, P_new = kalman_1d(prediction, P, measurement, R)
print(f"Du doan: {prediction} (P={P})")
print(f"Do luong: {measurement} (R={R})")
print(f"Uoc luong sau update: {estimate:.2f} (P moi={P_new:.3f})")
Output:
Du doan: 10.0 (P=0.5)
Do luong: 10.3 (R=0.1)
Uoc luong sau update: 10.25 (P moi=0.083)
The final estimate (10.25) favors the measurement (10.3) more than the prediction (10.0) because the measurement is more reliable (R is smaller than P) — and the uncertainty decreases from 0.5 to 0.083 after the update, exactly the "shrinking ellipse" behavior described in the Key Concepts section.
Common mistakes
- Initial covariance is too small (overconfident) — if the initial covariance estimate is wrong but the initial covariance is too small, Kalman Gain will always favor the "predicted" news (which was wrong to begin with) over the new measurement, causing the filter to converge very slowly to the correct value or never converge at all.
- EKF diverges when the motion is too nonlinear — the linearized approximation using the Jacobian is only accurate in a small neighborhood around the current point — a robot rotating too fast or sudden strong movements can seriously deviate this approximation, causing the estimate to diverge from the true value — this is why UKF is sometimes preferred for robots with strongly nonlinear motion.
- Not tuning the process/measurement noise matrix according to actual data — using arbitrary default values for
P,Rwithout measuring/calibrating according to actual sensor characteristics causes the calculated Kalman Gain to not reflect the true reliability, destroying the entire benefit of sensor fusion. - Expect EKF to handle multi-modal hypotheses — EKF assumes the uncertainty has a single Gaussian distribution (a single "peak") — cannot handle the situation "the robot could be in 1 of 2 very different locations" (eg: just started up, not sure which room it is in) — this is why AMCL (next article) uses a particle filter instead of Kalman Filter for global localization problem.
Conclusion
EKF realizes "automatic weighting" of sensor fusion with an explicit predict-update loop, using Kalman Gain to dynamically calculate based on the relative uncertainty between prediction and new measurement, extended to nonlinear systems via Jacobian linearization. This is the direct mathematical basis of robot_localization. The next article in the section, also the last article of the entire Robotics Fundamentals section, talks about AMCL - a global localization technique using particle filters, which correctly solves the "single hypothesis" limitation of EKF just mentioned.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.