我正在尝试实现卡尔曼滤波器来估计振荡器的状态'x'(位移和速度)。代码如下,应该很容易遵循。
clear; clc; close all;
% io = csvread('sim.csv');
% u = io(:, 1);
% y = io(:, 2);
% clear io;
% Estimation of state of a single degree-of-freedom oscillator using
% the Kalman filter
% x[n + 1] = A x[n] + B u[n] + w[n]
% y[n] = C x[n] + D u[n] + v[n]
% Here, x[n] is 2 x 1, u[n] is 1 x 1
% A is 2 x 2, B is 2 x 1, C is 1 x 2, D is 1 x 1
%% Code begins here
N = 1000;
u = randn(N, 1); % Synthetic input
y = randn(N, 1); % Synthetic output
%% Definitions
dt = 0.005; % Time step in seconds
T = 1.50; % Oscillator period
zeta = 0.05; % Damping ratio
sv0 = max(abs(u)) * dt;
sd0 = sv0 * dt;
Q = [sd0 ^ 2 0.0; 0.0 sv0 ^ 2]; % Prediction error covariance matrix
smeas = 0.001 * max(abs(u));
R = smeas ^ 2; % Measurement error (co)variance scalar
wn = 2. * pi / Ts;
c = 2.0 * zeta * wn;
k = wn ^ 2;
A = [0. 1.; -k -c];
Ad = expm(A * dt);
Bd = A \ (Ad - eye(2));
Bd = Bd(:, 2);
C = [-k -c];
D = -1.0;
%% State-space model and Kalman filter
sys = ss(Ad, Bd, C, D, dt, 'inputname', 'u', 'outputname', 'y');
[kest,L,P] = kalman(sys, Q, R, []);
这是我的问题。我得到错误:'在“kalman(SYS,QN,RN,NN,...)”命令中,QN必须是一个最多1行的实矩阵。
我认为QN = Q = const并且应该是2 x 2,但是它要求标量。也许我不理解MATLAB的'kalman'帮助描述中Q和QN之间的区别。任何见解?
感谢。
答案 0 :(得分:1)
MATLAB假设过程噪声只是一个随机变量,而不是像Q所表示的那样两个。
因此您必须像这样将G和H矩阵添加到您的系统中:
G = eye(2);
H = [0,0];
sys = ss(Ad, [Bd, G], C, [D, H], dt, 'inputname', 'u', 'outputname', 'y');
提醒一下,使用MATLAB的语法:
x*=Ax+Bu+Gw
y=Cx+Du+Hw+v