用卡尔曼滤波器跟踪导弹(给出matlab和python的仿真代码)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了用卡尔曼滤波器跟踪导弹(给出matlab和python的仿真代码),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7268字,纯文字阅读大概需要11分钟。
内容图文
![用卡尔曼滤波器跟踪导弹(给出matlab和python的仿真代码)](/upload/InfoBanner/zyjiaocheng/590/c3509bdd4bf04ffb8c88f125ff06c812.jpg)
题目
巡航导弹沿直线飞向目标,目标处设有一监视雷达,雷达对导弹的距离进行观测。
假设:(1)导弹初始距离
100
k
m
100km
100km,速度约为
300
m
/
s
300m/s
300m/s,基本匀速飞行,但受空气扰动影响,扰动加速度为零均值白噪声,方差强度
q
=
0.05
m
2
/
s
3
q=0.05m^2/s^3
q=0.05m2/s3 ;(2)雷达观测频率
2
H
z
2Hz
2Hz,观测误差为零均值白噪声,均方差为
50
m
50m
50m。
试使用卡尔曼滤波(Kalman filter)完成导弹的轨迹跟踪。
matlab仿真代码
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 功能描述:导弹运动Kalman滤波程序
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;clc;close all;
%% 01 初始化参数
T = 300;
Ts = 1/2; % 采样时间
Q = 0.05*Ts; % 过程噪声
R = 50/Ts; % 量测噪声
W = sqrt(Q)*randn(1,T);
V = sqrt(R)*randn(1,T);
P0 = diag([10^2,1^2]);
% 系统矩阵
Phi = [1,-Ts;0,1];
H = [1,0];
Tau = [0.5*Ts^2;Ts];
% 初始化
nS = 2; nZ = 1;
xState = zeros(nS,T);
zMea = zeros(nZ,T);
xKF = zeros(nS,T);
xKF_pre = zeros(nS,T);
xState(:,1) = [100000;300];
zMea(:,1) = H*xState(:,1);
xKF(:,1) = xState(:,1);
%% 02 用模型模拟真实状态
for t = 2:T
xState(:,t) = Phi*xState(:,t-1) + Tau*W(:,t);
zMea(:,t) = H*xState(:,t) + V(t);
end
%% 03 Kalman滤波
for t = 2:T
xKF_pre(:,t) = Phi*xKF(:,t-1);
P_pre = Phi*P0*Phi' + Tau*Q*Tau';
K = P_pre*H'*pinv(H*P_pre*H'+R);
xKF(:,t) = xKF_pre(:,t) + K*(zMea(:,t)-H*xKF_pre(:,t));
P0 = (eye(2)-K*H)*P_pre;
end
%% 04 画图
tPlot = 1:T;
FigWin1=figure('position',[300 400 550 450],'Color',[0.8 0.8 0.8],...
'Name','01-量测距离误差与估计距离误差的比较','NumberTitle','off');hold on;box on;
plot(tPlot,xState(1,:)-zMea(1,:),'-b','LineWidth',1.5);hold on;
plot(tPlot,xState(1,:)-xKF(1,:),'-r','LineWidth',1.5);hold on;
xlabel('时间 t/s');ylabel('误差距离 m');
legend('量测距离误差','估计距离误差');
title('量测距离误差与估计距离误差的比较');
FigWin2=figure('position',[850 400 550 450],'Color',[0.8 0.8 0.8],...
'Name','02-真实速度与估计速度的比较','NumberTitle','off');hold on;box on;
plot(tPlot,xState(2,:)-xKF(2,:),'-b','LineWidth',1.5);hold on;
xlabel('时间 t/s');ylabel('误差速度 m/s');
title('真实速度与估计速度的比较');
FigWin3=figure('position',[300 200 550 450],'Color',[0.8 0.8 0.8],...
'Name','03-导弹真实轨迹与雷达量测','NumberTitle','off');hold on;box on;
plot(tPlot,xState(1,:),'-b','LineWidth',1.5);hold on;
plot(tPlot,zMea(1,:),'-r','LineWidth',1.5);hold on;
xlabel('时间 t/s');ylabel('距离 m');
legend('导弹真实轨迹','雷达量测轨迹');
title('导弹真实轨迹与雷达量测轨迹');
FigWin4=figure('position',[850 200 550 450],'Color',[0.8 0.8 0.8],...
'Name','04-估计距离与估计速度','NumberTitle','off');hold on;box on;
subplot(211);
plot(tPlot,xKF(1,:),'-b','LineWidth',1.5);hold on;
xlabel('时间 t/s');ylabel('估计距离 m');
subplot(212);
plot(tPlot,xKF(2,:),'-r','LineWidth',1.5);hold on;
xlabel('时间 t/s');ylabel('估计速度 m/s');
title('估计距离与估计速度');
仿真结果图如下所示。
python仿真代码
"""
#-------------------------------------------------------------------#
# Li Lingwei's Python Code #
#-------------------------------------------------------------------#
# #
# @Project Name : Matlab to python #
# @File Name : Kamlan.py #
# @Programmer : Li Lingwei #
# @Start Date : 2021/4/18 #
# @Last Update : 2021/4/26 #
#-------------------------------------------------------------------#
# Function: #
# #
#-------------------------------------------------------------------#
"""
import numpy as np
import pylab as plt
"""01 初始化参数"""
T = 300
Ts = 1 / 2 # 采样时间
Q = 0.05 * Ts # 过程噪声
R = 50 / Ts # 量测噪声
W = np.sqrt(Q) * np.random.randn(1, T)
V = np.sqrt(R) * np.random.randn(1, T)
P0 = np.diag([10 ** 2, 1 ** 2])
# 系统矩阵
Phi = np.mat([[1, -Ts],
[0, 1]])
H = np.mat([1, 0])
Tau = np.mat([[0.5 * (Ts ** 2)],
[Ts]])
# 初始化
nS = 2
nZ = 1
xState = np.zeros([nS, T])
xState = np.mat(xState)
zMea = np.zeros([nZ, T])
zMea = np.mat(zMea)
xKF = np.zeros([nS, T])
xKF = np.mat(xKF)
xKF_pre = np.zeros([nS, T])
xKF_pre = np.mat(xKF_pre)
xState[:, 0] = [[100000], [300]]
xKF[:, 0] = xState[:, 0]
zMea[:, 0] = H * xState[:, 0]
"""02 用模型模拟真实状态"""
for t in range(1, T):
xState[:, t] = Phi * xState[:, t - 1] + Tau * W[:, t]
zMea[:, t] = H * xState[:, t] + V[:, t]
"""03 Kalman滤波"""
for t in range(1, T):
xKF_pre[:, t] = Phi * xKF[:, t - 1]
P_pre = Phi * P0 * Phi.T + Tau * Q * Tau.T
K = P_pre * H.T * np.linalg.pinv(H * P_pre * H.T + R)
xKF[:, t] = xKF_pre[:, t] + K * (zMea[:, t] - H * xKF_pre[:, t])
P0 = (np.identity(nS) - K * H) * P_pre
"""04 画图"""
font = {'family': 'Times New Roman',
'weight': 'normal',
'size': 10}
fontTitle = {'family': 'Times New Roman',
'weight': 'normal',
'size': 12}
# 变成数组才可以画图
t = [t for t in range(1, T+1)]
xStatePlot = xState.getA()
xKFPlot = xKF.getA()
zMeaPlot = zMea.getA()
# 01 量测距离误差与估计距离误差的比较
plt.figure(1)
plt.plot(t, xStatePlot[0, :]-zMeaPlot[0, :], 'b', linewidth=1, label='Position error of measurement')
plt.plot(t, xStatePlot[0, :]-xKFPlot[0, :], 'r', linewidth=1, label='Position error of estimation')
plt.title('Position error', fontdict=fontTitle)
plt.ylabel('Position (m)', fontdict=font)
plt.tick_params(pad=3)
plt.yticks(fontproperties='Times New Roman', size=10)
plt.xticks(fontproperties='Times New Roman', size=10)
plt.xlim(0, T)
plt.legend(prop=font)
# 02 真实速度与估计速度的比较
plt.figure(2)
plt.plot(t, xStatePlot[1, :]-xKFPlot[1, :], 'r', linewidth=1)
plt.title('Velocity error', fontdict=fontTitle)
plt.xlabel('Time (s)', fontdict=font), plt.ylabel('Velocity (m/s)', fontdict=font)
plt.tick_params(pad=3)
plt.yticks(fontproperties='Times New Roman', size=10)
plt.xticks(fontproperties='Times New Roman', size=10)
plt.xlim(0, T)
# 03 导弹真实轨迹与雷达量测
plt.figure(3)
plt.plot(t, xStatePlot[0, :], 'b', linewidth=1, label='True trajectory')
plt.plot(t, zMeaPlot[0, :], 'r', linewidth=1, label='Measure trajectory')
plt.title('Velocity error', fontdict=fontTitle)
plt.xlabel('Time (s)', fontdict=font), plt.ylabel('Velocity (m/s)', fontdict=font)
plt.tick_params(pad=3)
plt.yticks(fontproperties='Times New Roman', size=10)
plt.xticks(fontproperties='Times New Roman', size=10)
plt.xlim(0, T)
plt.legend(prop={'family': 'Times New Roman', 'size': 10})
# 04 估计距离与估计速度
plt.figure(4)
plt.subplot(2, 1, 1)
plt.plot(t, xKFPlot[0, :], 'b', linewidth=1)
plt.title('Position', fontdict=fontTitle)
plt.ylabel('Position (m)', fontdict=font)
plt.tick_params(pad=3)
plt.yticks(fontproperties='Times New Roman', size=10)
plt.xticks([], fontproperties='Times New Roman', size=10)
plt.xlim(0, T)
plt.subplot(2, 1, 2)
plt.plot(t, xKFPlot[1, :], 'r', linewidth=1)
plt.title('Velocity', fontdict=fontTitle)
plt.xlabel('Time (s)', fontdict=font), plt.ylabel('Velocity (m/s)', fontdict=font)
plt.tick_params(pad=3)
plt.yticks(fontproperties='Times New Roman', size=10)
plt.xticks(fontproperties='Times New Roman', size=10)
plt.xlim(0, T)
plt.show()
仿真结果图如下所示。
?
?
?
E
n
d
?
?
?
---End---
???End???
内容总结
以上是互联网集市为您收集整理的用卡尔曼滤波器跟踪导弹(给出matlab和python的仿真代码)全部内容,希望文章能够帮你解决用卡尔曼滤波器跟踪导弹(给出matlab和python的仿真代码)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。