![](http://static.javashuo.com/static/loading.gif)
% KALMANF - updates a system state vector estimate based upon an
![](http://static.javashuo.com/static/loading.gif)
% observation, using a discrete Kalman filter.
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% Version 1.0, June 30, 2004
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% This tutorial function was written by Michael C. Kleder
![](http://static.javashuo.com/static/loading.gif)
% (Comments are appreciated at: public@kleder.com)
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% INTRODUCTION
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% Many people have heard of Kalman filtering, but regard the topic
![](http://static.javashuo.com/static/loading.gif)
% as mysterious. While it's true that deriving the Kalman filter and
![](http://static.javashuo.com/static/loading.gif)
% proving mathematically that it is "optimal" under a variety of
![](http://static.javashuo.com/static/loading.gif)
% circumstances can be rather intense, applying the filter to
![](http://static.javashuo.com/static/loading.gif)
% a basic linear system is actually very easy. This Matlab file is
![](http://static.javashuo.com/static/loading.gif)
% intended to demonstrate that.
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% An excellent paper on Kalman filtering at the introductory level,
![](http://static.javashuo.com/static/loading.gif)
% without detailing the mathematical underpinnings, is:
![](http://static.javashuo.com/static/loading.gif)
% "An Introduction to the Kalman Filter"
![](http://static.javashuo.com/static/loading.gif)
% Greg Welch and Gary Bishop, University of North Carolina
![](http://static.javashuo.com/static/loading.gif)
% http://www.cs.unc.edu/~welch/kalman/kalmanIntro.html
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% PURPOSE:
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% The purpose of each iteration of a Kalman filter is to update
![](http://static.javashuo.com/static/loading.gif)
% the estimate of the state vector of a system (and the covariance
![](http://static.javashuo.com/static/loading.gif)
% of that vector) based upon the information in a new observation.
![](http://static.javashuo.com/static/loading.gif)
% The version of the Kalman filter in this function assumes that
![](http://static.javashuo.com/static/loading.gif)
% observations occur at fixed discrete time intervals. Also, this
![](http://static.javashuo.com/static/loading.gif)
% function assumes a linear system, meaning that the time evolution
![](http://static.javashuo.com/static/loading.gif)
% of the state vector can be calculated by means of a state transition
![](http://static.javashuo.com/static/loading.gif)
% matrix.
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% USAGE:
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% s = kalmanf(s)
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% "s" is a "system" struct containing various fields used as input
![](http://static.javashuo.com/static/loading.gif)
% and output. The state estimate "x" and its covariance "P" are
![](http://static.javashuo.com/static/loading.gif)
% updated by the function. The other fields describe the mechanics
![](http://static.javashuo.com/static/loading.gif)
% of the system and are left unchanged. A calling routine may change
![](http://static.javashuo.com/static/loading.gif)
% these other fields as needed if state dynamics are time-dependent;
![](http://static.javashuo.com/static/loading.gif)
% otherwise, they should be left alone after initial values are set.
![](http://static.javashuo.com/static/loading.gif)
% The exceptions are the observation vectro "z" and the input control
![](http://static.javashuo.com/static/loading.gif)
% (or forcing function) "u." If there is an input function, then
![](http://static.javashuo.com/static/loading.gif)
% "u" should be set to some nonzero value by the calling routine.
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% SYSTEM DYNAMICS:
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% The system evolves according to the following difference equations,
![](http://static.javashuo.com/static/loading.gif)
% where quantities are further defined below:
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% x = Ax + Bu + w meaning the state vector x evolves during one time
![](http://static.javashuo.com/static/loading.gif)
% step by premultiplying by the "state transition
![](http://static.javashuo.com/static/loading.gif)
% matrix" A. There is optionally (if nonzero) an input
![](http://static.javashuo.com/static/loading.gif)
% vector u which affects the state linearly, and this
![](http://static.javashuo.com/static/loading.gif)
% linear effect on the state is represented by
![](http://static.javashuo.com/static/loading.gif)
% premultiplying by the "input matrix" B. There is also
![](http://static.javashuo.com/static/loading.gif)
% gaussian process noise w.
![](http://static.javashuo.com/static/loading.gif)
% z = Hx + v meaning the observation vector z is a linear function
![](http://static.javashuo.com/static/loading.gif)
% of the state vector, and this linear relationship is
![](http://static.javashuo.com/static/loading.gif)
% represented by premultiplication by "observation
![](http://static.javashuo.com/static/loading.gif)
% matrix" H. There is also gaussian measurement
![](http://static.javashuo.com/static/loading.gif)
% noise v.
![](http://static.javashuo.com/static/loading.gif)
% where w ~ N(0,Q) meaning w is gaussian noise with covariance Q
![](http://static.javashuo.com/static/loading.gif)
% v ~ N(0,R) meaning v is gaussian noise with covariance R
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% VECTOR VARIABLES:
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% s.x = state vector estimate. In the input struct, this is the
![](http://static.javashuo.com/static/loading.gif)
% "a priori" state estimate (prior to the addition of the
![](http://static.javashuo.com/static/loading.gif)
% information from the new observation). In the output struct,
![](http://static.javashuo.com/static/loading.gif)
% this is the "a posteriori" state estimate (after the new
![](http://static.javashuo.com/static/loading.gif)
% measurement information is included).
![](http://static.javashuo.com/static/loading.gif)
% s.z = observation vector
![](http://static.javashuo.com/static/loading.gif)
% s.u = input control vector, optional (defaults to zero).
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% MATRIX VARIABLES:
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% s.A = state transition matrix (defaults to identity).
![](http://static.javashuo.com/static/loading.gif)
% s.P = covariance of the state vector estimate. In the input struct,
![](http://static.javashuo.com/static/loading.gif)
% this is "a priori," and in the output it is "a posteriori."
![](http://static.javashuo.com/static/loading.gif)
% (required unless autoinitializing as described below).
![](http://static.javashuo.com/static/loading.gif)
% s.B = input matrix, optional (defaults to zero).
![](http://static.javashuo.com/static/loading.gif)
% s.Q = process noise covariance (defaults to zero).
![](http://static.javashuo.com/static/loading.gif)
% s.R = measurement noise covariance (required).
![](http://static.javashuo.com/static/loading.gif)
% s.H = observation matrix (defaults to identity).
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% NORMAL OPERATION:
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% (1) define all state definition fields: A,B,H,Q,R
![](http://static.javashuo.com/static/loading.gif)
% (2) define intial state estimate: x,P
![](http://static.javashuo.com/static/loading.gif)
% (3) obtain observation and control vectors: z,u
![](http://static.javashuo.com/static/loading.gif)
% (4) call the filter to obtain updated state estimate: x,P
![](http://static.javashuo.com/static/loading.gif)
% (5) return to step (3) and repeat
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% INITIALIZATION:
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% If an initial state estimate is unavailable, it can be obtained
![](http://static.javashuo.com/static/loading.gif)
% from the first observation as follows, provided that there are the
![](http://static.javashuo.com/static/loading.gif)
% same number of observable variables as state variables. This "auto-
![](http://static.javashuo.com/static/loading.gif)
% intitialization" is done automatically if s.x is absent or NaN.
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
%x = inv(H)*z
![](http://static.javashuo.com/static/loading.gif)
%P = inv(H)*R*inv(H')
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% This is mathematically equivalent to setting the initial state estimate
![](http://static.javashuo.com/static/loading.gif)
% covariance to infinity.
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% SCALAR EXAMPLE (Automobile Voltimeter):
![](http://static.javashuo.com/static/loading.gif)
%
![](http://static.javashuo.com/static/loading.gif)
% % Define the system as a constant of 12 volts:
![](http://static.javashuo.com/static/loading.gif)
function T
![](http://static.javashuo.com/static/loading.gif)
clear s
![](http://static.javashuo.com/static/loading.gif)
s.x = 12;
![](http://static.javashuo.com/static/loading.gif)
s.A = 1;
![](http://static.javashuo.com/static/loading.gif)
% % Define a process noise (stdev) of 2 volts as the car operates:
![](http://static.javashuo.com/static/loading.gif)
s.Q = 2^2; % variance, hence stdev^2
![](http://static.javashuo.com/static/loading.gif)
% Define the voltimeter to measure the voltage itself:
![](http://static.javashuo.com/static/loading.gif)
s.H = 1;
![](http://static.javashuo.com/static/loading.gif)
% % Define a measurement error (stdev) of 2 volts:
![](http://static.javashuo.com/static/loading.gif)
s.R = 2^2; % variance, hence stdev^2
![](http://static.javashuo.com/static/loading.gif)
%Do not define any system input (control) functions:
![](http://static.javashuo.com/static/loading.gif)
s.B = 0;
![](http://static.javashuo.com/static/loading.gif)
s.u = 0;
![](http://static.javashuo.com/static/loading.gif)
% % Do not specify an initial state:
![](http://static.javashuo.com/static/loading.gif)
s.x = nan;
![](http://static.javashuo.com/static/loading.gif)
s.P = nan;
![](http://static.javashuo.com/static/loading.gif)
% % Generate random voltages and watch the filter operate.
![](http://static.javashuo.com/static/loading.gif)
tru=[]; % truth voltage
![](http://static.javashuo.com/static/loading.gif)
for t=1:20
![](http://static.javashuo.com/static/loading.gif)
tru(end+1) = randn*2+12;
![](http://static.javashuo.com/static/loading.gif)
s(end).z = tru(end) + randn*2; % create a measurement
![](http://static.javashuo.com/static/loading.gif)
s(end+1)=kalmanf(s(end)); % perform a Kalman filter iteration
![](http://static.javashuo.com/static/loading.gif)
% end
![](http://static.javashuo.com/static/loading.gif)
% figure
![](http://static.javashuo.com/static/loading.gif)
% hold on
![](http://static.javashuo.com/static/loading.gif)
% grid on
![](http://static.javashuo.com/static/loading.gif)
% % plot measurement data:
![](http://static.javashuo.com/static/loading.gif)
hz=plot([s(1:end-1).z],'r');hold on
![](http://static.javashuo.com/static/loading.gif)
% % plot a-posteriori state estimates:
![](http://static.javashuo.com/static/loading.gif)
hk=plot([s(2:end).x],'b-');hold on
![](http://static.javashuo.com/static/loading.gif)
ht=plot(tru,'g-');hold on
![](http://static.javashuo.com/static/loading.gif)
legend('observations','Kalman output','true voltage',0)
![](http://static.javashuo.com/static/loading.gif)
title('Automobile Voltimeter Example')
![](http://static.javashuo.com/static/loading.gif)
% hold off
![](http://static.javashuo.com/static/loading.gif)
end
![](http://static.javashuo.com/static/loading.gif)
function s = kalmanf(s)
![](http://static.javashuo.com/static/loading.gif)
% set defaults for absent fields:
![](http://static.javashuo.com/static/loading.gif)
if ~isfield(s,'x'); s.x=nan*z; end
![](http://static.javashuo.com/static/loading.gif)
if ~isfield(s,'P'); s.P=nan; end
![](http://static.javashuo.com/static/loading.gif)
if ~isfield(s,'z'); error('Observation vector missing'); end
![](http://static.javashuo.com/static/loading.gif)
if ~isfield(s,'u'); s.u=0; end
![](http://static.javashuo.com/static/loading.gif)
if ~isfield(s,'A'); s.A=eye(length(x)); end
![](http://static.javashuo.com/static/loading.gif)
if ~isfield(s,'B'); s.B=0; end
![](http://static.javashuo.com/static/loading.gif)
if ~isfield(s,'Q'); s.Q=zeros(length(x)); end
![](http://static.javashuo.com/static/loading.gif)
if ~isfield(s,'R'); error('Observation covariance missing'); end
![](http://static.javashuo.com/static/loading.gif)
if ~isfield(s,'H'); s.H=eye(length(x)); end
![](http://static.javashuo.com/static/loading.gif)
if isnan(s.x)
![](http://static.javashuo.com/static/loading.gif)
% initialize state estimate from first observation
![](http://static.javashuo.com/static/loading.gif)
if diff(size(s.H))
![](http://static.javashuo.com/static/loading.gif)
error('Observation matrix must be square and invertible for state autointialization.');
![](http://static.javashuo.com/static/loading.gif)
end
![](http://static.javashuo.com/static/loading.gif)
s.x = inv(s.H)*s.z;
![](http://static.javashuo.com/static/loading.gif)
s.P = inv(s.H)*s.R*inv(s.H');
![](http://static.javashuo.com/static/loading.gif)
else
![](http://static.javashuo.com/static/loading.gif)
% This is the code which implements the discrete Kalman filter:
![](http://static.javashuo.com/static/loading.gif)
% Prediction for state vector and covariance:
![](http://static.javashuo.com/static/loading.gif)
s.x = s.A*s.x + s.B*s.u;
![](http://static.javashuo.com/static/loading.gif)
s.P = s.A * s.P * s.A' + s.Q;
![](http://static.javashuo.com/static/loading.gif)
% Compute Kalman gain factor:
![](http://static.javashuo.com/static/loading.gif)
K = s.P*s.H'*inv(s.H*s.P*s.H'+s.R);
![](http://static.javashuo.com/static/loading.gif)
% Correction based on observation:
![](http://static.javashuo.com/static/loading.gif)
s.x = s.x + K*(s.z-s.H*s.x);
![](http://static.javashuo.com/static/loading.gif)
s.P = s.P - K*s.H*s.P;
![](http://static.javashuo.com/static/loading.gif)
% Note that the desired result, which is an improved estimate
![](http://static.javashuo.com/static/loading.gif)
% of the sytem state vector x and its covariance P, was obtained
![](http://static.javashuo.com/static/loading.gif)
% in only five lines of code, once the system was defined. (That's
![](http://static.javashuo.com/static/loading.gif)
% how simple the discrete Kalman filter is to use.) Later,
![](http://static.javashuo.com/static/loading.gif)
% we'll discuss how to deal with nonlinear systems.
![](http://static.javashuo.com/static/loading.gif)
end
![](http://static.javashuo.com/static/loading.gif)
% 狀態
![](http://static.javashuo.com/static/loading.gif)
% xk=A•xk-1+B•uk+wk
![](http://static.javashuo.com/static/loading.gif)
% zk=H•xk+vk,
![](http://static.javashuo.com/static/loading.gif)
% p(w) ~ N(0,Q)
![](http://static.javashuo.com/static/loading.gif)
% p(v) ~ N(0,R),
![](http://static.javashuo.com/static/loading.gif)
% 預測
![](http://static.javashuo.com/static/loading.gif)
% x'k=A•xk+B•uk
![](http://static.javashuo.com/static/loading.gif)
% P'k=A•P(k-1)*AT + Q
![](http://static.javashuo.com/static/loading.gif)
% 修正
![](http://static.javashuo.com/static/loading.gif)
% Kk=P'k•HT•(H•P'k•HT+R)-1
![](http://static.javashuo.com/static/loading.gif)
% xk=x'k+Kk•(zk-H•x'k)
![](http://static.javashuo.com/static/loading.gif)
% Pk=(I-Kk•H)•P'k
![](http://static.javashuo.com/static/loading.gif)
%要注意的是:必須把系統狀態和kalman濾波器內部預測的狀態分開
![](http://static.javashuo.com/static/loading.gif)
function Test
![](http://static.javashuo.com/static/loading.gif)
A=[1 0.1;0 1];
![](http://static.javashuo.com/static/loading.gif)
B=0;
![](http://static.javashuo.com/static/loading.gif)
Xp=rand(2,1)*0.1;
![](http://static.javashuo.com/static/loading.gif)
X=[0 0]';
![](http://static.javashuo.com/static/loading.gif)
H=[1 0];
![](http://static.javashuo.com/static/loading.gif)
Q=eye(2)*1e-5;
![](http://static.javashuo.com/static/loading.gif)
R=eye(1)*0.1;
![](http://static.javashuo.com/static/loading.gif)
P=eye(2);% P'(k)
![](http://static.javashuo.com/static/loading.gif)
angle=[];
![](http://static.javashuo.com/static/loading.gif)
angle_m=[];
![](http://static.javashuo.com/static/loading.gif)
angle_real=[];
![](http://static.javashuo.com/static/loading.gif)
for i=1:500
![](http://static.javashuo.com/static/loading.gif)
angle_real=[angle_real X(1)]; %實際角度
![](http://static.javashuo.com/static/loading.gif)
[Xp,P]=Predict(A,Xp,P,Q);
![](http://static.javashuo.com/static/loading.gif)
X=A*X+rand(2,1)*1e-5;
![](http://static.javashuo.com/static/loading.gif)
z_m=H*X+rand(1,1)*0.1-0.05;
![](http://static.javashuo.com/static/loading.gif)
angle_m=[angle_m z_m(1)]; %測量的角度
![](http://static.javashuo.com/static/loading.gif)
[Xp,P]=Correct(P,H,R,X,z_m);
![](http://static.javashuo.com/static/loading.gif)
angle=[angle Xp(1)]; %預測的角度
![](http://static.javashuo.com/static/loading.gif)
end
![](http://static.javashuo.com/static/loading.gif)
t=1:500;
![](http://static.javashuo.com/static/loading.gif)
plot(t,angle,'r',t,angle_m,'g',t,angle_real,'b')
![](http://static.javashuo.com/static/loading.gif)
legend('預測值','測量值','實際值')
![](http://static.javashuo.com/static/loading.gif)
figure
![](http://static.javashuo.com/static/loading.gif)
plot(t,angle-angle_real,'r',t,angle_m-angle_real,'g')
![](http://static.javashuo.com/static/loading.gif)
legend('濾波後的偏差','測量的偏差')
![](http://static.javashuo.com/static/loading.gif)
title('偏差分析')
![](http://static.javashuo.com/static/loading.gif)
xlabel('time');
![](http://static.javashuo.com/static/loading.gif)
ylabel('error');
![](http://static.javashuo.com/static/loading.gif)
function [Xk,Pk]=Predict(A,Xk,Pk_1,Q)
![](http://static.javashuo.com/static/loading.gif)
Xk=A*Xk;
![](http://static.javashuo.com/static/loading.gif)
Pk=A*Pk_1*A'+Q;
![](http://static.javashuo.com/static/loading.gif)
function [Xk,Pk]=Correct(Pk,H,R,Xk,zk)
![](http://static.javashuo.com/static/loading.gif)
Kk=Pk * H' * inv(H * Pk * H' + R);
![](http://static.javashuo.com/static/loading.gif)
Xk=Xk+ Kk*(zk-H*Xk);
![](http://static.javashuo.com/static/loading.gif)
Pk=(eye(size(Pk,1)) - Kk*H)*Pk;