拓展卡尔曼滤波(Kalman)附Matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab仿真内容点击👇

智能优化算法  神经网络预测 雷达通信  无线传感器

信号处理 图像处理 路径规划 元胞自动机 无人机  电力系统

⛄ 内容介绍

根据传统的扩展卡尔曼厚度算法在多普勒测量目标情况下估计精度低的,提出了扩展卡尔曼厚度跟踪优化算法。估计量测的扩展卡算法推广到包含普勒量测的提高目标跟踪位置精确度。仿真结果主动,算法以多方均方根的精度和方根的速度精度,可以很容易地实现精度良好地提高目标跟踪的精确度,可有效持续跟踪目标中的情况。

⛄ 部分代码

clear; close all; format compact;

addpath('filters');

addpath('helper');

addpath('thirdparty/shadedErrorBar');

% -------------------------------------------------------------------------

% Fake data time limits and resolution lower resolution with noise free

% data should cause the prediction to improve.

time.tmin = 0;

time.tmax = 2;

time.dt = 1e-3;

% Covariance for sensor noise set noise.add_noise = false 

% to have perfect noise free data.

% If add_noise if false, accel_noise and gyro_noise can be anything.

%

% Q1 and Q2 are two random matrices to try to create really bad noise

% with a ton of cross corelation between states

rng(2)

noise.add_noise = true;

m = 100;

Q1 = randn(3,3);

Q2 = randn(3,3);

noise.accel_noise = (6*9.81)^2*eye(3);

noise.gyro_noise = eye(3);

% Set the frequency of the correction step (Hz)

%  - Increase the frequency to test robustness of filter

%    to slow updates

f_cor = 1;

dt_cor = 1/f_cor;

% Generate the fake data see function definition for input and output

% variable definitions.

[omega, accel, ~, ~, gt, init, wf_data] = gen_fake_data(time, noise);

% Set the time data from the accelerometer

t = accel.t;

N = length(t);

% -------------------------------------------------------------------------

% Initialize the solution / gt vectors

p_gt = [gt.x;gt.y;gt.z];

theta_gt = zeros(3, N);

theta_gt(:,1) = Log(gt.R{1});

p_ekf = zeros(3,N);

p_ekf_var = zeros(3,N);

theta_ekf = zeros(3, N);

theta_ekf_var = zeros(3,N);

p_liekf = zeros(3,N);

p_liekf_var = zeros(3,N);

theta_liekf = zeros(3, N);

theta_liekf_var = zeros(3,N);

% -------------------------------------------------------------------------

% Initialize the filter (with initial condition)

% Note: the polynomial function created by gen_fake_data almost definitely

% wont be zero at t = 0

ekf = EKF(rotm2eul(init.R0)', init.p0, init.v0, eye(3) * 100);

p_ekf(:,1) = ekf.mu(1:3);

p_ekf_var(:,1) = sqrt(diag(ekf.Sigma(1:3,1:3)));

theta_ekf(:,1) = ekf.mu(7:9);

theta_ekf_var(:,1) = sqrt(diag(ekf.Sigma(7:9,7:9)));

% -------------------------------------------------------------------------

% Run the simulation on the data

t_cor = t(1);  %Time of first correction

for i = 1:N-1

    % Set dt off time data

    dt = t(i+1) - t(i);

    % Set the acceleration from the fake data

    a = [accel.x(i); accel.y(i); accel.z(i)];

    w = [omega.x(i); omega.y(i); omega.z(i)];

    

    % Run the ekf prediction step

    ekf.prediction(w, a, dt);

    

    % Run the ekf correction step

    if t(i) >= t_cor

        gps = [gt.x(i); gt.y(i); gt.z(i)];

        ekf.correction(gps)

        % Next correct at t + dt_cor

        t_cor = t(i) + dt_cor;

    end

    

    % Save the outputs (for plotting)

    variances = sqrt(diag(ekf.Sigma));

    p_ekf(:,i+1) = ekf.mu(1:3);

    theta_ekf(:,i+1) = Log(eul2rotm(ekf.mu(7:9)'));

    p_ekf_var(:,i+1) = variances(1:3);

    theta_ekf_var(:,i+1) = variances(7:9);

    

    theta_gt(:,i+1) = Log(gt.R{i});

end

% -------------------------------------------------------------------------

% LIEKF

liekf = LIEKF(init.R0, init.p0, init.v0, ...

    eye(3)*.01, eye(3)*.01, eye(3)*.01, eye(3)*.01, eye(3)*.01);

lieTocartesian(liekf)

vars = sqrt(diag(liekf.sigma_cart));

p_liekf(:,1) = init.p0;

p_liekf_var(:,1) = vars(1:3);

theta_liekf(:,1) = Log(liekf.mu(1:3,1:3));

theta_liekf_var(:,1) = vars(1:3);

% Run the simulation on the data

t_cor = t(1);  %Time of first correction

for i = 1:N-1

    % Set dt off time data

    dt = t(i+1) - t(i);

    % Set the acceleration from the fake data

    a = [accel.x(i); accel.y(i); accel.z(i)];

    w = [omega.x(i); omega.y(i); omega.z(i)];

    

    % Run the ekf prediction step

    liekf.prediction(w, a, dt);

    

    % Run the ekf correction step

    if t(i) >= t_cor

        gps = [gt.x(i); gt.y(i); gt.z(i)];

        liekf.correction(gps)

        % Next correct at t + dt_cor

        t_cor = t(i) + dt_cor;

    end

    % Extract the state from the filter

    [R, p, v] = liekf.getState(); 

    lieTocartesian(liekf)

    

    % Save the outputs (for plotting)

    p_liekf(:,i+1) = p;

    theta_liekf(:,i+1) = Log(R);

    

    vars = sqrt(diag(liekf.sigma_cart));

    p_liekf_var(:,i+1) = vars(7:9);

    theta_liekf_var(:,i+1) = vars(1:3);

end

% -------------------------------------------------------------------------

% Plot position and theta data to visualize

% the operation of the filter

figure;

subplot(311)

hold('on')

plot(t, p_gt(1,:), 'k--', 'LineWidth', 2);

plot(t, p_ekf(1,:), 'g', 'LineWidth', 1);

plot(t, p_liekf(1,:), 'r', 'LineWidth', 1);

axis([0,2,-200,200])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

title("Position");

subplot(312)

hold('on')

plot(t, p_gt(2,:),  'k--', 'LineWidth', 2)

plot(t, p_ekf(2,:), 'g', 'LineWidth', 1);

plot(t, p_liekf(2,:), 'r', 'LineWidth', 1)

axis([0,2,-400,0])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

subplot(313)

hold('on')

plot(t, p_gt(3,:), 'k--', 'LineWidth', 2)

plot(t, p_ekf(3,:), 'g', 'LineWidth', 1);

plot(t, p_liekf(3,:), 'r', 'LineWidth', 1)

axis([0,2,-300,100])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

print('position_noise', '-dpng')

figure;

subplot(311)

hold('on')

plot(t, theta_gt(1,:), 'k--', 'LineWidth', 2);

plot(t, theta_ekf(1,:), 'g', 'LineWidth', 1);

plot(t, theta_liekf(1,:), 'r', 'LineWidth', 1);

axis([0,2,-7,7])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

title("Theta");

subplot(312)

hold('on')

plot(t, theta_gt(2,:), 'k--', 'LineWidth', 2)

plot(t, theta_ekf(2,:), 'g', 'LineWidth', 1);

plot(t, theta_liekf(2,:), 'r', 'LineWidth', 1)

axis([0,2,-7,7])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

subplot(313)

hold('on')

plot(t, theta_gt(3,:),  'k--', 'LineWidth', 2)

plot(t, theta_ekf(3,:), 'g', 'LineWidth', 1);

plot(t, theta_liekf(3,:), 'r', 'LineWidth', 1)

axis([0,2,-7,7])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

print('theta_noise', '-dpng')

% -------------------------------------------------------------------------

% Plot position and theta data to visualize

% the operation of the filter

figure;

subplot(311)

hold('on')

plot(t, p_gt(1,:), 'k--', 'LineWidth', 2);

%plot(t, p_ekf(1,:), 'g', 'LineWidth', 1);

%plot(t, p_liekf(1,:), 'r', 'LineWidth', 1);

%plot(t, p_ekf(1,:)+3*p_ekf_var(1,:), 'b', 'LineWidth', 1);

%plot(t, p_ekf(1,:)-3*p_ekf_var(1,:), 'b', 'LineWidth', 1);

%plot(t, p_liekf(1,:)+3*p_liekf_var(1,:), 'm', 'LineWidth', 1);

%plot(t, p_liekf(1,:)-3*p_liekf_var(1,:), 'm', 'LineWidth', 1);

shadedErrorBar(t, p_ekf(1,:), 3*p_ekf_var(1,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, p_liekf(1,:), 3*p_liekf_var(1,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-200,200])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

title("Position");

subplot(312)

hold('on')

plot(t, p_gt(2,:),  'k--', 'LineWidth', 2)

%plot(t, p_ekf(2,:), 'g', 'LineWidth', 1);

%plot(t, p_liekf(2,:), 'r', 'LineWidth', 1)

%plot(t, p_ekf(2,:)+3*p_ekf_var(2,:), 'b', 'LineWidth', 1);

%plot(t, p_ekf(2,:)-3*p_ekf_var(2,:), 'b', 'LineWidth', 1);

%plot(t, p_liekf(2,:)+3*p_liekf_var(2,:), 'm', 'LineWidth', 1);

%plot(t, p_liekf(2,:)-3*p_liekf_var(2,:), 'm', 'LineWidth', 1);

shadedErrorBar(t, p_ekf(2,:), 3*p_ekf_var(2,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, p_liekf(2,:), 3*p_liekf_var(2,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-400,0])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

subplot(313)

hold('on')

plot(t, p_gt(3,:), 'k--', 'LineWidth', 2)

% plot(t, p_ekf(3,:), 'g', 'LineWidth', 1);

% plot(t, p_liekf(3,:), 'r', 'LineWidth', 1)

% plot(t, p_ekf(3,:)+3*p_ekf_var(3,:), 'b', 'LineWidth', 1);

% plot(t, p_ekf(3,:)-3*p_ekf_var(3,:), 'b', 'LineWidth', 1);

% plot(t, p_liekf(3,:)+3*p_liekf_var(3,:), 'm', 'LineWidth', 1);

% plot(t, p_liekf(3,:)-3*p_liekf_var(3,:), 'm', 'LineWidth', 1);

shadedErrorBar(t, p_ekf(3,:), 3*p_ekf_var(3,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, p_liekf(3,:), 3*p_liekf_var(3,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-300,100])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

print('position_noise_mean_cov', '-dpng')

figure;

subplot(311)

hold('on')

plot(t, theta_gt(1,:), 'k--', 'LineWidth', 2);

plot(t, theta_ekf(1,:), 'g', 'LineWidth', 1);

plot(t, theta_liekf(1,:), 'r', 'LineWidth', 1);

%plot(t, theta_ekf(1,:)+3*theta_ekf_var(1,:), 'b', 'LineWidth', 1);

%plot(t, theta_ekf(1,:)-3*theta_ekf_var(1,:), 'b', 'LineWidth', 1);

%plot(t, theta_liekf(1,:)+3*theta_liekf_var(1,:), 'm', 'LineWidth', 1);

%plot(t, theta_liekf(1,:)-3*theta_liekf_var(1,:), 'm', 'LineWidth', 1);

shadedErrorBar(t, theta_ekf(1,:), 3*theta_ekf_var(1,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, theta_liekf(1,:), 3*theta_liekf_var(1,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-7,7])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

title("Theta");

subplot(312)

hold('on')

plot(t, theta_gt(2,:), 'k--', 'LineWidth', 2)

%plot(t, theta_ekf(2,:), 'g', 'LineWidth', 1);

%plot(t, theta_liekf(2,:), 'r', 'LineWidth', 1)

%plot(t, theta_ekf(2,:)+3*theta_ekf_var(2,:), 'b', 'LineWidth', 1);

%plot(t, theta_ekf(2,:)-3*theta_ekf_var(2,:), 'b', 'LineWidth', 1);

%plot(t, theta_liekf(2,:)+3*theta_liekf_var(2,:), 'm', 'LineWidth', 1);

%plot(t, theta_liekf(2,:)-3*theta_liekf_var(2,:), 'm', 'LineWidth', 1);

shadedErrorBar(t, theta_ekf(2,:), 3*theta_ekf_var(2,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, theta_liekf(2,:), 3*theta_liekf_var(2,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-7,7])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

subplot(313)

hold('on')

plot(t, theta_gt(3,:),  'k--', 'LineWidth', 2)

%plot(t, theta_ekf(3,:), 'g', 'LineWidth', 1);

%plot(t, theta_liekf(3,:), 'r', 'LineWidth', 1)

%plot(t, theta_ekf(3,:)+3*theta_ekf_var(3,:), 'b', 'LineWidth', 1);

%plot(t, theta_ekf(3,:)-3*theta_ekf_var(3,:), 'b', 'LineWidth', 1);

%plot(t, theta_liekf(3,:)+3*theta_liekf_var(3,:), 'm', 'LineWidth', 1);

%plot(t, theta_liekf(3,:)-3*theta_liekf_var(3,:), 'm', 'LineWidth', 1);

shadedErrorBar(t, theta_ekf(3,:), 3*theta_ekf_var(3,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, theta_liekf(3,:), 3*theta_liekf_var(3,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-7,7])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

print('theta_noise_mean_cov', '-dpng')

% -------------------------------------------------------------------------

% Plot position and theta data to visualize

% the operation of the filter

figure;

subplot(311)

hold('on')

plot(t, zeros(size(p_gt(1,:))), 'k--', 'LineWidth', 2);

shadedErrorBar(t, zeros(size(p_ekf(1,:))), 3*p_ekf_var(1,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, zeros(size(p_liekf(1,:))), 3*p_liekf_var(1,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-200,200])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

title("Position 3 Standard Deviations");

subplot(312)

hold('on')

plot(t, zeros(size(p_gt(2,:))), 'k--', 'LineWidth', 2);

shadedErrorBar(t, zeros(size(p_ekf(1,:))), 3*p_ekf_var(2,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, zeros(size(p_liekf(1,:))), 3*p_liekf_var(2,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-200,200])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

subplot(313)

hold('on')

plot(t, zeros(size(p_gt(3,:))), 'k--', 'LineWidth', 2);

shadedErrorBar(t, zeros(size(p_ekf(1,:))), 3*p_ekf_var(3,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, zeros(size(p_liekf(1,:))), 3*p_liekf_var(3,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-200,200])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

print('position_noise_cov', '-dpng')

figure;

subplot(311)

hold('on')

plot(t, zeros(size(theta_gt(1,:))), 'k--', 'LineWidth', 2);

shadedErrorBar(t, zeros(size(theta_ekf(1,:))), 3*theta_ekf_var(1,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, zeros(size(theta_liekf(1,:))), 3*theta_liekf_var(1,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-7,7])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

title("Theta 3 Standard Deviations");

subplot(312)

hold('on')

plot(t, zeros(size(theta_gt(2,:))), 'k--', 'LineWidth', 2)

shadedErrorBar(t, zeros(size(theta_ekf(2,:))), 3*theta_ekf_var(2,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, zeros(size(theta_liekf(2,:))), 3*theta_liekf_var(2,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-7,7])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

subplot(313)

hold('on')

plot(t, zeros(size(theta_gt(3,:))),  'k--', 'LineWidth', 2)

shadedErrorBar(t, zeros(size(theta_ekf(3,:))), 3*theta_ekf_var(3,:), 'lineProps', {'g', 'LineWidth', 1})

shadedErrorBar(t, zeros(size(theta_liekf(3,:))), 3*theta_liekf_var(3,:), 'lineProps', {'r', 'LineWidth', 1})

axis([0,2,-7,7])

legend('Ground Truth', 'EKF', 'LIEKF', 'location', 'eastoutside')

print('theta_noise_cov', '-dpng')

% -------------------------------------------------------------------------

% Helper functions, mostly for SO3 stuff

function ux  = skew(u)

    ux = [

        0 -u(3) u(2)

        u(3) 0 -u(1)

        -u(2) u(1) 0

    ];

end

function u = unskew(ux)

    u(1,1) = -ux(2,3);

    u(2,1) = ux(1,3);

    u(3,1) = -ux(1,2);

end

function w = Log(R)

    w = unskew(logm(R));

end

function J = J_l(theta)

    t_x = skew(theta);

    t = norm(theta);

    J = eye(3) + (1 - cos(t))/t^2 * t_x + (t - sin(t))/t^3*(t_x)^2;

end

%-------------------------------------------------------------

⛄ 运行结果

⛄ 参考文献

❤️ 关注我领取海量matlab电子书和数学建模资料

❤️部分理论引用网络文献,若有侵权联系博主删除

  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,我无法确定您需要的自适应扩展卡尔曼滤波的具体实现。但是,我可以为您提供一个通用的卡尔曼滤波算法的Python实现,供您参考: ```python import numpy as np class KalmanFilter: def __init__(self, F, H, Q, R, x0, P0): self.F = F self.H = H self.Q = Q self.R = R self.x = x0 self.P = P0 def predict(self): self.x = np.dot(self.F, self.x) self.P = np.dot(np.dot(self.F, self.P), self.F.T) + self.Q def update(self, z): y = z - np.dot(self.H, self.x) S = np.dot(np.dot(self.H, self.P), self.H.T) + self.R K = np.dot(np.dot(self.P, self.H.T), np.linalg.inv(S)) self.x = self.x + np.dot(K, y) I = np.eye(self.F.shape[0]) self.P = np.dot(np.dot(I - np.dot(K, self.H), self.P), (I - np.dot(K, self.H)).T) + np.dot(np.dot(K, self.R), K.T) # 示例 dt = 1.0/60.0 # 状态转移矩阵 F = np.array([[1, dt, 0], [0, 1, dt], [0, 0, 1]]) # 观测矩阵 H = np.array([1, 0, 0]).reshape(1, 3) # 过程噪声协方差 q = 1e-5 Q = np.array([[q, 0, 0], [0, q, 0], [0, 0, q]]) # 测量噪声协方差 r = 0.01 R = np.array([r]).reshape(1, 1) # 初始状态 x = np.array([0, 0, 0]).reshape(3, 1) # 初始协方差矩阵 P = np.diag([100, 100, 100]) kf = KalmanFilter(F=F, H=H, Q=Q, R=R, x0=x, P0=P) # 生成模拟数据 times = np.arange(0, 10, dt) n_samples = len(times) xs = np.zeros((n_samples, 3)) ys = np.zeros((n_samples, 1)) for i in range(n_samples): t = times[i] xs[i] = np.array([0.01*t**2, 0.1*t, 10*np.sin(t)]) ys[i] = np.dot(H, xs[i]) + r*np.random.randn() # 进行卡尔曼滤波 filtered_xs = np.zeros((n_samples, 3)) for i in range(n_samples): kf.predict() kf.update(ys[i]) filtered_xs[i] = kf.x.T # 输出结果 print(filtered_xs) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matlab科研助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值