clc
close all
clear all
% Set true trajectory
Nsamples=60;
a=2; % acceleration of ship
v0=6; %initial velocity of ship
dt = 0.1; % sampling time
t=0:dt:dt*Nsamples;
Vtrue = v0 + a*t; %m/s (true velocity of ship)
Xinitial = 1000; % initial position of ship
Xtrue = Xinitial -(v0*t + (a/2) * t.^2);% Xtrue is a vector of true positions of the ship
%%% Motion equations
% Previous state (initial guess): Our guess is that the ship starts at 1000 m with velocity
% that equals to 70% of the real velocity
Xk_prev = [1000;
0.7*v0;
a];
% Current state estimate
Xk=[];
% Motion equation: Xk = Phi*Xk_prev + Noise, that is Xk(n) = Xk(n-1) + Vk(n-1) * dt
% here, velocity and acceleration is not measured, but it is estimated
% Phi represents the dynamics of the system: it is the motion equation
Phi = [1 -dt -dt*dt/2; 0 1 dt; 0 0 1];
% The error matrix (or the confidence matrix): P states whether we should
% give more weight to the new measurement or to the model estimate
sigma_model = 1;
% P = sigma^2*G*G';
P = [sigma_model^2 0 0;
0 sigma_model^2 0;
0 0 sigma_model^2];
% Q is the process noise covariance
Q = [0 0 0;
0 0 0;
0 0 0];
% M is the measurement matrix.
% We measure X, so M(1) = 1
% We do not measure V and a, so M(2)= M(3)= 0
M = [1 0 0];
% R is the measurement noise covariance. Generally R and sigma_meas can
% vary between samples.
sigma_meas = 3; % 1 m/sec
R = sigma_meas^2;
%%% Kalman iteration
% Buffers for later display
Xk_buffer = zeros(3,Nsamples+1);
Xk_buffer(:,1) = Xk_prev;
Z_buffer = zeros(1,Nsamples+1);
Z_buffer(1,1)=1000;
for k=1:Nsamples
% Z is the measurement vector. In our
% case, Z = TrueData + RandomGaussianNoise
Z = Xtrue(k+1)+sigma_meas*randn;
Z_buffer(k+1) = Z;
% Kalman iteration
P1 = Phi*P*Phi' + Q;
S = M*P1*M' + R;
% K is Kalman gain. If K is large, more weight goes to the measurement.
% If K is low, more weight goes to the model prediction.
K = P1*M'*inv(S)
P = P1 - K*M*P1
Xk = Phi*Xk_prev + K*(Z-M*Phi*Xk_prev);
Xk_buffer(:,k+1) = Xk;
% For the next iteration
Xk_prev = Xk;
end;
%%% Plot resulting graphs
figure;
plot(t,Xtrue-Z_buffer,'g','Linewidth',2);
hold on;
% plot(t,Z_buffer,'c');
plot(t,Xtrue-Xk_buffer(1,:),'--m','Linewidth',2);
title('Position estimation results');
xlabel('Time (s)');
ylabel('Error (m)');
% legend('True position','Measurement','Kalman estimated');
legend('Possition Error in case of Without Filtering','Error in case of Kalman Estimation');
grid on;
figure;
plot(t,Xtrue,'--g','Linewidth',2);
hold on;
plot(t,Z_buffer,'c','Linewidth',2);
plot(t,Xk_buffer(1,:),'.m','Linewidth',2);
title('Position estimation results');
xlabel('Time (s)');
ylabel('Position (m)');
legend('True position','Without Filtering','Kalman estimated');
% legend('Error of Measurement','Error of estimation');
grid on;
%%% Velocity analysis
% The instantaneous velocity as derived from 2 consecutive position
% measurements
InstantaneousVelocity = [-9 (Z_buffer(2:Nsamples+1)-Z_buffer(1:Nsamples))/dt];
figure;
plot(t,Vtrue,'m','Linewidth',2);
hold on;
plot(t,-InstantaneousVelocity,'--g','Linewidth',2);
plot(t,Xk_buffer(2,:),'.k','Linewidth',2);
title('Velocity estimation results');
xlabel('Time (s)');
ylabel('Velocity (m/s)');
legend('True velocity','Estimated velocity by raw consecutive samples','Estimated velocity by Kalman filter');
%%% Position analysis
%
% figure;
% plot(t,Xtrue,'g');
% hold on;
% plot(t,Z_buffer,'c');
%
% plot(t,Xk_buffer(1,:),'m');
% title('Position estimation results');
% xlabel('Time (s)');
% ylabel('Position (m)');
% legend('True position','Measurement','Kalman estimated');
%
% % %%% Velocity analysis
% %
% % % The instantaneous velocity as derived from 2 consecutive position
% % % measurements
% InstantaneousVelocity = [-9 (Z_buffer(2:Nsamples+1)-Z_buffer(1:Nsamples))/dt];
%
% figure;
% plot(t,Vtrue,'m');
% hold on;
% plot(t,-InstantaneousVelocity,'g');
% plot(t,Xk_buffer(2,:),'k');
% title('Velocity estimation results');
% xlabel('Time (s)');
% ylabel('Velocity (m/s)');
% legend('True velocity','Estimated velocity by raw consecutive samples','Estimated velocity by Kalman filter');
alvarocfc
- 粉丝: 134
- 资源: 1万+
最新资源
- 工作备忘录【按日期显示备忘录】.xlsm
- ibm 磁带库的驱动程序
- docker-hadoop-spark-hive 快速构建你的大数据环境.zip
- 基于LOS制导与自适应反步控制律的欠驱动无人船 艇路径跟踪控制(Path Following)系统仿真:LOS艏向角与速度的双重制导方案;基于自适应模糊逻辑系统逼近外界环境以及自身建模不确定项和参数摄
- 正点原子脱机下载软件及驱动
- ibm 磁带库的驱动程序
- echarts市县级地图数据大概334个.zip
- HFSS 3D LAYOUT v2.2:PCB与封装的全波三维电磁场仿真用户手册
- ibm 磁带库的驱动程序 for win 2012 2016
- 采用离散元(pfc)建立考虑颗粒破碎的cluster土石混合体松散地基贯入振动密实模拟二维模型 土石级配可调整、块石形状可调整,可监测应力、位移、孔隙比等参数变化
- ibm 磁带库的驱动程序 for win 2008
- 基于ASP.NET的MVC网上商城系统(对信息的增删改查)源码+数据库(高分项目)
- ibm 磁带库的驱动程序
- 毕设-java-swing-汽车租赁管理系统(详细文档+视频+源码)4.zip
- 基于Learning MPC(LMPC)的四旋翼飞行器(VAU)的避障路径规划 本模型建立一种学习模型预测控制(LMPC)的四旋翼飞行器VAU的控制算法 该控制器可以从数据中学习并找到最佳路径轨迹
- FBP项目全称FootBallPrediction,历经9个月完成的足球比赛预测项目 项目结合大数据+机器学习,不断摸索开发了一个程序 .zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈