| 1 | function testKF(skipgen)
|
|---|
| 2 | if nargin<1, skipgen=0; end
|
|---|
| 3 |
|
|---|
| 4 | if ~skipgen
|
|---|
| 5 | A=[1 -0.5; 1 0];
|
|---|
| 6 | B=[1;0.1];
|
|---|
| 7 | C=[1 0];%; 0 1];
|
|---|
| 8 | D=0.1;%[0.1; 0];
|
|---|
| 9 | R=0.01;%[1 0; 0 0.1];
|
|---|
| 10 | Q=[0.2 0 ; 0 0.2];
|
|---|
| 11 |
|
|---|
| 12 | sQ = chol(Q)';
|
|---|
| 13 | sR = chol(R)';
|
|---|
| 14 |
|
|---|
| 15 | N =3000;
|
|---|
| 16 | mu0 = [0;0];
|
|---|
| 17 | P0 = 200*eye(2);
|
|---|
| 18 |
|
|---|
| 19 | u = zeros(1,N);
|
|---|
| 20 | x = zeros(2,N);
|
|---|
| 21 | y = zeros(1,N);
|
|---|
| 22 |
|
|---|
| 23 | x(:,1) = [10;10];
|
|---|
| 24 | Et = sQ*randn(2,N);
|
|---|
| 25 | Wt = sR*randn(1,N);
|
|---|
| 26 | for i=2:N;
|
|---|
| 27 | x(:,i) = A*x(:,i-1) + B*u(i) + Et(:,i);
|
|---|
| 28 | y(:,i) = C*x(:,i) + D*u(i) + Wt(:,1);
|
|---|
| 29 | end
|
|---|
| 30 |
|
|---|
| 31 | d=[y;u];
|
|---|
| 32 | itsave('kalman_stress.it',d,A,B,C,D,Q,R,P0,mu0)
|
|---|
| 33 | save testKF
|
|---|
| 34 | else
|
|---|
| 35 | load testKF
|
|---|
| 36 | end
|
|---|
| 37 | % init
|
|---|
| 38 | mu = mu0;
|
|---|
| 39 | P = P0;
|
|---|
| 40 | EP = [0;0];
|
|---|
| 41 |
|
|---|
| 42 | Oxt = mu0;
|
|---|
| 43 | OPt = P0;
|
|---|
| 44 | ll =0;
|
|---|
| 45 | oxt = mu0;
|
|---|
| 46 | oPt = chol(P0)';
|
|---|
| 47 | oll=0;
|
|---|
| 48 |
|
|---|
| 49 | Mu = zeros(2,N);
|
|---|
| 50 | Mu_oo = zeros(2,N);
|
|---|
| 51 |
|
|---|
| 52 | tic;
|
|---|
| 53 | for t=2:N
|
|---|
| 54 | mu = A*mu + B*u(t);
|
|---|
| 55 | P = A*P*A' + Q;
|
|---|
| 56 |
|
|---|
| 57 | %Data update
|
|---|
| 58 | Ry = C*P*C' + R;
|
|---|
| 59 | iRy = inv(Ry);
|
|---|
| 60 | K = P*C'*iRy;
|
|---|
| 61 | P = P- K*C*P; % P = P -KCP;
|
|---|
| 62 | mu = mu + K*(y(:,t)-C*mu-D*u(t));
|
|---|
| 63 | Mu(1:2,t)=mu;
|
|---|
| 64 |
|
|---|
| 65 | % [Oxt,OPt,ll(t)] = Kalman(Oxt,y(:,t),A,C,Q,R,OPt);
|
|---|
| 66 | % [oxt,oPt,oll(t)] = KalmanSq(oxt,y(:,t),A,C,sQ,sR,oPt);
|
|---|
| 67 | % MuK(1:2,t) = Oxt;
|
|---|
| 68 | % MuS(1:2,t) = oxt;
|
|---|
| 69 | end
|
|---|
| 70 | exec_matlab = toc
|
|---|
| 71 | %keyboard
|
|---|
| 72 |
|
|---|
| 73 | %%%%%%%% OBJECTs in MATLAB %%%%%
|
|---|
| 74 | addpath ../../../applications/bdmtoolbox/mex/mex_classes
|
|---|
| 75 | addpath ../../../applications/bdmtoolbox/mex/
|
|---|
| 76 |
|
|---|
| 77 | oKAL=mexKalman;
|
|---|
| 78 | oKAL.A = A;
|
|---|
| 79 | oKAL.B = B;
|
|---|
| 80 | oKAL.C = C;
|
|---|
| 81 | oKAL.D = D;
|
|---|
| 82 | oKAL.Q = Q;
|
|---|
| 83 | oKAL.R = R;
|
|---|
| 84 | oKAL=oKAL.validate;
|
|---|
| 85 | oKAL.apost_pdf.mu = mu0;
|
|---|
| 86 | oKAL.apost_pdf.R = P0;
|
|---|
| 87 |
|
|---|
| 88 | tic;
|
|---|
| 89 | for t=2:N
|
|---|
| 90 | oKAL=oKAL.bayes(y(t),u(t));
|
|---|
| 91 | Mu_oo(1:2,t) = oKAL.apost_pdf.mu;
|
|---|
| 92 | end
|
|---|
| 93 | exec_matlab_oo=toc
|
|---|
| 94 |
|
|---|
| 95 | !./stresssuite kalman_stress
|
|---|
| 96 | itload('kalman_stress_res.it');
|
|---|
| 97 |
|
|---|
| 98 | hold off
|
|---|
| 99 | plot(x');
|
|---|
| 100 | hold on
|
|---|
| 101 | plot([Mu]','--'); % shift the predldmatictions
|
|---|
| 102 | plot([Mu_oo]',':'); % shift the predldmatictions
|
|---|
| 103 | plot(xth2','+');
|
|---|
| 104 | plot(xthE','o');
|
|---|
| 105 | %plot([zeros(size(xth,1),1) MuK]','d'); % shift the predictions
|
|---|
| 106 |
|
|---|
| 107 | exec_times
|
|---|
| 108 | exec_matlab./exec_times
|
|---|
| 109 | %keyboard
|
|---|
| 110 | end
|
|---|