% Please cite the following book chapter if you find this code helpful.
%%% Y. Kim and H. Bang, Introduction to Kalman Filter and Its Applications, InTechOpen, 2018
% Example 3.3.1 - Target tracking
close all
clc
clear
%% settings
N = 20; % number of time steps
dt = 1; % time between time steps
M = 100; % number of Monte-Carlo runs
sig_mea_true = [0.02; 0.02; 1.0]; % true value of standard deviation of measurement noise
sig_pro = [5; 5; 5]; % user input of standard deviation of process noise
sig_mea = [0.02; 0.02; 1.0]; % user input of standard deviation of measurement noise
sig_init = [1; 1; 0; 0; 0; 0]; % standard deviation of initial guess
Q = [zeros(3), zeros(3); zeros(3), diag(sig_pro.^2)]; % process noise covariance matrix
R = diag(sig_mea.^2); % measurement noise covariance matrix
F = [eye(3), eye(3)*dt; zeros(3), eye(3)]; % state transition matrix
B = eye(6); % control-input matrix
u = zeros(6,1); % control vector
H = zeros(3, 6); % measurement matrix - to be determined
%% true trajectory
% sensor trajectory
p_sensor = zeros(3,N+1);
for k = 1:1:N+1
p_sensor(1,k) = 20 + 20*cos(2*pi/30 * (k-1));
p_sensor(2,k) = 20 + 20*sin(2*pi/30 * (k-1));
p_sensor(3,k) = 50;
end
% true target trajectory
x_true = zeros(6,N+1);
x_true(:,1) = [10; -10; 0; -1; -2; 0]; % initial true state
for k = 2:1:N+1
x_true(:,k) = F*x_true(:,k-1) + B*u;
end
%% extended Kalman filter simulation
res_x_est = zeros(6,N+1,M); % Monte-Carlo estimates
res_x_err = zeros(6,N+1,M); % Monte-Carlo estimate errors
P_diag = zeros(6,N+1); % diagonal term of error covariance matrix
% filtering
for m = 1:1:M
% initial guess
x_est(:,1) = x_true(:,1) + normrnd(0, sig_init);
P = [eye(3)*sig_init(1)^2, zeros(3); zeros(3), eye(3)*sig_init(4)^2];
P_diag(:,1) = diag(P);
for k = 2:1:N+1
%%% Prediction
% predicted state estimate
x_est(:,k) = F*x_est(:,k-1) + B*u;
% predicted error covariance
P = F*P*F' + Q;
%%% Update
% obtain measurement
p = x_true(1:3,k) - p_sensor(:,k); % true relative position
z_true = [atan2(p(1), p(2));
atan2(p(3), sqrt(p(1)^2 + p(2)^2));
norm(p)]; % true measurement
z = z_true + normrnd(0, sig_mea); % erroneous measurement
% predicted meausrement
pp = x_est(1:3,k) - p_sensor(:,k); % predicted relative position
z_p = [atan2(pp(1), pp(2));
atan2(pp(3), sqrt(pp(1)^2 + pp(2)^2));
norm(pp)]; % predicted measurement
% measurement residual
y = z - z_p;
% measurement matrix
H = [pp(2)/(pp(1)^2+pp(2)^2), -pp(1)/(pp(1)^2+pp(2)^2), 0, zeros(1,3);
-pp(1)*pp(3)/(pp'*pp)/norm(pp(1:2)), -pp(2)*pp(3)/(pp'*pp)/norm(pp(1:2)), 1/norm(pp(1:2)), zeros(1,3);
pp(1)/norm(pp), pp(2)/norm(pp), pp(3)/norm(pp), zeros(1,3)];
% Kalman gain
K = inv(P)*H'/(R+H*inv(P)*H');
% updated state estimate
x_est(:,k) = x_est(:,k) + K*y;
% updated error covariance
P = (eye(6) - K*H)*P;
P_diag(:,k) = diag(P);
end
res_x_est(:,:,m) = x_est;
res_x_err(:,:,m) = x_est - x_true;
end
%% get result statistics
x_est_avg = mean(res_x_est,3);
x_err_avg = mean(res_x_err,3);
x_RMSE = zeros(6,N+1); % root mean square error
for k = 1:1:N+1
x_RMSE(1,k) = sqrt(mean(res_x_err(1,k,:).^2,3));
x_RMSE(2,k) = sqrt(mean(res_x_err(2,k,:).^2,3));
x_RMSE(3,k) = sqrt(mean(res_x_err(3,k,:).^2,3));
x_RMSE(4,k) = sqrt(mean(res_x_err(4,k,:).^2,3));
x_RMSE(5,k) = sqrt(mean(res_x_err(5,k,:).^2,3));
x_RMSE(6,k) = sqrt(mean(res_x_err(6,k,:).^2,3));
end
%% plot results
time = (0:1:N)*dt;
figure
subplot(2,1,1); hold on;
plot(time, x_true(1,:), 'linewidth', 2);
plot(time, res_x_est(1,:,1), '--', 'linewidth', 2);
legend({'True', 'Estimated'}, 'fontsize', 12);
ylabel('X position', 'fontsize', 12); grid on;
subplot(2,1,2); hold on;
plot(time, x_true(4,:), 'linewidth', 2);
plot(time, res_x_est(4,:,1), '--', 'linewidth', 2);
ylabel('X velocity', 'fontsize', 12); xlabel('Time', 'fontsize', 12); grid on;
figure
subplot(2,1,1); hold on;
plot(time, x_RMSE(1,:), 'linewidth', 2);
plot(time, sqrt(P_diag(1,:)), '--', 'linewidth', 2);
legend({'RMSE', 'Estimated'}, 'fontsize', 12);
ylabel('X position error std', 'fontsize', 12); grid on;
subplot(2,1,2); hold on;
plot(time, x_RMSE(4,:), 'linewidth', 2);
plot(time, sqrt(P_diag(4,:)), '--', 'linewidth', 2);
ylabel('X velocity error std', 'fontsize', 12); xlabel('Time', 'fontsize', 12); grid on;
figure
plot(p_sensor(1,:), p_sensor(2,:), 'linewidth', 2); hold on;
plot(x_true(1,:), x_true(2,:), 'linewidth', 2); hold on;
legend({'Sensor', 'Target'}, 'fontsize', 12);
xlabel('X', 'fontsize', 12); ylabel('Y', 'fontsize', 12);
axis equal;


阿里matlab建模师

- 粉丝: 5920
最新资源
- 2015年电子商务农业协会微信推广方案.doc
- 计算机技术与科学在未来的具体发展趋势.docx
- iPhone4游戏软件安装方法.doc
- 跨平台的安全运维建设实践-青藤云安全.pdf
- 浅析计算机信息处理技术在办公自动化中的应用.docx
- 计算机网络技术在计生药具档案管理中的价值分析.docx
- 公路工程项目管理模式探讨.docx
- 区块链+保险会是什么样子?有哪些项目?.docx
- VBACCESS数据库应用课程研究设计贸易公司管理信息系统【总报告】.doc
- VMware-vSphere虚拟化实施手册及故障案例.docx
- 现代消防通信新技术.doc
- 信息化环境下小学英语写作教学策略探究.docx
- 网络课程视频资源制作的形式美学思考.docx
- 会计信息化下内部控制审计探究.docx
- 网络计划技术在工程管理中的应用.docx
- Flash制作方法.ppt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
前往页