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];
% 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
t=0:0.1:20;
figure(2);
yp=Y(1,:);
plot(t,yp,'r.-');
axis([0 20 0 20]);
xlabel('time');
ylabel('yp position');
title('the track after kalman filtering');
hold on;
% the diagram of velocity after filtering
figure(3);
yv=Y(2,:);
plot(t,yv,'k.-');
xlabel('time');
ylabel('yv velocity');
title('the velocity caused by random acceleration');