Kalman Filter Implemented in The XA Eases Sensory Fusion: Integrated Circuits
Kalman Filter Implemented in The XA Eases Sensory Fusion: Integrated Circuits
ABSTRACT
This application note describes the use of the Kalman filter for sensory fusion.
It covers the theory of operation as well as an implementation example based
on the XA microcontroller.
AN715
Kalman filter implemented in
the XA eases sensory fusion
Philips Semiconductors Application note
Introduction
A microcontroller is often used to interface with sensors and generate control actions based on information
gathered from sensors. For reliability and completeness, more than one sensor is generally used. If different
sensors are employed to provide measurement of the same physical quantity, a better estimation can often be
obtained by calculating weighted average of the individual measurements, assuming the noises they carry are not
statistically correlated.
Sometimes it is useful to have sensors that measure different physical variables and the combined information is
used for control decision making. The operation to combine information from such multi-modal sensors is called
sensory fusion.
Kalman filters can be used to derive the best estimation by combining sensory input from different sources. It has
been used most extensively in navigation since it was invented in 1960. Recently, it has been used in high-end
GPS navigation systems to improve performance.
The concept of Kalman filtering can be illustrated in Figure 1. Suppose a human is traveling in one dimension. He
has a positioning device that gives him estimations of where he is at time t1, t2, and t3, with some variance. For
example, if the measurement says the person is at position t2, the real position could be anywhere around t2 with
a certain distribution function as shown in the figure.
t1 t2 t3
SU01196
Figure 1. A traveler takes position measurements at time t1, t2, and t3.
Suppose this person also knows how fast he is traveling from a speed sensor. He can estimate the distance by
integrating speed over time. Notice the variance increases with time—a phenomenon called drift. Since he has to
integrate speed over time to come up with the estimate, the variances that characterize the quality of this kind of
estimation will grow linearly over time. This is a typical problem for all inertia navigation systems.
t1 t2 t3
SU01197
Figure 2. The traveler can also estimate the position by integrating speed measurements.
1999 May 03 2
Philips Semiconductors Application note
With Kalman filtering, the two measurements can be combined to produce a better measurement. As shown in
Figure 3, the error variance from the combined estimation is both bounded and much reduced.
t1 t2 t3
+
t1 t2 t3
t1 t2 t3
SU01198
Figure 3. With Kalman filtering, speed and position measurements can be combined to produce better
position estimates.
The condition for obtaining better results from Kalman filtering is the following:
• The relationship between different variables, such as speed and distance in this example, is known,
i.e., distance += speed * dt.
• There is NO statistical correlation between the errors carried by the speed sensor and those carried by
positioning device.
• All noises are white with zero mean.
When different sensing mechanisms are used to provide different measurement, there is usually no reason for the
errors to be correlated.
Theory of operation
Let me start with some math equations. For those who do not like to read equations, the numerical example in the
next section will make things appear simpler.
Suppose the dynamics of a system can be described as
x K1 k x k w k (1)
Assume that the system dynamics can be described by n system variables. xk is an “n by 1” vector containing all
the state variables. Φk is an “n by n” matrix that dictates the transition from xk to xk+1 ⋅ wk is called the plant noise.
We assume it is white with known covariance Qk.
Now, suppose part of all the state variables (e.g., m out of n) can be observed also. We use vector zk to denote all
the observable variables, we have:
zk Hk xk vk (2)
where zk is an “m by 1” vector consists of all the observable state parameters. Hk is an “m by n” matrix that extracts
observable variables from the state variables. There is some measurement noise denoted by vk , with known
covariance Rk.
Assuming our initial estimation of the state variables is x^ k , the error of such estimation can be defined as:
k xk xk
e ^ (3)
1999 May 03 3
Philips Semiconductors Application note
With Kalman filtering, the measurement of x can be improved by incorporating measurable variable zk .
* *
x^ k + x^ k ) K k (z k * H k x^ k ) (4)
Kk is an “n by m” matrix yet to be determined. This matrix is called the Kalman gain. It can be calculated as the
following:
Kk + P*
k H k (H k P k H k ) R k)
T * T *1 (5)
The calculation of Kalman gain not only depends on the known measurement error covariance Rk, but also the
state estimation covariance Pk – . The superscript “–” means it is a prior estimate from the last step.
After calculation of Kalman gain, a new and improved estimation covariance can be obtained from:
P k + (I * K k H k)P *
k (6)
The error covariance projected to the next step can be calculated as:
*
P k)1 + k P k Tk ) Q k (7)
X(tn–)
H(tn) φ(tn, tn–1)
X(tn–1+)
SU01199
ȱ10 0.1 0 0
ȳ
+ȧ0 ȧ
1 0 0
1 0.1 x k ) plant_noise
xk (8)
0
Ȳ0 0 0 1 ȴ
Now we are interested in knowing the position based on position measurement, zk = (px, py) :
ƪ
1 0 0 0
ƫ
z k + 0 0 1 0 x k ) measurement_noise (9)
1999 May 03 4
Philips Semiconductors Application note
Let us assume that the measurement error is about 10m rms (the covariance will be 100), we have:
Let us assume that the speed measurement is relatively accurate, with 2m/s/s rms:
ȱ00 0 0 0
ȳ
Q +ȧ0 ȧ
4 0 0
(11)
0 0 0
Ȳ0 0 0 4 ȴ
And let us assume that initially we know where the object is with uncertainty of 10 m. With Kalman filter, the
average measurement error can be reduced from 10 m to 2.4 m.
The following is the code that calculate Kalman gain and error covariance. These lines of code should be included
in a loop and be repeatedly executed:
/*
*
*.....Compute Kalman gain
*
*/
mult_mat_mat(tmp_nm,PMinus,HTranspose);
mult_mat_mat(tmp_mm,H,tmp_nm);
add_mat_mat(tmp_mm,tmp_mm,R);
gaussj(tmp_mm);
mult_mat_mat(tmp_nm,HTranspose,tmp_mm);
mult_mat_mat(Gain,PMinus,tmp_nm);
/*
*
*.....Compute state estimation x^
*
*/
mult_mat_mat(tmp_m_1,H,x_hat_k);
sub_mat_mat(tmp_m_1,z,tmp_m_1);
mult_mat_mat(tmp_n_1,Gain,tmp_m_1);
add_mat_mat(x_hat,x_hat,tmp_n_1);
/*
*
* Compute P – the error covariance
*
*/
mult_mat_mat(tmp_nn,Gain,H);
sub_mat_mat(tmp_nn,Ident,tmp_nn);
mult_mat_mat(PPlus, tmp_nn, PMinus);
1999 May 03 5
Philips Semiconductors Application note
/*
*
* Project ahead
*
*/
}
}
}
1999 May 03 6
Philips Semiconductors Application note
1999 May 03 7
Philips Semiconductors Application note
XA performance
The example shown above was tested on the XA. The example takes only 24K of code and about 8K of data
space. The calculations can be done within 0.1 second, thus allowing a navigation resolution of 0.1 second. The
code is entirely written in C and compiled by TASKING C compiler V2.0.
Figure 5 is the result of a Kalman filter implemented according to the example shown above. The top graph shows
the object movement trace compared with the estimations without Kalman filtering. The bottom graph shows
position estimation with Kalman filtering. The estimation follows the real path much more closely with Kalman
filtering.
200
100
200
0 10 20 30 40 50 60 70 80 90 100
sec
200
100
200
0 10 20 30 40 50 60 70 80 90 100
sec
SU01200
1999 May 03 8
Philips Semiconductors Application note
NOTES
1999 May 03 9
Philips Semiconductors Application note
Definitions
Short-form specification — The data in a short-form specification is extracted from a full data sheet with the same type number and title. For
detailed information see the relevant data sheet or data handbook.
Limiting values definition — Limiting values given are in accordance with the Absolute Maximum Rating System (IEC 134). Stress above one
or more of the limiting values may cause permanent damage to the device. These are stress ratings only and operation of the device at these or
at any other conditions above those given in the Characteristics sections of the specification is not implied. Exposure to limiting values for extended
periods may affect device reliability.
Application information — Applications that are described herein for any of these products are for illustrative purposes only. Philips
Semiconductors make no representation or warranty that such applications will be suitable for the specified use without further testing or
modification.
Disclaimers
Life support — These products are not designed for use in life support appliances, devices or systems where malfunction of these products can
reasonably be expected to result in personal injury. Philips Semiconductors customers using or selling these products for use in such applications
do so at their own risk and agree to fully indemnify Philips Semiconductors for any damages resulting from such application.
Right to make changes — Philips Semiconductors reserves the right to make changes, without notice, in the products, including circuits, standard
cells, and/or software, described or contained herein in order to improve design and/or performance. Philips Semiconductors assumes no
responsibility or liability for the use of any of these products, conveys no license or title under any patent, copyright, or mask work right to these
products, and makes no representations or warranties that these products are free from patent, copyright, or mask work right infringement, unless
otherwise specified.
1999 May 03 10