Kalman filter explained
Source
McGee and Schmidt, NASA TM-86847
The Kalman filter estimates altitude and velocity from noisy sensors by repeating two steps: predict with a motion model, then update with a measurement. Kalman published it in 1960; NASA Ames first applied it to Apollo navigation (NASA TM-86847).
01Knowledge
How does a Kalman filter remove sensor noise?
A Kalman filter removes noise by never fully trusting one source: it predicts the next state from a motion model, compares the prediction with a new measurement, and pulls the estimate toward the more reliable of the two. The state is the quantity to estimate, such as altitude or velocity.
Every sensor is noisy. A barometer (baro) reports altitude jumping a few meters around the true value. An accelerometer is smoother, but turning its output into altitude takes two integrations, so small errors add up.
The filter always keeps two things: the estimate x̂ and the uncertainty P (a variance). Thanks to P, the computer knows how far to trust the result. The filter is a standard part of avionics, navigation and flight software, from Apollo to model rocket flight computers; in the guidance, navigation and control (GNC) loop it sits in the navigation block.
How does the predict-update loop of a Kalman filter work?
The Kalman filter loop has two steps per measurement cycle: predict makes uncertainty grow, update makes it shrink. For a one-dimensional state:
- Predict: the new estimate is the old estimate plus the change given by the model. The predicted variance is the old variance plus the model noise Q: P⁻ = P + Q.
- Kalman gain: K is the predicted variance divided by the sum of the predicted variance and the measurement variance R: K = P⁻ / (P⁻ + R), always between 0 and 1.
- Update: the estimate is the prediction plus K times the gap between the measurement z and the prediction: x̂ = x̂⁻ + K · (z − x̂⁻), and P = (1 − K) · P⁻.
The gain K is an automatic trust dial. A very noisy sensor (large R) pushes K toward 0 and the filter nearly ignores the measurement; a poor model (large P⁻) pushes K toward 1 and the filter follows it. The gap z − x̂⁻ is the innovation. With several states the numbers become matrices: P⁻ = F·P·Fᵀ + Q, K = P⁻·Hᵀ·(H·P⁻·Hᵀ + R)⁻¹.
Kalman’s 1960 paper gave a recursive formula. According to NASA TM-86847, Stanley Schmidt’s group at NASA Ames split it into a time update and a measurement update, a step the authors call major and critical.
A 1D example: how do you blend an accelerometer with a barometer?
Here the Kalman filter blends a 105 m prediction with a 108 m measurement into 106.07 m, and the standard deviation drops from 2.24 m (prediction) and 3 m (measurement) to 1.79 m. CosmicAxis computed every number.
Source
CosmicAxis own calculation
The previous step estimated 100 m, variance 4 m²; velocity is 50 m/s and the cycle is 0.1 s.
- Predict: x̂⁻ = 100 + 50 × 0.1 = 105 m; model noise Q = 1 m², so P⁻ = 5 m².
- Measure: the baro reads z = 108 m with noise R = 9 m².
- Gain: K = 5 / (5 + 9) = 0.357.
- Update: x̂ = 105 + 0.357 × 3 = 106.07 m; P = (1 − 0.357) × 5 = 3.21 m².
| Step | Altitude (m) | Variance (m²) | Source |
|---|---|---|---|
| Previous estimate | 100 | 4 | Example assumption, CosmicAxis, 2026-09-28 |
| Prediction from accelerometer | 105 | 5 | CosmicAxis own calculation, 2026-09-28 |
| Baro measurement | 108 | 9 | Example assumption, CosmicAxis, 2026-09-28 |
| After Kalman update | 106.07 | 3.21 | CosmicAxis own calculation, 2026-09-28 |
The new variance of 3.21 m² is below both sources and equals 1 / (1/5 + 1/9): blending independent sources adds their confidence.
Why does an IMU need GPS or another sensor to correct it?
An IMU needs an outside measurement because its error grows without bound over time. An inertial measurement unit (IMU) contains accelerometers and gyroscopes; to get position you integrate acceleration twice. A constant bias b = 0.05 m/s² causes a position error of ½ · b · t²: 2.5 m after 10 s but 90 m after 60 s (CosmicAxis own calculation).
GPS or a baro does not drift, but it is noisier, slower, and sometimes lost. Sensor fusion with a Kalman filter uses the IMU for a high-rate predict step and the slow source for the update step. The C-5A transport aircraft blended inertial data with other navigation equipment this way; per NASA TM-86847, it was the first real-time aircraft application the two authors knew of.
02Practice
How can a flight computer estimate model rocket altitude?
A model rocket flight computer estimates altitude with a two-state Kalman filter (altitude, velocity): the accelerometer drives the predict step and the baro drives the update step. This is the practice level before the CubeSat, where the same algorithm determines satellite attitude.
The code simulates 5 s of flight: 2 s of thrust at 30 m/s², then coasting, with accelerometer noise 0.5 m/s² and baro noise 3 m. On an Arduino, swap the simulated numbers for sensor readings.
import random
random.seed(1)
DT = 0.05 # s, 20 Hz
Q = 0.5 ** 2 # accelerometer noise, (m/s²)²
R = 3.0 ** 2 # baro noise, m²
def kalman(h, v, P, a, z):
# Predict with the accelerometer, then correct with the baro
h, v = h + v * DT + 0.5 * a * DT**2, v + a * DT
p00 = P[0][0] + DT * (P[0][1] + P[1][0]) + DT**2 * P[1][1] + Q * DT**4 / 4
p01 = P[0][1] + DT * P[1][1] + Q * DT**3 / 2
p11 = P[1][1] + Q * DT**2
k0, k1 = p00 / (p00 + R), p01 / (p00 + R)
y = z - h
h, v = h + k0 * y, v + k1 * y
P = [[(1 - k0) * p00, (1 - k0) * p01],
[p01 - k1 * p00, p11 - k1 * p01]]
return h, v, P
h_true = v_true = 0.0
h, v, P = 0.0, 0.0, [[1.0, 0.0], [0.0, 1.0]]
for i in range(100):
a_true = 30.0 if i < 40 else -9.81
h_true += v_true * DT + 0.5 * a_true * DT**2
v_true += a_true * DT
a_meas = a_true + random.gauss(0, 0.5)
z = h_true + random.gauss(0, 3.0)
h, v, P = kalman(h, v, P, a_meas, z)
print(f"true {h_true:.1f} | baro {z:.1f} | Kalman {h:.1f}")
# true 195.9 | baro 199.2 | Kalman 196.5
On the CosmicAxis machine (Python 3.14, seed 1), the root-mean-square (RMS) error from second 1 to second 5 is 3.10 m with the baro alone and 0.57 m after the filter. This is optimistic because the simulation uses the exact Q and R; with real sensors, measure noise at rest to choose R. Two cautions for real hardware:
- An accelerometer measures specific force: at rest the vertical axis reads 1 g, and in free fall it reads 0 (weightlessness). Subtract gravity before the predict step.
- At this level the computer only records data and deploys the parachute; software splits reading, filtering, and logging into periodic tasks, usually on a real-time operating system (RTOS). Fly only on certified commercial motors, following NAR/Tripoli rules.
03Reality
How did the Kalman filter originate and reach Apollo?
The Kalman filter originated in 1960 in “A New Approach to Linear Filtering and Prediction Problems” by R. E. Kalman (RIAS, Baltimore), in the Journal of Basic Engineering, volume 82, pages 35–45.
According to McGee and Schmidt (NASA TM-86847, November 1985), in fall 1960 Kalman presented the paper at NASA Ames to the Dynamic Analysis Branch, then only eight researchers. The branch wanted a spacecraft flying around the Moon to navigate itself from the crew’s optical measurements on a small computer; JPL’s least-squares method was too heavy for it. Schmidt’s group added two improvements: relinearizing around the current estimate, which is the extended Kalman filter (EKF), and multiplying the matrices in the order (KH)P instead of K(HP), which is more numerically stable.
By early 1961, simulations on an IBM 704 showed the method was accurate enough for lunar navigation: computing the delta-v vector for each midcourse correction and bringing the spacecraft into the narrow corridor for atmospheric reentry. Schmidt reported the results to Battin at the MIT Instrumentation Laboratory, which was studying Apollo. Potter, working with Battin, then created the first square-root filter, the version used for Apollo because it suits computers with short word lengths.
What does Ariane 5 flight 501 teach about navigation software?
Ariane 5 flight 501 (4 June 1996) shows that an estimator is useless when the software feeding it fails: the rocket broke up 39 s after H0, the Vulcain main engine ignition, because both inertial reference systems (SRI) shut down from one software fault. The report of the Inquiry Board (chairman Jacques-Louis Lions) does not blame a Kalman filter; the fault was in the SRI alignment software.
| Time (s after H0) | Event | Source |
|---|---|---|
| H0 − 3 | SRI enters flight mode; alignment keeps running 50 s, about 40 s after liftoff | Inquiry Board 2.1, viewed 2026-09-28 |
| H0 + 36.7 | Backup SRI shuts down: error converting a 64-bit float to a 16-bit integer | Inquiry Board 2.1–2.2, viewed 2026-09-28 |
| ≈ 0.05 s later | Active SRI shuts down from the same fault, sends a diagnostic bit pattern to the on-board computer | Inquiry Board 2.2, viewed 2026-09-28 |
| H0 + 39 | Nozzles at full deflection, angle of attack above 20°, rocket breaks up and self-destructs | Inquiry Board 2.1, viewed 2026-09-28 |
According to the report, the SRI software was kept almost unchanged from Ariane 4, including an alignment function useful only before launch. Ariane 5 builds up horizontal velocity five times faster, so the variable BH (horizontal bias) exceeded the signed 16-bit integer limit (maximum 32,767). Of seven variables at risk of overflow, only four were protected. The specification required the processor to shut down on an error; two SRIs ran the same software, so both failed together, and the on-board computer treated the diagnostic bit pattern as flight data.
The lesson has three parts. Check every measurement before it enters the filter, for example by comparing the innovation with the expected uncertainty. Test reused software against the new vehicle’s trajectory: Ariane 5 trajectory data was not in the SRI specification. Identical redundant hardware cannot save a systematic software fault, so FMEA-style analysis and the whole-system view of systems engineering matter as much as the algorithm. A crewed spacecraft project must test its guidance software with its own real trajectory.
04Milestone
Milestone: check yourself
If you can do all of these, move on to the next article in this phase.
- Can you recompute the 1D example: prediction 105 m (P⁻ = 5 m²), measurement 108 m (R = 9 m²), K = 0.357, result 106.07 m?
- Can you explain why the variance after the update is smaller than both the prediction variance and the measurement variance?
- Can you run the Python estimator, then change R to see how much the filter trusts the baro?
- Can you retell the Ariane 501 chain of events, from the 64-bit to 16-bit conversion to the breakup?
Frequently asked questions
How is a Kalman filter different from a moving average?
A moving average weights the last N measurements equally, so it lags. A Kalman filter predicts with a motion model, then blends in the measurement with the gain K, so it lags less and reports how far to trust the result.
What is an extended Kalman filter (EKF)?
An extended Kalman filter (EKF) handles nonlinear systems: each step it linearizes the model around the current estimate, then applies the Kalman equations. Schmidt's group at NASA Ames created it for Moon navigation (NASA TM-86847).
Can you use a Kalman filter with an MPU6050 on Arduino?
Yes. The MPU6050 has a gyroscope for the tilt-angle predict step and an accelerometer for the update step. Choose Q and R from the sensor's measured noise.
References
- Kalman — A New Approach to Linear Filtering and Prediction Problems, Journal of Basic Engineering 82(1): 35–45, ASME, 1960 — accessed
- McGee and Schmidt — Discovery of the Kalman Filter as a Practical Tool for Aerospace and Industry, NASA TM-86847, November 1985 — accessed
- Lions et al. — Ariane 5 Flight 501 Failure, Report by the Inquiry Board, 19 July 1996 — accessed
- ESA — Ariane 501: Presentation of Inquiry Board report, 23 July 1996 — accessed
Cite this article
CosmicAxis. (2026). Kalman filter explained: equations, Python example. https://cosmicaxis.space/en/kalman-filter/