|
卡尔曼滤波MATlab程序:
% modeling data
% sample number N=200 points
% sample time T=0.1s
t=0:0.1:20-0.1;
T=0.1; % sample time
v=0.5; % initial velocity
y=ones(1,200);
y(1)=0;
for n=2:200;
y(n)=y(n-1)+v*T;
end
figure(1);
plot(t,y,'-');
axis([0 20 0 20]);
xlabel('time');
ylabel('yp position');
title('the initial track of movement');
![](http://cache.amobbs.com/bbs_upload782111/files_47/ourdev_697050GOSZPM.jpg)
(原文件名:初始轨迹.jpg)
% add white gauss noise
a=0.5*randn(1,200);
s=y+a; %data+noise
figure(2);
plot(t,s,'-');
axis([0 20 0 20]);
xlabel('time');
ylabel('yp position');
title('the track of movement with noise');
save initial_track s
![](http://cache.amobbs.com/bbs_upload782111/files_47/ourdev_697051Y1DG37.jpg)
(原文件名:加入高斯白噪声后的轨迹.jpg)
% 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]; %cov 初始值
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,:);
plot(t,yp,'-');
axis([0 20 0 20]);
xlabel('time');
ylabel('yp position');
title('the track after kalman filtering');
![](http://cache.amobbs.com/bbs_upload782111/files_47/ourdev_697054PQVZ9P.jpg)
(原文件名:加入噪声并卡尔曼滤波后波形.jpg)
% the diagram of velocity after filtering
figure(4)
yv=Y(2,:);
plot(t,yv,'-');
xlabel('time');
ylabel('yv velocity');
title('the velocity caused by random acceleration');
![](http://cache.amobbs.com/bbs_upload782111/files_47/ourdev_697052UEX5O6.jpg)
(原文件名:随机误差对角速率的影响.jpg) |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|