You are on page 1of 8

DAN SIMON

f e a t u r e

Kalman Filtering
Originally developed for use in spacecraft navigation, the Kalman filter
turns out to be useful for many applications. It is mainly used to estimate
system states that can only be observed indirectly or inaccurately by the
system itself.

F
iltering is desirable in many situations in engineering and
embedded systems. For example, radio communication signals
are corrupted with noise. A good filtering algorithm can remove
the noise from electromagnetic signals while retaining the useful
information. Another example is power supply voltages.
Uninterruptible power supplies are devices that filter line volt-
ages in order to smooth out undesirable fluctuations that might otherwise
shorten the lifespan of electrical devices such as computers and printers.
The Kalman filter is a tool that can estimate the variables of a wide range
of processes. In mathematical terms we would say that a Kalman filter esti-
mates the states of a linear system. The Kalman filter not only works well in
practice, but it is theoretically attractive because it can be shown that of all
possible filters, it is the one that minimizes the variance of the estimation
error. Kalman filters are often implemented in embedded control systems
because in order to control a process, you first need an accurate estimate
of the process variables.
This article will tell you the basic concepts that you need to know to
design and implement a Kalman filter. I will introduce the Kalman filter
algorithm and we’ll look at the use of this filter to solve a vehicle navigation
problem. In order to control the position of an automated vehicle, we first
must have a reliable estimate of the vehicle’s present position. Kalman fil-
tering provides a tool for obtaining that reliable estimate.

Linear systems
In order to use a Kalman filter to remove noise from a signal, the process
that we are measuring must be able to be described by a linear system.
Many physical processes, such as a vehicle driving along a road, a satellite
orbiting the earth, a motor shaft driven by winding currents, or a sinusoidal

72 JUNE 2001 Embedded Systems Programming


The Kalman filter not only works well in practice, but it is theoretical-
ly attractive because it can be shown that of all possible filters, it is
the one that minimizes the variance of the estimation error.

radio-frequency carrier signal, can be need a way to estimate the state x. This
vk +1 = vk + Tuk
approximated as linear systems. A lin- is where the Kalman filter comes in.
ear system is simply a process that can
be described by the following two That is, the velocity one time-step The Kalman filter theory
equations: from now (T seconds from now) will and algorithm
be equal to the present velocity plus Suppose we have a linear system model
State equation: the commanded acceleration multi- as described previously. We want to use
xk +1 = Axk + Buk + wk plied by T. But the previous equation the available measurements y to esti-
does not give a precise value for vk+1. mate the state of the system x. We know
Output equation: Instead, the velocity will be perturbed how the system behaves according to
yk = Cxk + zk by noise due to gusts of wind, pot- the state equation, and we have mea-
holes, and other unfortunate realities. surements of the position, so how can
The velocity noise is a random variable we determine the best estimate of the
In the above equations A, B, and C that changes with time. So a more real- state x? We want an estimator that gives
are matrices; k is the time index; x is istic equation for v would be: an accurate estimate of the true state
called the state of the system; u is a even though we cannot directly mea-
known input to the system; y is the vk +1 = vk + Tuk + vk~ sure it. What criteria should our esti-
measured output; and w and z are the mator satisfy? Two obvious require-
~
noise. The variable w is called the where vk is the velocity noise. A simi- ments come to mind.
process noise, and z is called the mea- lar equation can be derived for the First, we want the average value of
surement noise. Each of these quanti- position p: our state estimate to be equal to the
ties are (in general) vectors and there- average value of the true state. That is,
1
fore contain more than one element. pk +1 = pk + Tvk + T 2uk + pk~ we don’t want our estimate to be biased
The vector x contains all of the infor- 2 one way or another. Mathematically, we
mation about the present state of the would say that the expected value of
system, but we cannot measure x where pk~ is the position noise. Now the estimate should be equal to the
directly. Instead we measure y, which is we can define a state vector x that con- expected value of the state.
a function of x that is corrupted by the sists of position and velocity: Second, we want a state estimate
noise z. We can use y to help us obtain that varies from the true state as little
an estimate of x, but we cannot neces-  pk  as possible. That is, not only do we
xk =  
sarily take the information from y at  vk  want the average of the state estimate
face value because it is corrupted by to be equal to the average of the true
noise. The measurement is like a politi- Finally, knowing that the measured state, but we also want an estimator
cian. We can use the information that output is equal to the position, we can that results in the smallest possible
it presents to a certain extent, but we write our linear system equations as variation of the state estimate.
cannot afford to grant it our total trust. follows: Mathematically, we would say that we
For example, suppose we want to want to find the estimator with the
model a vehicle going in a straight 1 T  T 2 2  smallest possible error variance.
x k +1 =   xk +  uk + wk
line. We can say that the state consists 0 1  It so happens that the Kalman filter
 T 
of the vehicle position p and velocity v. is the estimator that satisfies these two
yk = [1 0]xk + zk
The input u is the commanded accel- criteria. But the Kalman filter solution
eration and the output y is the mea- does not apply unless we can satisfy
sured position. Let’s say that we are zk is the measurement noise due to certain assumptions about the noise
able to change the acceleration and such things as instrumentation errors. that affects our system. Remember
HANNAH BLAIR

measure the position every T seconds. If we want to control the vehicle with from our system model that w is the
In this case, elementary laws of physics some sort of feedback system, we need process noise and z is the measure-
say that the velocity v will be governed an accurate estimate of the position p ment noise. We have to assume that
by the following equation: and the velocity v. In other words, we the average value of w is zero and the

Embedded Systems Programming JUNE 2001 73


kalman filters

lent ways to express the equations. One


FIGURE 1 Vehicle position (true, measured, and estimated) of the formulations is given as follows:

1800
Kk = APk C T (CPk C T + Sz )
−1

1600
xˆ k +1 = ( Axˆ k + Buk ) + Kk ( yk +1 − Cxˆ k )
1400
Pk +1 = APk AT + Sw − APk C T Sz−1CPk AT
1200
Position (feet)

1000 That’s the Kalman filter. It consists


of three equations, each involving
800
matrix manipulation. In the above
600 equations, a –1 superscript indicates
400
matrix inversion and a T superscript
indicates matrix transposition. The K
200 matrix is called the Kalman gain, and
0 the P matrix is called the estimation
error covariance.
-200 The state estimate ( x̂ ) equation is
0 10 20 30 40 50 60
Time (sec) fairly intuitive. The first term used to
derive the state estimate at time k + 1 is
just A times the state estimate at time k,
plus B times the known input at time k.
FIGURE 2 Position measurement error and position estimation error This would be the state estimate if we
didn’t have a measurement. In other
30
words, the state estimate would propa-
gate in time just like the state vector in
20 the system model. The second term in
the x̂ equation is called the correction
term and it represents the amount by
10
Position Error (feet)

which to correct the propagated state


estimate due to our measurement.
0 Inspection of the K equation shows
that if the measurement noise is large,
Sz will be large, so K will be small and
-10
we won’t give much credibility to the
measurement y when computing the
-20 next x̂ . On the other hand, if the mea-
surement noise is small, Sz will be
small, so K will be large and we will
-30
0 10 20 30 40 50 60 give a lot of credibility to the measure-
Time (sec) ment when computing the next x̂ .

Vehicle navigation
average value of z is zero. We have to Measurement noise covariance Now consider the vehicle navigation

Sz = E( z z )
further assume that no correlation T problem that we looked at earlier. A
k k
exists between w and z. That is, at any vehicle is traveling along a road. The
time k, wk, and zk are independent ran- position is measured with an error of
dom variables. Then the noise covari- where wT and zT indicate the transpose 10 feet (one standard deviation). The
ance matrices Sw and Sz are defined as: of the w and z random noise vectors, commanded acceleration is a constant
and E(·) means the expected value. 1 foot/sec2. The acceleration noise is
Process noise covariance Now we are finally in a position to 0.2 feet/sec2 (one standard devia-

Sw = E(wk wkT )
look at the Kalman filter equations. tion). The position is measured 10
There are many alternative but equiva- times per second (T = 0.1). How can

74 JUNE 2001 Embedded Systems Programming


kalman filters
we best estimate the position of the of the vehicle, the measured position, smooth curves are the true position
moving vehicle? In view of the large and the estimated position. The two and the estimated position, and they
measurement noise, we can surely do
better than just taking the measure-
ments at face value.
Since T = 0.1, the linear model that FIGURE 3 Velocity (true and estimated)
represents our system can be derived 70
from the system model presented ear-
lier in this article as follows:
60

1 0.1 0.005
x k +1 =   xk +  0.1 uk + wk
50
 0 1   
Velocity (feet/sec)
yk = [1 0]xk + zk 40

30
Because the standard deviation of the
measurement noise is 10 feet, the Sz
matrix is simply equal to 100. 20
Now we need to derive the Sw
matrix. Since the position is propor- 10
tional to 0.005 times the acceleration,
and the acceleration noise is 0.2 0
feet/sec2, the variance of the position 0 10 20 30 40 50 60
Time (sec)
noise is (0.005)2·(0.2)2 = 10-6.
Similarly, since the velocity is propor-
tional to 0.1 times the acceleration,
the variance of the velocity noise is
(0.1)2·(0.2)2 = 4·10-4. Finally, the
covariance of the position noise and
velocity noise is equal to the standard
deviation of the position noise times
the standard deviation of the velocity
noise, which can be calculated as
(0.005·0.2)·(0.1·0.2) = 2·10-5. We
can combine all of these calculations
to obtain the following matrix for Sw:

  p 
Sw = E( xx T ) = E   [ p v]
 v  
 p2 pv   10 −6 2 × 10 −5 
= E = 
 vp v 2  2 × 10 −5 4 × 10 −4 

Now we just initialize x̂0 as our best


initial estimate of position and veloci-
ty, and we initialize P0 as the uncer-
tainty in our initial estimate. Then we
execute the Kalman filter equations
once per time step and we are off and
running.
I simulated the Kalman filter for
this problem using Matlab. The results
are shown in the accompanying fig-
ures. Figure 1 shows the true position

Embedded Systems Programming JUNE 2001 75


kalman filters

are almost too close to distinguish


LISTING 1 Kalman filter simulation from one another. The noisy-looking
curve is the measured position.
function kalman(duration, dt) Figure 2 shows the error between
the true position and the measured
% function kalman(duration, dt) position, and the error between the
% true position and the Kalman filter’s
% Kalman filter simulation for a vehicle travelling along a road. estimated position. The measurement
% INPUTS error has a standard deviation of about
% duration = length of simulation (seconds) 10 feet, with occasional spikes up to 30
% dt = step size (seconds) feet (3 sigma). The estimated position
error stays within about two feet.
measnoise = 10; % position measurement noise (feet)
Figure 3 shows a bonus that we get
accelnoise = 0.2; % acceleration noise (feet/sec^2)
from the Kalman filter. Since the vehi-
cle velocity is part of the state x, we get
a = [1 dt; 0 1]; % transition matrix
a velocity estimate along with the posi-
b = [dt^2/2; dt]; % input matrix
tion estimate. Figure 4 shows the error
c = [1 0]; % measurement matrix
between the true velocity and the
x = [0; 0]; % initial state vector
xhat = x; % initial state estimate
Kalman filter’s estimated velocity.
The Matlab program that I used to
Sz = measnoise^2; % measurement error covariance generate these results is shown in
Sw = accelnoise^2 * [dt^4/4 dt^3/2; dt^3/2 dt^2]; % process noise cov Listing 1. Don’t worry if you don’t
P = Sw; % initial estimation covariance know Matlab—it’s an easy-to-read lan-
guage, almost like pseudocode, but
% Initialize arrays for later plotting. with built-in matrix operations. If you
pos = []; % true position array use Matlab to run the program you
poshat = []; % estimated position array will get different results every time
posmeas = []; % measured position array because of the random noise that is
vel = []; % true velocity array simulated, but the results will be simi-
velhat = []; % estimated velocity array lar to the figures shown here.

for t = 0 : dt: duration, Practical issues and


% Use a constant commanded acceleration of 1 foot/sec^2. extensions
u = 1; The basic ideas of Kalman filtering are
% Simulate the linear system. straightforward, but the filter equations
ProcessNoise = accelnoise * [(dt^2/2)*randn; dt*randn];
rely heavily on matrix algebra. Listing 2
x = a * x + b * u + ProcessNoise;
shows the Kalman filter update equa-
% Simulate the noisy measurement
tions in C. The matrix algebra listings
MeasNoise = measnoise * randn;
referenced in Listing 2 can be found at
y = c * x + MeasNoise;
www.embedded.com/code.html.
% Extrapolate the most recent state estimate to the present time.
These listings are very general and,
xhat = a * xhat + b * u;
% Form the Innovation vector.
if the problem is small enough, could
Inn = y - c * xhat;
probably be simplified considerably.
% Compute the covariance of the Innovation. For example, the inverse of the 2-by-2
s = c * P * c’ + Sz; matrix:
% Form the Kalman Gain matrix.
K = a * P * c’ * inv(s);  d1 d2 
D=
% Update the state estimate.
d3 d4 
xhat = xhat + K * Inn;
% Compute the covariance of the estimation error. is equal to:

Listing 1 continued on p. 78.


1  d4 − d2 
D−1 =
d1d4 − d2 d3 − d3 d1 

76 JUNE 2001 Embedded Systems Programming


kalman filters
So if you need to invert a 2-by-2 state at time k we could use measure- Kalman filter can be modified for this
matrix you can use the above equa- ments not only up to and including problem to obtain the so-called
tion. Some additional C code for time k, but also after time k. The Kalman smoother.
matrix manipulation and Kalman fil-
tering can be found at http://wad.
www.media.mit.edu/people/wad/mas864
/proj_src.html. FIGURE 4 Velocity estimation error
Systems with more than three states 0.4
could exceed your budget for program
size and computational effort. The 0.3
computational effort associated with
matrix inversion is proportional to n3 0.2

Velocity Error (feet/sec)


(where n is the size of the matrix). This
means that if the number of states in 0.1

the Kalman filter doubles, the compu-


0
tational effort increases by a factor of
eight. It’s not too hard to see how you
-0.1
could run out of throughput pretty
quickly for a moderately sized Kalman -0.2
filter. But never fear! The so-called
“steady state Kalman filter” can greatly -0.3
reduce the computational expense
while still giving good estimation per- -0.4
formance. In the steady state Kalman 0 10 20 30 40 50 60
Time (sec)
filter the matrices Kk and Pk are con-
stant, so they can be hard-coded as
constants, and the only Kalman filter
equation that needs to be implement-
ed in real time is the x̂ equation,
which consists of simple multiplies and
addition steps (or multiply and accu-
mulates if you’re using a DSP).
We have discussed state estimation
for linear systems. But what if we want
to estimate the states of a nonlinear sys-
tem? As a matter of fact, almost all real
engineering processes are nonlinear.
Some can be approximated by linear
systems but some cannot. This was rec-
ognized early in the history of Kalman
filters and led to the development of
the “extended Kalman filter,” which is
simply an extension of linear Kalman
filter theory to nonlinear systems.
Up to this point we have talked
about estimating the state one step at
a time as we obtain measurements. But
what if we want to estimate the state as
a function of time after we already
have the entire time-history of mea-
surements? For example, what if we
want to reconstruct the trajectory of
our vehicle after the fact? Then it
seems that we could do better than the
Kalman filter because to estimate the

Embedded Systems Programming JUNE 2001 77


kalman filters

that make it more effective in cer-


LISTING 1, continued Kalman filter simulation tain situations.
Kalman filter theory assumes that
P = a * P * a’ - a * P * c’ * inv(s) * c * P * a’ + Sw;
the process noise w and the measure-
% Save some parameters for plotting later.
ment noise z are independent from
pos = [pos; x(1)];
each other. What if we have a system
posmeas = [posmeas; y];
where these two noise processes are
poshat = [poshat; xhat(1)];
not independent? This is the correlat-
vel = [vel; x(2)];
ed noise problem, and the Kalman fil-
velhat = [velhat; xhat(2)];
ter can be modified to handle this
end
case. In addition, the Kalman filter
requires that the noise covariances Sw
% Plot the results
and Sz be known. What if they are not
close all;
known? Then how can we best esti-
t = 0 : dt : duration;
mate the state? Again, this is the prob-
lem solved by the H¥ filter.
figure;
Kalman filtering is a huge field
plot(t,pos, t,posmeas, t,poshat);
whose depths we cannot hope to begin
grid;
to plumb in these few pages.
xlabel(‘Time (sec)’);
Thousands of papers and dozens of
ylabel(‘Position (feet)’);
textbooks have been written on this
title(‘Figure 1 - Vehicle Position (True, Measured, and Estimated)’)
subject since its inception in 1960.

figure;
Historical perspective
plot(t,pos-posmeas, t,pos-poshat);
The Kalman filter was developed by
grid;
Rudolph Kalman, although Peter
xlabel(‘Time (sec)’);
Swerling developed a very similar algo-
ylabel(‘Position Error (feet)’);
rithm in 1958. The filter is named after
title(‘Figure 2 - Position Measurement Error and Position Estimation Error’);
Kalman because he published his results
in a more prestigious journal and his
figure;
work was more general and complete.
plot(t,vel, t,velhat);
Sometimes the filter is referred to as the
grid;
Kalman-Bucy filter because of Richard
xlabel(‘Time (sec)’);
Bucy’s early work on the topic, conduct-
ylabel(‘Velocity (feet/sec)’);
ed jointly with Kalman.
title(‘Figure 3 - Velocity (True and Estimated)’);
The roots of the algorithm can be
traced all the way back to the 18-year-
figure;
old Karl Gauss’s method of least squares
plot(t,vel-velhat);
in 1795. Like many new technologies,
grid;
the Kalman filter was developed to solve
xlabel(‘Time (sec)’);
a specific problem, in this case, space-
ylabel(‘Velocity Error (feet/sec)’);
craft navigation for the Apollo space
title(‘Figure 4 - Velocity Estimation Error’);
program. Since then, the Kalman filter
has found applications in hundreds of
diverse areas, including all forms of nav-
The Kalman filter not only works rather than the “average” estimation igation (aerospace, land, and marine),
well but is theoretically attractive. It error? This problem is solved by the nuclear power plant instrumentation,
can be shown that the Kalman filter H¥ filter. The H¥ filter (pronounced demographic modeling, manufactur-
minimizes the variance of the esti- “H infinity” and sometimes written ing, the detection of underground
mation error. But what if we have a as H¥ ) is an alternative to Kalman radioactivity, and fuzzy logic and neural
problem where we are more con- filtering that was developed in the network training. esp
cerned with the worst case estima- 1980s. It is less widely known and
tion error? What if we want to mini- less commonly applied than the Dan Simon is a professor in the electrical
mize the “worst” estimation error Kalman filter, but it has advantages and computer engineering department at

78 JUNE 2001 Embedded Systems Programming


kalman filters
Cleveland State University and a consul-
tant to industry. His teaching and research LISTING 2 Kalman filter equations
interests include filtering, control theory, // The following code snippet assumes that the linear system has n states, m
embedded systems, fuzzy logic, and neural // inputs, and r outputs. Therefore, the following variables are assumed to
networks. He is presently trying to imple- // already be defined.
// A is an n by n matrix
ment a DSP-based motor controller using a
// B is an n by m matrix
Kalman filter. You can contact him at // C is an r by n matrix
d.j.simon@csuohio.edu. // xhat is an n by 1 vector
// y is an r by 1 vector
// u is an m by 1 vector
For further reading // Sz is an r by r matrix
Gelb, A. Applied Optimal Estimation. // Sw is an n by n matrix
Cambridge, MA: MIT Press, 1974. This // P is an n by n matrix
is what you call an “oldie but goodie.”
float AP[n][n]; // This is the matrix A*P
And don’t worry that it’s published by float CT[n][r]; // This is the matrix CT
MIT Press; it’s a simple and straightfor- float APCT[n][r]; // This is the matrix A*P*CT
ward book that starts with the basics float CP[r][n]; // This is the matrix C*P
and is heavy on practical issues. float CPCT[r][r]; // This is the matrix C*P*CT
float CPCTSz[r][r]; // This is the matrix C*P*CT+Sz
Anderson, B. and J. Moore. Optimal float CPCTSzInv[r][r]; // This is the matrix (C*P*CT+Sz)-1
Filtering. Englewood Cliffs, NJ: float K[n][r]; // This is the Kalman gain.
Prentice-Hall, 1979. This is very math- float Cxhat[r][1]; // This is the vector C*xhat
float yCxhat[r][1]; // This is the vector y-C*xhat
ematical and difficult to read, but I
float KyCxhat[n][1]; // This is the vector K*(y-C*xhat)
have relied heavily on it for obtaining float Axhat[n][1]; // This is the vector A*xhat
a fundamental theoretical understand- float Bu[n][1]; // This is the vector B*u
ing of Kalman filtering and related float AxhatBu[n][1]; // This is the vector A*xhat+B*u
float AT[n][n]; // This is the matrix AT
issues.
float APAT[n][n]; // This is the matrix A*P*AT
Grewal, M. and A. Andrews. Kalman float APATSw[n][n]; // This is the matrix A*P*AT+Sw
Filtering Theory and Practice. float CPAT[r][n]; // This is the matrix C*P*AT
Englewood Cliffs, NJ: Prentice-Hall, float SzInv[r][r]; // This is the matrix Sz-1
float APCTSzInv[n][r]; // This is the matrix A*P*CT*Sz-1
1993. This is a happy medium between
float APCTSzInvCPAT[n][n]; // This is the matrix A*P*CT*Sz-1*C*P*AT
the first two references, a nice balance
between theory and practice. One good // The following sequence of function calls computes the K matrix.
feature of this book is that it includes MatrixMultiply((float*)A, (float*)P, n, n, n, (float*)AP);
MatrixTranspose((float*)C, r, n, (float*)CT);
Kalman filtering source code on a floppy MatrixMultiply((float*)AP, (float*)CT, n, n, r, (float*)APCT);
disk. One not-so-nice feature is that the MatrixMultiply((float*)C, (float*)P, r, n, n, (float*)CP);
source code is written in Fortran. MatrixMultiply((float*)CP, (float*)CT, r, n, r, (float*)CPCT);
Sorenson, H. Kalman Filtering: Theory and MatrixAddition((float*)CPCT, (float*)Sz, r, r, (float*)CPCTSz);
MatrixInversion((float*)CPCTSz, r, (float*)CPCTSzInv);
Application. Los Alamitos, CA: IEEE MatrixMultiply((float*)APCT, (float*)CPCTSzInv, n, r, r, (float*)K);
Press, 1985. This is a collection of some
of the classic papers on Kalman filtering, // The following sequence of function calls updates the xhat vector.
MatrixMultiply((float*)C, (float*)xhat, r, n, 1, (float*)Cxhat);
starting with Kalman’s original paper in
MatrixSubtraction((float*)y, (float*)Cxhat, r, 1, (float*)yCxhat);
1960. The papers are academically ori- MatrixMultiply((float*)K, (float*)yCxhat, n, r, 1, (float*)KyCxhat);
ented, but someone who likes theory MatrixMultiply((float*)A, (float*)xhat, n, n, 1, (float*)Axhat);
will obtain an interesting historical per- MatrixMultiply((float*)B, (float*)u, n, r, 1, (float*)Bu);
MatrixAddition((float*)Axhat, (float*)Bu, n, 1, (float*)AxhatBu);
spective from this book.
MatrixAddition((float*)AxhatBu, (float*)KyCxhat, n, 1, (float*)xhat);
http://ourworld.compuserve.com/home-
pages/PDJoseph/—This is Peter Joseph’s // The following sequence of function calls updates the P matrix.
Web site, and a useful resource on the MatrixTranspose((float*)A, n, n, (float*)AT);
MatrixMultiply((float*)AP, (float*)AT, n, n, n, (float*)APAT);
topic of Kalman filtering. Dr. Joseph has
MatrixAddition((float*)APAT, (float*)Sw, n, n, (float*)APATSw);
worked with Kalman filters since their MatrixTranspose((float*)APCT, n, r, (float*)CPAT);
inception in 1960, and coauthored per- MatrixInversion((float*)Sz, r, (float*)SzInv);
haps the earliest text on the subject (in MatrixMultiply((float*)APCT, (float*)SzInv, n, r, r, (float*)APCTSzInv);
MatrixMultiply((float*)APCTSzInv, (float*)CPAT, n, r, n,
1968). His Web page includes lessons (float*)APCTSzInvCPAT);
for the beginning, intermediate, and MatrixSubtraction((float*)APATSw, (float*)APCTSzInvCPAT, n, n, (float*)P);
advanced student.

Embedded Systems Programming JUNE 2001 79

You might also like