kalman_intro.mht
来自「本人写的用MATLAB实现卡尔曼滤波」· MHT 代码 · 共 93 行
MHT
93 行
From: <由 Microsoft Internet Explorer 5 保存>
Subject:
Date: Mon, 16 Apr 2007 15:06:23 +0800
MIME-Version: 1.0
Content-Type: text/html;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://yaoxuchen.googlepages.com/kalman_intro.m
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.2180" name=3DGENERATOR></HEAD>
<BODY><PRE>% Kalman filter example demo in Matlab
% This M code is modified from Andrew D. Straw's Python=20
% implementation of Kalman filter algorithm.
% The original code is here:
% http://www.scipy.org/Cookbook/KalmanFiltering
% Below is the Python version's comments:
% Kalman filter example demo in Python
% A Python implementation of the example given in pages 11-15 of =
"An
% Introduction to the Kalman Filter" by Greg Welch and Gary =
Bishop,
% University of North Carolina at Chapel Hill, Department of =
Computer
% Science, TR 95-041,
% http://www.cs.unc.edu/~welch/kalman/kalmanIntro.html
% by Andrew D. Straw
% by Xuchen Yao
=20
clear all;
close all;
% intial parameters
n_iter =3D 50;
sz =3D [n_iter, 1]; % size of array
x =3D -0.37727; % truth value (typo in example at top of p. 13 calls =
this z)
z =3D x + sqrt(0.1)*randn(sz); % observations (normal about x, =
sigma=3D0.1)
Q =3D 1e-5; % process variance
% allocate space for arrays
xhat=3Dzeros(sz); % a posteri estimate of x
P=3Dzeros(sz); % a posteri error estimate
xhatminus=3Dzeros(sz); % a priori estimate of x
Pminus=3Dzeros(sz); % a priori error estimate
K=3Dzeros(sz); % gain or blending factor
R =3D 0.01; % estimate of measurement variance, change to see effect
% intial guesses
xhat(1) =3D 0.0;
P(1) =3D 1.0;
for k =3D 2:n_iter
% time update
xhatminus(k) =3D xhat(k-1);
Pminus(k) =3D P(k-1)+Q;
% measurement update
K(k) =3D Pminus(k)/( Pminus(k)+R );
xhat(k) =3D xhatminus(k)+K(k)*(z(k)-xhatminus(k));
P(k) =3D (1-K(k))*Pminus(k);
end
figure();
plot(z,'k+');
hold on;
plot(xhat,'b-')
hold on;
plot(x*ones(sz),'g-');
legend('noisy measurements', 'a posteri estimate', 'truth value');
xlabel('Iteration');
ylabel('Voltage');
hold off;
figure();
valid_iter =3D [2:n_iter]; % Pminus not valid at step 1
plot(valid_iter,Pminus([valid_iter]));
legend('a priori error estimate');
xlabel('Iteration');
ylabel('$(Voltage)^2$');
ylim([0,.01]);</PRE></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?