设计卡尔曼滤波器
Designing Kalman Filter
因此,我被要求设计卡尔曼滤波器和 运行 一些模拟以查看结果。
给定方程式:
x(k+1) = -0.8*x(k) + sin(0.1k) + w(k)
y(k) = x(k) + v(k)
其中 w(k) 和 v(k) 是处理和测量噪声
w(k)~N(0, 0.01)
v(k)~N(0, 1)
我想确保一切正常并且在我的代码中有意义。
A = -0.8;
B = 1;
C = 1;
% Covariance matrices
% Processing noise
W = 0.01;
Q=W;
% Measurement noise
V = 1;
R=V;
% Initial conditions
x0 = 0;
P0 = 0;
xpri = x0;
Ppri = P0;
xpost = x0;
Ppost = P0;
% State
Y = zeros(1, size(t,2));
X = Y;
X(1) = x0;
% xpri - x priori
% xpost - x posteriori
% Ppri - P priori
% Ppost - P posteriori
for i = 1:size(t,2)
%Simulation
Y(i) = C*sin((i-1)*0.1)+normrnd(0,sqrt(V));
if i > 1
% Prediction
xpri = A*xpost+B*sin((i-1)*0.1);
Ppri = A*Ppost*A' + Q;
eps = Y(i) - C*xpri;
S = C*Ppri*C' + R;
K = Ppri*C'*S^(-1);
xpost = xpri + K*eps;
Ppost = Ppri - K*S*K';
X(i) = xpost;
end
end
plot(t, Y, 'b', t, X, 'r')
title('Kalman's Filter')
xlabel('Time')
ylabel('Value')
legend('Measured value', 'Estimated value')
这个KF可以正常使用吗?如果不是,有什么问题?
代码看起来不错,结果也不错。是什么让你怀疑?
这是一个通用的卡尔曼滤波器实现作为一个函数,如果你想仔细检查,你可以检查一下。但是有了过滤器,这一切都是关于协方差矩阵的调整 P
、Q
、R
...
function [v_f,xyz] = KF(u,z,A,B,C,D,x0,P0,Q,R)
%% initialization
persistent x P
if isempty(x)||isempty(P)
% state vector
x = reshape(x0,numel(x0),1); % ensure vector size
if nargin < 8 || isempty(P0)
P0 = eye(length(x)); %default initialization
emd
P = P0; % covariance matrix
end
%% covariance matrices
if nargin < 9 || isempty(Q)
Q = diag( ones(length(x) )*1e1; % proess-noise-covariance
end
% % Q = 0 -> perfect model. Q =/= 0 -> pay more attention to the measurements.
%
if nargin < 10
R = diag( ones(length(z)) ) *1e1; % measurement-noise-covariance
end
% The higher R, the less trusting the measurements
%% prediction
x_P = A*x + B*u;
P = A*P*A.' + Q; % covariance matrix
%% update
H = C;
K = P*H.'*(H*P*H.' +R)^-1;
x = x_P + K*(z - H*x_P);
P = (eye(length(x)) - K*H)*P;
%% Output
y = C*x;
end
无论如何,我建议将这个问题转移到 codereview-pages 因为你显然没有问题,只是想检查你的代码,对吧?
因此,我被要求设计卡尔曼滤波器和 运行 一些模拟以查看结果。 给定方程式:
x(k+1) = -0.8*x(k) + sin(0.1k) + w(k)
y(k) = x(k) + v(k)
其中 w(k) 和 v(k) 是处理和测量噪声
w(k)~N(0, 0.01)
v(k)~N(0, 1)
我想确保一切正常并且在我的代码中有意义。
A = -0.8;
B = 1;
C = 1;
% Covariance matrices
% Processing noise
W = 0.01;
Q=W;
% Measurement noise
V = 1;
R=V;
% Initial conditions
x0 = 0;
P0 = 0;
xpri = x0;
Ppri = P0;
xpost = x0;
Ppost = P0;
% State
Y = zeros(1, size(t,2));
X = Y;
X(1) = x0;
% xpri - x priori
% xpost - x posteriori
% Ppri - P priori
% Ppost - P posteriori
for i = 1:size(t,2)
%Simulation
Y(i) = C*sin((i-1)*0.1)+normrnd(0,sqrt(V));
if i > 1
% Prediction
xpri = A*xpost+B*sin((i-1)*0.1);
Ppri = A*Ppost*A' + Q;
eps = Y(i) - C*xpri;
S = C*Ppri*C' + R;
K = Ppri*C'*S^(-1);
xpost = xpri + K*eps;
Ppost = Ppri - K*S*K';
X(i) = xpost;
end
end
plot(t, Y, 'b', t, X, 'r')
title('Kalman's Filter')
xlabel('Time')
ylabel('Value')
legend('Measured value', 'Estimated value')
这个KF可以正常使用吗?如果不是,有什么问题?
代码看起来不错,结果也不错。是什么让你怀疑?
这是一个通用的卡尔曼滤波器实现作为一个函数,如果你想仔细检查,你可以检查一下。但是有了过滤器,这一切都是关于协方差矩阵的调整 P
、Q
、R
...
function [v_f,xyz] = KF(u,z,A,B,C,D,x0,P0,Q,R)
%% initialization
persistent x P
if isempty(x)||isempty(P)
% state vector
x = reshape(x0,numel(x0),1); % ensure vector size
if nargin < 8 || isempty(P0)
P0 = eye(length(x)); %default initialization
emd
P = P0; % covariance matrix
end
%% covariance matrices
if nargin < 9 || isempty(Q)
Q = diag( ones(length(x) )*1e1; % proess-noise-covariance
end
% % Q = 0 -> perfect model. Q =/= 0 -> pay more attention to the measurements.
%
if nargin < 10
R = diag( ones(length(z)) ) *1e1; % measurement-noise-covariance
end
% The higher R, the less trusting the measurements
%% prediction
x_P = A*x + B*u;
P = A*P*A.' + Q; % covariance matrix
%% update
H = C;
K = P*H.'*(H*P*H.' +R)^-1;
x = x_P + K*(z - H*x_P);
P = (eye(length(x)) - K*H)*P;
%% Output
y = C*x;
end
无论如何,我建议将这个问题转移到 codereview-pages 因为你显然没有问题,只是想检查你的代码,对吧?