1 | /*! |
---|
2 | \file |
---|
3 | \brief A test for Kalman with unknown Q |
---|
4 | \author Vaclav Smidl. |
---|
5 | |
---|
6 | ----------------------------------- |
---|
7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
8 | |
---|
9 | Using IT++ for numerical operations |
---|
10 | ----------------------------------- |
---|
11 | */ |
---|
12 | |
---|
13 | |
---|
14 | #include <estim/libKF.h> |
---|
15 | #include <estim/libPF.h> |
---|
16 | #include <stat/libFN.h> |
---|
17 | |
---|
18 | #include "pmsm.h" |
---|
19 | |
---|
20 | using namespace bdm; |
---|
21 | |
---|
22 | //!Extended Kalman filter with unknown \c Q |
---|
23 | class EKF_unQ : public EKFCh , public BMcond { |
---|
24 | public: |
---|
25 | //! Default constructor |
---|
26 | void condition ( const vec &Q0 ) { |
---|
27 | Q.setD ( Q0,0 ); |
---|
28 | //from EKF |
---|
29 | preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() ); |
---|
30 | }; |
---|
31 | }; |
---|
32 | |
---|
33 | int main() { |
---|
34 | // Kalman filter |
---|
35 | int Ndat = 10000; |
---|
36 | |
---|
37 | // cout << KF; |
---|
38 | // internal model |
---|
39 | IMpmsm fxu; |
---|
40 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
41 | fxu.set_parameters ( 0.28, 0.003465, 20*1e-6, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
42 | // observation model |
---|
43 | OMpmsm hxu; |
---|
44 | |
---|
45 | vec mu0= "100 100 100 1"; |
---|
46 | vec Qdiag ( "0.1 0.1 0.01 0.00001" ); |
---|
47 | vec Rdiag ( "0.02 0.02" ); |
---|
48 | vec vQ = "0.01:0.01:100"; |
---|
49 | |
---|
50 | chmat Q ( Qdiag ); |
---|
51 | chmat R ( Rdiag ); |
---|
52 | |
---|
53 | RV rQ ( "{Q}","2" ); |
---|
54 | EKF_unQ KFE ; |
---|
55 | KFE.set_parameters ( &fxu,&hxu,Q,R ); |
---|
56 | KFE.set_est ( mu0, chmat ( 1000*ones ( 4 ) ) ); |
---|
57 | |
---|
58 | mgamma evolQ ; |
---|
59 | //evolQ.set_parameters ( 10000.0 ); //sigma = 1/10 mu |
---|
60 | |
---|
61 | MPF<EKF_unQ> M ( &evolQ,&evolQ,100,KFE ); |
---|
62 | |
---|
63 | const epdf& KFEep = KFE.posterior(); |
---|
64 | const epdf& Mep = M.posterior(); |
---|
65 | // initialize |
---|
66 | evolQ.set_parameters ( 1.0, "0.5 0.5" ); //sigma = 1/10 mu |
---|
67 | evolQ.condition ( "0.5 0.5" ); |
---|
68 | const epdf& pfinit=evolQ._epdf(); |
---|
69 | M.set_est ( pfinit ); |
---|
70 | evolQ.set_parameters ( 1000.0 , "0.5 0.5"); //sigma = 1/10 mu |
---|
71 | |
---|
72 | //simulator values |
---|
73 | vec dt ( 2 ); // output (isa isb) |
---|
74 | vec wt ( 2 ); // noise on dt |
---|
75 | vec ut ( 2 ); // |
---|
76 | vec xt=mu0; // initial state |
---|
77 | vec et ( 4 ); // noise on xt |
---|
78 | |
---|
79 | mat Xt=zeros ( 4,Ndat ); // True trajetory of xt |
---|
80 | mat XtE=zeros ( 4,Ndat ); // Estimate of xt using EKF (known Q) |
---|
81 | mat XtM=zeros ( 6,Ndat ); // Estimate of xt using EKF-MPF |
---|
82 | Xt.set_col ( 0,mu0 ); |
---|
83 | XtM.set_col ( 0,Mep.mean() ); |
---|
84 | |
---|
85 | for ( int t=1;t<Ndat;t++ ) { |
---|
86 | //simulator |
---|
87 | // UniRNG.sample_vector ( 2,wt ); |
---|
88 | |
---|
89 | if ( rem ( t,500 ) <200 ) ut = rem ( t,500 ) *ones ( 2 ); |
---|
90 | else |
---|
91 | ut=zeros ( 2 ); |
---|
92 | |
---|
93 | NorRNG.sample_vector ( 4,et ); |
---|
94 | NorRNG.sample_vector ( 2,wt ); |
---|
95 | xt = fxu.eval ( xt,ut ) + Q.sqrt_mult ( et ); |
---|
96 | dt = hxu.eval ( xt,ut ) + R.sqrt_mult ( wt ); |
---|
97 | |
---|
98 | //estimator |
---|
99 | KFE.bayes ( concat ( dt,ut ) ); |
---|
100 | M.bayes ( concat ( dt,ut ) ); |
---|
101 | |
---|
102 | Xt.set_col ( t,xt ); |
---|
103 | XtE.set_col ( t,KFEep.mean() ); |
---|
104 | XtM.set_col ( t,Mep.mean() ); |
---|
105 | } |
---|
106 | |
---|
107 | it_file fou ( "pmsm.it" ); |
---|
108 | |
---|
109 | fou << Name ( "xth" ) << Xt; |
---|
110 | fou << Name ( "xthE" ) << XtE; |
---|
111 | fou << Name ( "xthM" ) << XtM; |
---|
112 | //Exit program: |
---|
113 | return 0; |
---|
114 | |
---|
115 | } |
---|