Changeset 230
- Timestamp:
- 01/15/09 10:53:55 (16 years ago)
- Files:
-
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/math/chmat.h
r183 r230 75 75 chmat& operator += ( const chmat &A2 ); 76 76 chmat& operator -= ( const chmat &A2 ) ; 77 chmat& operator * ( const chmat &A2 ); 78 chmat& operator * ( const double &d ){Ch*d; return *this;}; 79 chmat& operator = ( const chmat &A2 ){Ch=A2.Ch;dim=A2.dim;return *this;} 77 80 }; 78 81 -
bdm/stat/loggers.cpp
r162 r230 44 44 for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries 45 45 int rvsize = entries ( i ).size ( j ); 46 if ( rvsize ==1 ) { 47 scalarnames ( ii ) = names ( i ) + "_" + entries ( i ).name ( j ); 48 ii++; 49 } 50 else { 46 // for non-empty names 47 if (names(i).length()>0) {scalarnames(ii)=names(i) + "_";} 48 // add name 49 scalarnames ( ii ) += entries ( i ).name ( j ); 50 // add number when needed 51 if ( rvsize >1 ) { 52 // copy the same name for th whole RV 53 for ( k=1;k<rvsize;k++ ) {scalarnames(ii+k)=scalarnames(ii);} 54 // add numbers 51 55 for ( k=0;k<rvsize;k++ ) { //for all scalars in given RV 52 56 sprintf ( num,"%d",k ); 53 scalarnames ( ii ) = names ( i ) + "_" + entries ( i ).name ( j ) +"_" + num;57 scalarnames ( ii ) += (std::string)"_" + num; 54 58 ii++; 55 59 } 56 } 60 } else { 61 ii++;} 57 62 } 58 63 } -
matlab/pmsm/mpf_u_delta_disp.m
r228 r230 15 15 hold on; 16 16 plot(xthE(:,i)','r') 17 plot(xthM(:,i+ 2)','g')17 plot(xthM(:,i+3)','g') 18 18 % plot(xthV(i,:)','m'); 19 19 grid on 20 20 end 21 21 22 figure(7); plot(abs(xthM(:,3))); 22 23 23 24 figure(3) … … 55 56 56 57 hold on; 57 di = xthM(:, 1+4)'-xth(:,1+2)';58 di = xthM(:,3+3)'-xth(:,1+2)'; 58 59 plot(tm,di,'-') 59 60 MSE_M=di*di'/9000 -
pmsm/mpf_test.cpp
r229 r230 18 18 #include <stat/libFN.h> 19 19 20 #include <stat/loggers.h> 21 20 22 #include "pmsm.h" 21 23 #include "simulator.h" … … 29 31 double h = 1e-6; 30 32 int Nsimstep = 125; 31 int Npart = 20 ;33 int Npart = 200; 32 34 33 35 // internal model … … 56 58 MPF<EKFCh_unQ> M ( rx,rQ,evolQ,evolQ,Npart,KFEp ); 57 59 // initialize 58 evolQ.set_parameters ( 0.1, Qdiag, 1.0); //sigma = 1/10 mu 59 evolQ.condition (Qdiag ); //Zdenek default 60 double xxx; 61 cout << Qdiag <<endl << "smp:"<< evolQ.samplecond(Qdiag,xxx) <<endl; 62 eigamma* pfinit=dynamic_cast<eigamma*>(evolQ._e()); 63 cout << pfinit->mean()<<endl; 64 M.set_est ( *pfinit ); 65 evolQ.set_parameters ( 0.10, Qdiag,0.995); //sigma = 1/10 mu 66 cout << Qdiag <<endl << "smp:"<< evolQ.samplecond(Qdiag,xxx) <<endl; 67 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 68 64 // 69 65 … … 71 67 const epdf& Mep = M._epdf(); 72 68 73 mat Xt=zeros ( Ndat ,4 ); //true state from simulator 74 mat Dt=zeros ( Ndat,2+2 ); //observation 75 mat XtE=zeros ( Ndat, 4 ); 76 mat VarE=zeros ( Ndat, 4 ); 77 mat Qtr=zeros ( Ndat, 4 ); 78 mat XtM=zeros ( Ndat,4+4 ); //Q + x 79 mat VarM=zeros ( Ndat,4+4 ); //Q + x 80 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 81 79 // SET SIMULATOR 82 80 pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); … … 101 99 xtm = xt; 102 100 103 104 101 //Variances 105 102 if (tK==1000) Qdiag(0)*=10; … … 116 113 M.bayes ( concat ( dt,ut ) ); 117 114 118 Xt.set_row ( tK, xt); //vec from C-array 119 Dt.set_row ( tK, concat ( dt,ut)); 120 Qtr.set_row ( tK, Qdiag); 121 XtE.set_row ( tK,KFEep.mean() ); 122 VarE.set_row ( tK,KFEep.variance() ); 123 XtM.set_row ( tK,Mep.mean() ); 124 VarM.set_row ( tK,Mep.variance() ); 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(); 125 123 } 126 127 it_file fou ( "mpf_test.it" ); 128 129 fou << Name ( "xth" ) << Xt; 130 fou << Name ( "Dt" ) << Dt; 131 fou << Name ( "Qtr" ) << Qtr; 132 fou << Name ( "xthE" ) << XtE; 133 fou << Name ( "xthM" ) << XtM; 134 fou << Name ( "VarE" ) << VarE; 135 fou << Name ( "VarM" ) << VarM; 124 L.finalize(); 136 125 //Exit program: 137 126 -
pmsm/mpf_u_delta.cpp
r226 r230 25 25 using namespace itpp; 26 26 27 //!Extended Kalman filter with unknown \c Q 28 class EKFCh_cond : public EKFCh , public BMcond { 27 //!Extended Kalman filter with unknown \c Q and delta u 28 class EKFCh_du_kQ : public EKFCh , public BMcond { 29 chmat Qref; 29 30 public: 30 31 //! Default constructor 31 EKFCh_cond ( RV rx, RV ry,RV ru,RV rC ) :EKFCh ( rx,ry,ru ),BMcond ( rC ) {}; 32 EKFCh_du_kQ ( RV rx, RV ry,RV ru,RV rC ) :EKFCh ( rx,ry,ru ),BMcond ( rC ),Qref(rx.count()) {}; 33 void set_ref(const chmat &Qref0){Qref=Qref0;} 32 34 void condition ( const vec &val ) { 33 pfxu->condition ( val ); 35 pfxu->condition ( val.left(2) ); 36 37 Q = Qref*std::abs(val(2)); 38 preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() ); 34 39 }; 35 40 }; … … 74 79 double h = 1e-6; 75 80 int Nsimstep = 125; 76 int Npart = 20 0;81 int Npart = 20; 77 82 78 83 mat Rnoise = randn ( 2,Ndat ); … … 89 94 vec mu0= "0.0 0.0 0.0 0.0"; 90 95 vec Qdiag ( "0.4 0.4 0.001 0.000001" ); //zdenek: 0.01 0.01 0.0001 0.0001 91 vec Rdiag ( "0. 1 0.1" ); //var(diff(xth)) = "0.034 0.034"96 vec Rdiag ( "0.2 0.2" ); //var(diff(xth)) = "0.034 0.034" 92 97 chmat Q ( Qdiag ); 93 98 chmat R ( Rdiag ); … … 96 101 KFE.set_est ( mu0, chmat ( ones ( 4 ) ) ); 97 102 98 RV rUd ( "{ud }", "2" );99 EKFCh_ condKFEp ( rx,ry,ru,rUd );103 RV rUd ( "{ud k}", "2 1" ); 104 EKFCh_du_kQ KFEp ( rx,ry,ru,rUd ); 100 105 KFEp.set_parameters ( &fxu,&hxu,Q,R ); 106 KFEp.set_ref(Q); 101 107 KFEp.set_est ( mu0, chmat ( ones ( 4 ) ) ); 102 108 103 109 mlnorm<ldmat> evolUd ( rUd,rUd ); 104 MPF<EKFCh_ cond> M ( rx,rUd,evolUd,evolUd,Npart,KFEp );110 MPF<EKFCh_du_kQ> M ( rx,rUd,evolUd,evolUd,Npart,KFEp ); 105 111 // initialize 106 vec Ud0="0 0 ";107 evolUd.set_parameters ( eye ( 2 ), vec_2 ( 0.0,0.0 ), ldmat ( 10.0*eye ( 2 ) ));112 vec Ud0="0 0 1.0"; 113 evolUd.set_parameters ( eye ( 3 ), zeros(3), ldmat ( vec( "1 1 0.0001" ))); 108 114 evolUd.condition ( Ud0 ); 109 115 epdf& pfinit=evolUd._epdf(); 110 116 M.set_est ( pfinit ); 111 evolUd.set_parameters ( eye ( 2 ), vec_2 ( 0.0,0.0 ), ldmat ( 0.1*eye ( 2 ) ));117 evolUd.set_parameters ( eye ( 3 ), zeros(3), ldmat ( vec(" 4e-3 4e-3 1e-3" ))); 112 118 113 119 mat Xt=zeros ( Ndat ,4 ); //true state from simulator … … 115 121 mat XtE=zeros ( Ndat, 4 ); 116 122 mat Qtr=zeros ( Ndat, 4 ); 117 mat XtM=zeros ( Ndat, 2+4 ); //W + x123 mat XtM=zeros ( Ndat, 3+4 ); //W + x 118 124 mat XtMTh=zeros ( Ndat,1 ); 119 125