In any autonomous system, you rarely have perfect information. Your internal physics models naturally drift over time, and your hardware sensors always come with a degree of noise. The Kalman filter is a mathematical algorithm that bridges this gap, fusing these two imperfect sources of information to estimate a systemβs true state.
This post demystifies the standard linear Kalman filter from first principles. Using a simple 1D motion model, we will walk step-by-step through the matrix algebra behind the recursive Predict and Update loops. We will translate basic Newtonian kinematics into state vectors, track how uncertainty grows and shrinks using covariance matrices, and mathematically show how the Kalman Gain uses a simple position measurement to automatically correct an unmeasured velocity state.
Introduction
Kalman filter at its core is a recursive bayes estimator (Bayes Filter). This means that the estimation of the state from previous time steps and the current measurements are required to calculate the current state. One of the assumptions while using Kalman Filter are that all the involved probablity distribution are required to be Gaussian Distributions. (For cases following other probablisitic distribution, other filters like Particle Filter, etc can be used.)
At its core the kalman filter has 2 steps ie.
- Prediction
- Correction
Also it can be noted that the Kalman Filter is the optimal solution for linear models and Gaussian distributions.
Perdiction Step
This state involves prediction of the future state using the current state and the control input sent. It projects the state forward in time. The prediction phase asks the question βBased on the last state and the control input recieved, what should be the current state of the system?β.
Therefore we can map it to:
Prediction of the state
where:
- : The mean of the predicted belief one time step later, before incorporating the measurement.
- : A square matrix of size representing the linear state transition. It applies the deterministic version of the state transition function. This can also be described as how will the state change without any control input.
- : The mean of the belief at time .
- : A matrix of size that is multiplied by the control vector. This describes how the control input will change the state from to .
- : The control vector at time .
This equation describes the physics and the control input of the system to predict the next state. Lets assume the exmaple of a 1D car where, each state using 2 variables .
From basic Newtonian physics, assuming constant acceleration over a tiny time step (), the equations of motion are:
- Position:
- Velocity:
To make this computable for a Kalman filter, we pack these two separate equations into a single matrix operation: .
- (State): We stack position and velocity into a vector:
- (Control Input): The known acceleration commanded to the motors:
By mapping the physics to matrices, we extract and :
Predict the Covariance
where
- : The predicted uncertainty. Note that uncertainty grows during the predict step because you are projecting forward without new ground truth.
- : The Process Noise Covariance. This is a crucial tuning parameter. It represents the uncertainty in your physics model.
Continuing the motion example, since the previous state estimate wasnt perfect and the real world doesnβt always obey basic laws of motion. The uncertainty will increase when you predict the future state without looking at the sensors.
The Covariance Matrix () tracks the variance of your state variables. So in this case for a 1D position and velocity state, would be a matrix:
This matrix represents how the errors across velocity and position correlate with each other (represented in the off-diagonals) and how unsure the estimation of the position and velocity are (represented in the diagonals).
Correction Step
In this step, the filter transforms the predicted belief into the desired posterior belief by incorporating the new sensor measurement .
Kalman Gain Computation
where
- : The Kalman gain. It specifies the degree to which the measurement is incorporated into the new state estimate.
- : The measurement matrix. It maps the internal state space to the measurement space.
- : The measurement noise covariance. The distribution of the measurement noise is a multivariate Gaussian with zero mean and covariance .
So for the 1D car, we can assume the sensor to be a GPS sensor (for simplicity we will assume only one sensor). Therefore, we can say that
- Predicted Covariance ():
- Measurement Matrix ():
- Measurement Noise (): A scalar value representing the variance of the GPS error, which we will call .
Note:
Here you can see that we have set as since we are using a position sensor ie. GPS. So we can get
If we use an additional sensor for velocity like a speedometer, we can set as identity matrix.
So now solving for the current scenario we will get
and therefore we can calculate the innovation covariance ie.
Here we can also see that the predicted uncertainty of the GPS is the predicted uncertainty of the position. So now we can just add the sensor noise ie and calculate the Kalman Gain as
(since the innovation covariance is scalar we can just transfer the power to division)
Update the State Estimation
where:
- : The updated mean of the posterior state.
- : The actual measurement received from the sensor.
- : The measurement predicted according to the measurement probability.
- : The deviation of the actual measurement from the predicted measurement. This line adjusts the mean in proportion to both this deviation and the Kalman gain .
So now assume that teh predicted estimate before the measurement was , and the GPS measurement gave us a raw positional reading ie.
Now we can appy teh measurement matrix to our predicted state:
Now we can apply the Kalman Gain and Update
Note
Look at the velocity update equation: . The GPS did not measure velocity. Yet, the filter updates the velocity by taking the position error and scaling it by the velocity gain (). If you were further ahead of your predicted position than expected, the math correctly deduces you must have been moving faster than expected, and fixes the velocity state automatically.
Update the Covariance
Now we can calculate the update as
So the new pose variance becomes
Note
If your GPS is perfect, approaches . Therefore, . Your position variance drops to exactly zero. You are 100% certain of where you are. If your GPS is pure noise, approaches . Therefore, . Your variance remains unchanged (). The filter ignores the bad sensor and retains its original uncertainty.