1 | /*! |
---|
2 | \file |
---|
3 | \brief TR 2525 file for testing Toy Problem of mpf for Covariance Estimation |
---|
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 <itpp/itbase.h> |
---|
15 | #include <estim/libKF.h> |
---|
16 | #include <estim/libPF.h> |
---|
17 | #include <estim/ekf_templ.h> |
---|
18 | #include <stat/libFN.h> |
---|
19 | |
---|
20 | #include <stat/loggers.h> |
---|
21 | |
---|
22 | #include "pmsm.h" |
---|
23 | #include "simulator.h" |
---|
24 | #include "sim_profiles.h" |
---|
25 | |
---|
26 | using namespace itpp; |
---|
27 | |
---|
28 | int main() { |
---|
29 | // Kalman filter |
---|
30 | int Ndat = 9000; |
---|
31 | double h = 1e-6; |
---|
32 | int Nsimstep = 125; |
---|
33 | int Npart = 200; |
---|
34 | |
---|
35 | // internal model |
---|
36 | IMpmsm fxu; |
---|
37 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
38 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
39 | // observation model |
---|
40 | OMpmsm hxu; |
---|
41 | |
---|
42 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
43 | vec Qdiag ( "1e-6 1e-6 0.001 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
44 | vec Rdiag ( "1e-8 1e-8" ); //var(diff(xth)) = "0.034 0.034" |
---|
45 | chmat Q ( Qdiag ); |
---|
46 | chmat R ( Rdiag ); |
---|
47 | EKFCh KFE ( rx,ry,ru ); |
---|
48 | KFE.set_parameters ( &fxu,&hxu,Q,R ); |
---|
49 | KFE.set_est ( mu0, chmat ( zeros ( 4 ) ) ); |
---|
50 | |
---|
51 | RV rQ ( "{Q }","4" ); |
---|
52 | EKFCh_unQ KFEp ( rx,ry,ru,rQ ); |
---|
53 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
---|
54 | KFEp.set_est ( mu0, chmat ( zeros ( 4 ) ) ); |
---|
55 | |
---|
56 | //mgamma_fix evolQ ( rQ,rQ ); |
---|
57 | migamma_fix evolQ ( rQ,rQ ); |
---|
58 | MPF<EKFCh_unQ> M ( rx,rQ,evolQ,evolQ,Npart,KFEp ); |
---|
59 | // initialize |
---|
60 | evolQ.set_parameters ( 0.1, 10*Qdiag, 1.0); //sigma = 1/10 mu |
---|
61 | evolQ.condition (10*Qdiag ); //Zdenek default |
---|
62 | M.set_est ( *evolQ._e() ); |
---|
63 | evolQ.set_parameters ( 0.10, 10*Qdiag,0.9999); //sigma = 1/10 mu |
---|
64 | // |
---|
65 | |
---|
66 | const epdf& KFEep = KFE._epdf(); |
---|
67 | const epdf& Mep = M._epdf(); |
---|
68 | |
---|
69 | dirfilelog L("exp/mpf_test",100); |
---|
70 | int l_X = L.add(rx, "xt"); |
---|
71 | int l_D = L.add(concat(ry,ru), ""); |
---|
72 | int l_XE= L.add(rx, "xtE"); |
---|
73 | int l_XM= L.add(concat(rQ,rx), "xtM"); |
---|
74 | int l_VE= L.add(rx, "VE"); |
---|
75 | int l_VM= L.add(concat(rQ,rx), "VM"); |
---|
76 | int l_Q= L.add(rQ, ""); |
---|
77 | L.init(); |
---|
78 | |
---|
79 | // SET SIMULATOR |
---|
80 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
81 | vec dt ( 2 ); |
---|
82 | vec ut ( 2 ); |
---|
83 | vec xt ( 4 ); |
---|
84 | vec xtm=zeros(4); |
---|
85 | double Ww=0.0; |
---|
86 | vec vecW="1 2 4 9 4 2 0 -4 -9 -16 -4 0 0"; |
---|
87 | |
---|
88 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
89 | //Number of steps of a simulator for one step of Kalman |
---|
90 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
91 | //simulator |
---|
92 | sim_profile_vec01t(Ww,vecW); |
---|
93 | pmsmsim_step ( Ww ); |
---|
94 | }; |
---|
95 | ut(0) = KalmanObs[4]; |
---|
96 | ut(1) = KalmanObs[5]; |
---|
97 | xt = fxu.eval(xtm,ut) + diag(sqrt(Qdiag))*randn(4); |
---|
98 | dt = hxu.eval(xt,ut); |
---|
99 | xtm = xt; |
---|
100 | |
---|
101 | //Variances |
---|
102 | if (tK==1000) Qdiag(0)*=10; |
---|
103 | if (tK==2000) Qdiag(0)/=10; |
---|
104 | if (tK==3000) Qdiag(1)*=10; |
---|
105 | if (tK==4000) Qdiag(1)/=10; |
---|
106 | if (tK==5000) Qdiag(2)*=10; |
---|
107 | if (tK==6000) Qdiag(2)/=10; |
---|
108 | if (tK==7000) Qdiag(3)*=10; |
---|
109 | if (tK==8000) Qdiag(3)/=10; |
---|
110 | |
---|
111 | //estimator |
---|
112 | KFE.bayes ( concat ( dt,ut ) ); |
---|
113 | M.bayes ( concat ( dt,ut ) ); |
---|
114 | |
---|
115 | L.logit(l_X,xt); |
---|
116 | L.logit(l_D,concat(dt,ut)); |
---|
117 | L.logit(l_XE,KFEep.mean()); |
---|
118 | L.logit(l_XM,Mep.mean()); |
---|
119 | L.logit(l_VE,KFEep.variance()); |
---|
120 | L.logit(l_VM,Mep.variance()); |
---|
121 | L.logit(l_Q,Qdiag); |
---|
122 | L.step(); |
---|
123 | } |
---|
124 | L.finalize(); |
---|
125 | //Exit program: |
---|
126 | |
---|
127 | return 0; |
---|
128 | } |
---|