Introduction to Kalman Filter Matlab
MATLAB provides a variety of functionalities with real-life implications. This article covers a very important MATLAB functionality called the ‘Kalman filter. We use Kalman filter to estimate state of a given system from the measured data. Once this is done, refinement of estimates is also done. Let’s first understand what Kalman filter is.
What is Kalman Filter?
Kalman filter developed primarily by the Hungry based Engineer, Mr. Rudolf Kalman, is an algorithm used to estimate state of a given system using measured data. The Kalman filter’s algorithm is a 2-step process. In the first step, the state of system is predicted and in the second step, estimates of the system state are refined using noisy measurements. Kalman filter has evolved a lot over time and now its several variants are available. Kalman filters are used in applications that involve estimations, like computer vision, navigation&guidance systems, signal processing, and econometrics.
Guidance, Navigation & Control
Kalman filters are used in guidance, navigation, and control systems. These filters synthesize position & velocity signals in sensor fusion. This is achieved by fusing together GPS & IMU measurements (inertial measurement units). Kalman filters are commonly used in estimating the value of a signal which cannot be measured. For example, the temperature inside the aircraft engine or turbine, as temperature sensors would fail in such conditions. These filters are also used commonly along with LQR compensators (linear-quadratic-regulator) for LQG control(linear-quadratic-Gaussian).
Now that we have refreshed our understanding of Kalman filtering, let’s see a detailed example to understand Kalman filter in MATLAB
Steps to Implement Kalman Filter in Matlab
Below are the steps user will need to follow to implement Kalman filter in MATLAB. The MATLAB code is also provided along with the steps:
1. We will define length of simulation:
simulen = 30
2. Let us now define the system
b = 1
c = 4
(we use b = 1 for constant systems; you can use |b| < 1 for a system of 1st order)
3. Next, we will declare the covariances of noise
4. We will now pre-allocate the memory for the arrays. Please keep in mind that doing pre-allocation is not necessary, we only do it to speed-up the processing in MATLAB.
5. Next, we will define initial estimates for ‘the state’ and error (‘priori error’)
6. Next, we will compute the process noise and measurement noise
Here we are taking percentage for a better comparison
7. Let’s now define initial condition on x and initial estimates for posteriori covariance and state
8. Next, we will compute the first guesses of all the values. This will be done based on the initial estimates of state followed by posteriori covariance. Next steps will be computed in loopComputing state & output:
x(1)= b *x_0+w(1);
z(1)= c *x(1)+v(1);
- Our Predictor equations are:
residual(1)=z(1) – c *inpriori(1);
- Corrector equations will be:
k(1)= c *rapriori(1)/ (c * c *rapriori(1)+R);
raposteriori(1)=rapriori(1)*(1 – h*k(1));
- Computing rest values:
for p =2:nlen,
x(p) = b * x(p -1)+w(p);
z(p) = c *x(p)+v(p);
inpriori(p)= b *inposteriori(p-1);
residual(p)=z(p) – c *inpriori(p);
rapriori(p)= b * b *raposteriori(p -1)+Q;
k(p)= c *rapriori(p)/(c * c *rapriori(p)+R);
raposteriori(p)=rapriori(p)*(1 – c *k(p));
9. Let’s now plot state & their estimates
h2= stem(p +0.5,inposteriori,'g');
hold offlegend([h1(1) h2(1) h3(1)], ‘priori', 'posteriori','exact');
title(‘State with priori and posteriori’);
10. Let us also plot the covariance
legend([h1(1) h2(1)], 'priori', 'posteriori');
title(‘Priori and posteriori covariance’);
On implementing above code, below is how our output will look like in MATLAB:
Kalman filters are perfect for systems which are changing continuously. These filters have the advantage of being light on the memory as they don’t require to keep anything in history other than their previous state. Kalman filters are also very fast which make them great tool for embedded systems and real-time problems.
This is a guide to Kalman Filter Matlab. Here we discuss the Introduction, syntax, What is Kalman Filter, and Steps to Implement Kalman Filter respectively. You may also have a look at the following articles to learn more –