% kalman filtering
load initial_track s; % y:initial data,s:data with noise
T=0.1;
% yp denotes the sample value of position
% yv denotes the sample value of velocity
% Y=[yp(n);yv(n)];
% error deviation caused by the random acceleration
% known data
Y=zeros(2,200);
Y0=[0;1];
Y(:,1)=Y0;
A=[1 T
0 1];
B=[1/2*(T)^2 T]';
H=[1 0];
C0=[0 0
0 1];
C=[C0 zeros(2,2*199)];
Q=(0.25)^2;
R=(0.25)^2;
% kalman algorithm ieration
for n=1:200
i=(n-1)*2+1;
K=C(:,i:i+1)*H'*inv(H*C(:,i:i+1)*H'+R);
Y(:,n)=Y(:,n)+K*(s(:,n)-H*Y(:,n));
Y(:,n+1)=A*Y(:,n);
C(:,i:i+1)=(eye(2,2)-K*H)*C(:,i:i+1);
C(:,i+2:i+3)=A*C(:,i:i+1)*A'+B*Q*B';
end
% the diagram of position after filtering
figure(3)
t=0:0.1:20;
yp=Y(1,:);A
plot(t,yp,'+');
axis([0 20 0 20]);
xlabel('time');
ylabel('yp position');
title('the track after kalman filtering');