1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| clear;clc;
Ak = exp(-0.02); Ck = 1; Qk = 1-exp(-0.04); Rk = 1;
X0 = 0; P0 = 1;
Y = [-3.2 -0.8 -14 -16 -17 -18 -3.3 -2.4 -18 -0.3 -0.4 -0.8 -19 -2.0 -1.2 ... -11 -14 -0.9 0.8 10 0.2 0.5 2.4 -0.5 0.5 -13 0.5 10 -12 0.5 -0.6 -15 -0.7 15 ... 0.5 -0.7 -2.0 -19 -17 -11 -14];
N = length(Y); for k = 1:N if k == 1 X_pre(k) = Ak*X0; P_pre(k) = Ak*P0*Ak+Qk; K(k) = P_pre(k)*Ck'*(Ck*P_pre(k)*Ck'+Rk)^(-1); X_kalman(k) = Ak*X_pre(k)+K(k)*(Y(k)-Ck*Ak*X_pre(k)); I = eye(size(K(k))); P_kalman(k) = (I-K(k)*Ck)*P_pre(k);
else X_pre(k) = Ak*(X_kalman(k-1)); P_pre(k) = Ak*P_kalman(k-1)*Ak+Qk; K(k) = P_pre(k)*Ck'*(Ck*P_pre(k)*Ck'+Rk)^(-1); X_kalman(k) = Ak*X_pre(k)+K(k)*(Y(k)-Ck*Ak*X_pre(k)); I = eye(size(K(k))); P_kalman(k) = (I-K(k)*Ck)*P_pre(k); end end M=1:N; T=0.02*M;
figure () plot(T,Y,'r','LineWidth',1); hold on; plot(T,X_kalman,'b','LineWidth',1); legend('测量信号y(t)','Kalman估计信号x(t)')
|