% kalman_demo.m - simple 2D constant-velocity Kalman filter dt = 0.1; % time step T = 20; % total time (s) N = T/dt;
The Kalman filter is a mathematical algorithm used to estimate the state of a system from noisy measurements. It's a powerful tool for a wide range of applications, including navigation, control systems, and signal processing. In this guide, we'll introduce the basics of the Kalman filter and provide MATLAB examples to help you get started.
Let's consider a simple example where we want to estimate the position and velocity of an object from noisy measurements of its position.
The Kalman Filter is often treated like a "black box" of complex math, but at its heart, it is an elegant algorithm for finding the truth in a world full of noise. Whether you are tracking a satellite or just trying to smooth out GPS jitter on a smartphone, the Kalman Filter is the industry standard for state estimation. What is a Kalman Filter?
Calculates how much to trust the measurement. Updated State Estimate ( x̂k|kx hat sub k vertical line k end-sub ): The final refined guess. Updated Error Covariance ( Pk|kcap P sub k vertical line k end-sub ): The updated uncertainty. 4. Kalman Filter MATLAB Examples
% --- Calculate RMS Error --- pos_error_kf = sqrt(mean((x_hist(1,:) - x_true(1,:)).^2)); pos_error_meas = sqrt(mean((measurements - x_true(1,:)).^2)); fprintf('RMS Position Error:\n'); fprintf(' Raw Measurements: %.3f m\n', pos_error_meas); fprintf(' Kalman Filter: %.3f m\n', pos_error_kf); fprintf('Improvement: %.1f%%\n', (1 - pos_error_kf/pos_error_meas)*100);
If you’ve ever wondered how a GPS keeps track of your car even when you drive under a bridge, or how a rocket stays on course despite wind gusts, you’re looking at the in action.
The examples above focus on the , which assumes a linear system. But what if your system is nonlinear, like the angle of a pendulum or the trajectory of a satellite? This is where the extensions come in.
In this scenario, our system state (x) is simply the voltage. Since the voltage is constant, our state transition matrix (A) is 1, and we have no control input (B u). The measurement matrix (H) is also 1 because we directly measure the voltage.
: An interactive project with live scripts and 3D simulations of a pendulum system.
5. MATLAB Example 2: Tracking a Moving Object (Position & Velocity)
The is an optimal estimation algorithm that calculates the state of a system (like the position or speed of a drone) by blending noisy sensor measurements with a mathematical prediction. How It Works: The Predict-Correct Cycle
% 1D Kalman Filter Implementation for Constant Voltage Tracking clear; clc; close all; % 1. Simulation Parameters nSamples = 100; trueVoltage = 1.2; % True constant voltage measuredNoiseStd = 0.2; % Standard deviation of measurement noise % Generate fake noisy sensor data rng(42); % Set random seed for reproducibility measurements = trueVoltage + measuredNoiseStd * randn(1, nSamples); % 2. Kalman Filter Initialization Q = 1e-5; % Process noise covariance (assumed very low since voltage is constant) R = measuredNoiseStd^2; % Measurement noise covariance xtilde = 0; % Initial state guess (0V) P = 1; % Initial error covariance guess % Allocations for plotting estimatedHistory = zeros(1, nSamples); % 3. Kalman Filter Loop for k = 1:nSamples % --- Predict Phase --- % State doesn't change over time (A = 1, B = 0) xtilde_minus = xtilde; P_minus = P + Q; % --- Update Phase --- % Measurement directly maps to state (H = 1) K = P_minus / (P_minus + R); % Kalman Gain xtilde = xtilde_minus + K * (measurements(k) - xtilde_minus); P = (1 - K) * P_minus; % Save result estimatedHistory(k) = xtilde; end % 4. Visualization figure('Position', [100, 100, 800, 400]); plot(1:nSamples, measurements, 'r.', 'MarkerSize', 10); hold on; plot(1:nSamples, estimatedHistory, 'b-', 'LineWidth', 2); yline(trueVoltage, 'g--', 'LineWidth', 2); xlabel('Sample Iteration'); ylabel('Voltage (V)'); title('1D Kalman Filter: Constant Voltage Tracking'); legend('Noisy Measurements', 'Kalman Filter Estimate', 'True Value'); grid on; Use code with caution. Explanation of the 1D Results
Before filtering, we describe our system using two primary equations: (How the system moves) Measurement Equation: (What our sensors see) is the Process Noise (covariance is the Measurement Noise (covariance Phase 1: Predict (Time Update)