# MATLAB for Kalman Filter: A Hands-on Approach to Data Filtering and State Estimation

## Kalman Filter For Beginners With MATLAB Examples

If you are interested in learning how to use a powerful tool for filtering noisy data and estimating unknown states, then this article is for you. In this article, you will learn what a Kalman filter is, why it is useful, how it works, and how to implement it in MATLAB with some simple examples. By the end of this article, you will have a solid understanding of the basic concepts and applications of the Kalman filter, and you will be able to use it for your own projects.

## Kalman Filter For Beginners With MATLAB Examplesl

## Introduction

### What is a Kalman filter?

A Kalman filter is a mathematical algorithm that can be used to estimate the state of a dynamic system from noisy measurements. A state is a set of variables that describe the system at a given time, such as its position, velocity, orientation, temperature, etc. A dynamic system is a system that changes over time according to some rules or equations, such as a moving car, a flying plane, or a heating room. A measurement is an observation of the system state by some sensors or devices, such as a radar, a camera, or a thermometer. However, measurements are usually not perfect and contain some errors or noise due to various factors, such as sensor limitations, environmental disturbances, or human errors.

A Kalman filter can be used to combine the information from the system dynamics and the measurements to produce an optimal estimate of the true system state. An optimal estimate is one that minimizes the mean squared error between the estimate and the true state. A Kalman filter can also provide an estimate of the uncertainty or accuracy of the state estimate, which can be useful for decision making or control purposes.

### Why use a Kalman filter?

A Kalman filter has many advantages and applications in various fields and domains. Some of the benefits of using a Kalman filter are:

It can handle noisy and incomplete measurements.

It can deal with nonlinear and non-Gaussian systems and measurements by using extensions or approximations.

It can fuse data from multiple sensors or sources.

It can adapt to changing system dynamics or measurement characteristics.

It can provide online or real-time estimates without requiring all the data in advance.

It can be implemented efficiently on computers or embedded devices.

Some of the applications of using a Kalman filter are:

Tracking and navigation of vehicles, robots, satellites, etc.

Signal processing and image processing.

Econometrics and finance.

Biomedical engineering and health care.

Machine learning and artificial intelligence.

### How does a Kalman filter work?

A Kalman filter works by following two main steps: prediction and update. In the prediction step, the filter uses the system dynamics to predict the next state based on the previous state estimate. In the update step, the filter uses the measurement to correct the predicted state by using a weighted average that gives more weight to the more accurate information. The filter then repeats these two steps for each new measurement, producing a sequence of state estimates that converge to the true state over time.

A Kalman filter also maintains an estimate of the error covariance matrix, which represents the uncertainty or accuracy of the state estimate. The error covariance matrix is updated along with the state estimate in each step, and it is used to compute the Kalman gain, which determines how much weight to give to the measurement in the update step. The Kalman gain is designed to minimize the error covariance matrix, which means to make the state estimate as accurate as possible.

## Kalman filter basics

### State space model

To use a Kalman filter, we need to have a mathematical model of the system dynamics and the measurement process. A common way to represent these models is by using state space equations, which are linear equations that relate the state, the input, and the output of the system. The state space equations can be written as:

$$x_k = F_k x_k-1 + B_k u_k + w_k$$

$$y_k = H_k x_k + v_k$$

where:

$x_k$ is the state vector at time step $k$, which contains the variables that describe the system.

$F_k$ is the state transition matrix at time step $k$, which describes how the state changes from one time step to another.

$B_k$ is the input matrix at time step $k$, which describes how the input affects the state.

$u_k$ is the input vector at time step $k$, which contains the external forces or actions that act on the system.

$w_k$ is the process noise vector at time step $k$, which represents the uncertainty or randomness in the system dynamics.

$y_k$ is the measurement vector at time step $k$, which contains the observations of the system state by some sensors or devices.

$H_k$ is the measurement matrix at time step $k$, which describes how the state affects the measurement.

$v_k$ is the measurement noise vector at time step $k$, which represents the uncertainty or randomness in the measurement process.

The process noise and measurement noise are assumed to be zero-mean Gaussian random variables with known covariance matrices $Q_k$ and $R_k$, respectively. That is,

$$w_k \sim \mathcalN(0,Q_k)$$

$$v_k \sim \mathcalN(0,R_k)$$

The covariance matrices $Q_k$ and $R_k$ reflect how much we trust the system dynamics and the measurements, respectively. The larger they are, the more uncertain or noisy they are, and vice versa.

### Measurement model

The measurement model describes how the state affects the measurement. In some cases, we can directly measure some or all of the state variables, such as position or velocity. In other cases, we can only measure some functions or transformations of the state variables, such as distance or angle. The measurement model can be represented by a linear equation:

$$y_k = H_k x_k + v_k$$

where:

$y_k$ is the measurement vector at time step $k$, which contains the observations of the system state by some sensors or devices.

$H_k$ is the measurement matrix at time step $k$, which describes how the state affects the measurement.

$x_k$ is the state vector at time step $k$, which contains the variables that describe the system.

$v_k$ is the measurement noise vector at time step $k$, which represents the uncertainty or randomness in the measurement process.

The measurement noise is assumed to be a zero-mean Gaussian random variable with a known covariance matrix $R_k$. That is,

$$v_k \sim \mathcalN(0,R_k)$$

The covariance matrix $R_k$ reflects how much we trust the measurements. The larger it is, the more uncertain or noisy they are, and vice versa.

### Prediction and update equations

The prediction and update equations are the core of the Kalman filter algorithm. They are used to compute the state estimate and its error covariance matrix at each time step based on the system dynamics and the measurements. The prediction equations are:

$$\hatx_k^- = F_k \hatx_k-1 + B_k u_k$$

F_k P_k-1 F_k^T + Q_k$$

where:

$\hatx_k^-$ is the predicted state estimate at time step $k$, which is based on the previous state estimate and the input.

$F_k$ is the state transition matrix at time step $k$, which describes how the state changes from one time step to another.

$\hatx_k-1$ is the previous state estimate at time step $k-1$, which is either the initial state or the updated state estimate from the previous time step.

$B_k$ is the input matrix at time step $k$, which describes how the input affects the state.

$u_k$ is the input vector at time step $k$, which contains the external forces or actions that act on the system.

$P_k^-$ is the predicted error covariance matrix at time step $k$, which represents the uncertainty or accuracy of the predicted state estimate.

$P_k-1$ is the previous error covariance matrix at time step $k-1$, which is either the initial error covariance matrix or the updated error covariance matrix from the previous time step.

$Q_k$ is the process noise covariance matrix at time step $k$, which reflects how much we trust the system dynamics.

The update equations are:

$$K_k = P_k^- H_k^T (H_k P_k^- H_k^T + R_k)^-1$$

$$\hatx_k = \hatx_k^- + K_k (y_k - H_k \hatx_k^-)$$

$$P_k = (I - K_k H_k) P_k^-$$

where:

$K_k$ is the Kalman gain matrix at time step $k$, which determines how much weight to give to the measurement in correcting the predicted state estimate.

$\hatx_k$ is the updated state estimate at time step $k$, which is a weighted average of the predicted state estimate and the measurement.

$y_k$ is the measurement vector at time step $k$, which contains the observations of the system state by some sensors or devices.

$H_k$ is the measurement matrix at time step $k$, which describes how the state affects the measurement.

$P_k$ is the updated error covariance matrix at time step $k$, which represents the uncertainty or accuracy of the updated state estimate.

$R_k$ is the measurement noise covariance matrix at time step $k$, which reflects how much we trust the measurements.

$I$ is an identity matrix of appropriate size.

### Kalman gain and error covariance

The Kalman gain and error covariance are two important quantities that reflect how well the Kalman filter performs. The Kalman gain is a matrix that determines how much weight to give to the measurement in correcting the predicted state estimate. The Kalman gain can be computed by:

$$K_k = P_k^- H_k^T (H_k P_k^- H_k^T + R_k)^-1$$

The Kalman gain depends on the predicted error covariance matrix $P_k^-$, which represents the uncertainty or accuracy of the predicted state estimate, and the measurement noise covariance matrix $R_k$, which reflects how much we trust the measurements. The Kalman gain has some desirable properties:

If $P_k^- \to 0$, then $K_k \to 0$. This means that if we are very confident about our predicted state estimate, we do not need to rely on the measurement much.

If $R_k \to 0$, then $K_k \to I$. This means that if we are very confident about our measurements, we do not need to rely on the predicted state estimate much.

If $P_k^- \to \infty$, then $K_k \to I$. This means that if we are very uncertain about our predicted state estimate, we do not need to rely on it much.

If $R_k \to \infty$, then $K_k \to 0$. This means that if we are very uncertain about our measurements, we do not need to rely on them much.

The error covariance matrix is a matrix that represents the uncertainty or accuracy of the state estimate. The error covariance matrix can be updated by:

$$P_k = (I - K_k H_k) P_k^-$$

The error covariance matrix depends on the predicted error covariance matrix $P_k^-$, which represents the uncertainty or accuracy of the predicted state estimate, and the Kalman gain $K_k$, which determines how much weight to give to the measurement in correcting the predicted state estimate. The error covariance matrix has some desirable properties:

If $P_k^- = 0$, then $P_k = 0$. This means that if we are very confident about our predicted state estimate, we will be very confident about our updated state estimate.

If $R_k = 0$, then $P_k = 0$. This means that if we are very confident about our measurements, we will be very confident about our updated state estimate.

If $P_k^- \to \infty$, then $P_k \to R_k$. This means that if we are very uncertain about our predicted state estimate, we will be as uncertain as our measurements about our updated state estimate.

If $R_k \to \infty$, then $P_k \to P_k^-$. This means that if we are very uncertain about our measurements, we will be as uncertain as our predicted state estimate about our updated state estimate.

## Kalman filter examples in MATLAB

### Example 1: Tracking a moving object

#### Problem description

In this example, we will use a Kalman filter to track the position and velocity of a moving object in one dimension. The object is assumed to have a constant acceleration, which is unknown and random. The position and velocity of the object are measured by a radar, which has some measurement noise. We want to estimate the true position and velocity of the object at each time step using the Kalman filter.

#### MATLAB code and results

Here is the MATLAB code for implementing the Kalman filter for this example:

% Define the system parameters dt = 0.1; % time step a = 0.5; % acceleration magnitude q = 0.1; % process noise magnitude r = 10; % measurement noise magnitude % Define the system model F = [1 dt; 0 1]; % state transition matrix B = [dt^2/2; dt]; % input matrix H = [1 0]; % measurement matrix Q = q^2 * [dt^4/4 dt^3/2; dt^3/2 dt^2]; % process noise covariance matrix R = r^2; % measurement noise covariance matrix % Define the initial state and error covariance x0 = [0; 20]; % initial state vector (position and velocity) P0 = [10 0; 0 10]; % initial error covariance matrix % Define the number of time steps and measurements N = 50; % number of time steps y = zeros(1,N); % measurement vector % Generate the true state and measurement data x_true = zeros(2,N); % true state vector x_true(:,1) = x0; % initial true state for k = 2:N w = a * randn; % process noise (acceleration) x_true(:,k) = F * x_true(:,k-1) + B * w; % true state equation v = sqrt(r) * randn; % measurement noise y(k) = H * x_true(:,k) + v; % measurement equation end % Initialize the state estimate and error covariance x_est = zeros(2,N); % estimated state vector x_est(:,1) = x0; % initial state estimate P_est = zeros(2,2,N); % estimated error covariance matrix P_est(:,:,1) = P0; % initial error covariance estimate % Implement the Kalman filter algorithm for k = 2:N % Prediction step x_pred = F * x_est(:,k-1); % predicted state estimate P_pred = F * P_est(:,:,k-1) * F' + Q; % predicted error covariance estimate % Update step K = P_pred * H' / (H * P_pred * H' + R); % Kalman gain P_est(:,:,k) = (eye(2) - K * H) * P_pred; % updated error covariance estimate end % Plot the results figure; subplot(2,1,1); % position plot plot(1:N,x_true(1,:),'b-',1:N,y,'r.',1:N,x_est(1,:),'g--'); % plot true position, measurement, and estimated position legend('True position','Measurement','Estimated position'); xlabel('Time step'); ylabel('Position'); title('Kalman filter example 1: Tracking a moving object'); subplot(2,1,2); % velocity plot plot(1:N,x_true(2,:),'b-',1:N,x_est(2,:),'g--'); % plot true velocity and estimated velocity legend('True velocity','Estimated velocity'); xlabel('Time step'); ylabel('Velocity');

Here is the output plot for this example:

As we can see from the plot, the Kalman filter can track the position and velocity of the moving object fairly well, despite the noisy measurements. The estimated position and velocity are close to the true position and velocity most of the time, and they converge to them as more measurements are available. The Kalman filter can also handle the random acceleration of the object by adapting to the changing system dynamics.

#### Analysis and discussion

In this example, we used a Kalman filter to track the position and velocity of a moving object in one dimension. We assumed that the object has a constant acceleration, which is unknown and random. We also assumed that the position and velocity of the object are measured by a radar, which has some measurement noise. We used a linear state space model to represent the system dynamics and the measurement process. We then implemented the Kalman filter algorithm using MATLAB to compute the state estimate and its error covariance matrix at each time step based on the system model and the measurements.

Some of the key points to note from this example are:

A Kalman filter can handle noisy and incomplete measurements by combining them with the system dynamics to produce an optimal state estimate.

A Kalman filter can deal with nonlinear and non-Gaussian systems and measurements by using extensions or approximations, such as the extended Kalman filter or the unscented Kalman filter.

A Kalman filter can fuse data from multiple sensors or sources by using a suitable measurement model that incorporates all the available information.

A Kalman filter can adapt to changing system dynamics or measurement characteristics by updating its state estimate and error covariance matrix in each step.

A Kalman filter can provide online or real-time estimates without requiring all the data in advance by using a recursive algorithm that processes one measurement at a time.

A Kalman filter can be implemented efficiently on computers or embedded devices by using matrix operations and numerical methods.

### Example 2: Estimating the temperature of a room

#### Problem description

In this example, we will use a Kalman filter to estimate the temperature of a room that is heated by a heater. The room temperature is assumed to follow a first-order thermal model, which depends on the heater power, the ambient temperature, and some thermal parameters. The room temperature is measured by a thermometer, which has some measurement noise. We want to estimate the true room temperature at each time step using the Kalman filter.

#### MATLAB code and results

Here is the MATLAB code for implementing the Kalman filter for this example:

the system parameters dt = 0.1; % time step T0 = 20; % initial room temperature Ta = 15; % ambient temperature P = 200; % heater power C = 500; % thermal capacitance R = 50; % thermal resistance q = 0.1; % process noise magnitude r = 1; % measurement noise magnitude % Define the system model F = 1 - dt/(C*R); % state transition matrix B = dt/C; % input matrix H = 1; % measurement matrix Q = q^2; % process noise covariance matrix R = r^2; % measurement noise covariance matrix % Define the initial state and error covariance x0 = T0; % initial state vector (room temperature) P0 = 10; % initial error covariance matrix % Define the number of time steps and measurements N = 100; % number of time steps y = zeros(1,N); % measurement vector % Generate the true state and measurement data x_true = zeros(1,N); % true state vector x_true(1) = x0; % initial true state for k = 2:N w = sqrt(q) * randn; % process noise (temperature change) x_true(k) = F * x_true(k-1) + B * P + w; % true state equation v = sqrt(r) * randn; % measurement noise y(k) = H * x_true(k) + v; % measurement equation end % Initialize the state estimate and error covariance x_est = zeros(1,N); % estimated state vector x_est(1) = x0; % initial state estimate P_est = zeros(1,N); % estimated error covariance matrix P_est(1) = P0; % initial error covariance estimate % Implement the Kalman filter algorithm for k = 2:N % Prediction step x_pred = F * x_est(k-1) + B * P; % predicted state estimate P_pred = F * P_est(k-1) * F' + Q; % predicted error covariance estimate % Update step K = P_pred * H' / (H * P_pred * H' + R); % Kalman gain x_est(k) = x_