Changeset 54 for tests/pmsm_sim.cpp
- Timestamp:
- 03/25/08 13:40:27 (17 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
tests/pmsm_sim.cpp
r48 r54 19 19 #include "simulator.h" 20 20 21 #include <netcdfcpp.h> 22 void write_to_nc ( NcFile &nc, mat &X, std::string Xn, Array<std::string> A ) { 23 char tmpstr[200]; 24 int Len = X.rows(); 25 26 sprintf ( tmpstr,"%s.length",Xn.c_str() ); 27 NcDim* lengt = nc.add_dim ( tmpstr, ( long ) Len ); 28 for ( int j=0; j<X.cols(); j++ ) { 29 if ( j<A.length() ) 30 sprintf ( tmpstr,"%s_%s",Xn.c_str(), ( A ( j ) ).c_str() ); 31 else 32 sprintf ( tmpstr,"%s_%d",Xn.c_str(),j ); 33 // Create variables and their attributes 34 NcVar* P = nc.add_var ( tmpstr, ncDouble, lengt ); 35 const double* Dp = X._data(); 36 P->put ( &Dp[j*Len],Len ); 37 } 38 } 39 40 21 41 using namespace itpp; 22 42 //!Extended Kalman filter with unknown \c Q … … 29 49 //from EKF 30 50 preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() ); 31 }; 51 }; 32 52 }; 33 53 … … 35 55 int main() { 36 56 // Kalman filter 37 int Ndat = 10000;57 int Ndat = 30000; 38 58 double h = 1e-6; 39 59 int Nsimstep = 125; 40 int Npart = 100 ;41 60 int Npart = 1000; 61 42 62 // internal model 43 63 IMpmsm fxu; … … 48 68 49 69 vec mu0= "0.0 0.0 0.0 0.0"; 50 vec Qdiag ( "0.0 5 0.05 0.001 0.001" ); //zdenek: 0.05 0.05 0.001 0.00151 vec Rdiag ( "0.0 3 0.03" ); //var(diff(xth)) = "0.034 0.034"70 vec Qdiag ( "0.01 0.01 0.00001 0.00001" ); //zdenek: 0.01 0.01 0.0001 0.0001 71 vec Rdiag ( "0.0005 0.0005" ); //var(diff(xth)) = "0.034 0.034" 52 72 chmat Q ( Qdiag ); 53 73 chmat R ( Rdiag ); 54 74 EKFCh KFE ( rx,ry,ru ); 55 75 KFE.set_parameters ( &fxu,&hxu,Q,R ); 56 KFE.set_est ( mu0, chmat ( 1 000*ones ( 4 ) ) );76 KFE.set_est ( mu0, chmat ( 1*ones ( 4 ) ) ); 57 77 58 RV rQ ( "100","{Q}"," 4","0" );78 RV rQ ( "100","{Q}","2","0" ); 59 79 EKF_unQ KFEp ( rx,ry,ru,rQ ); 60 80 KFEp.set_parameters ( &fxu,&hxu,Q,R ); 61 KFEp.set_est ( mu0, chmat ( 1 000*ones ( 4 ) ) );81 KFEp.set_est ( mu0, chmat ( 1*ones ( 4 ) ) ); 62 82 63 83 mgamma evolQ ( rQ,rQ ); … … 65 85 // initialize 66 86 evolQ.set_parameters ( 100.0 ); //sigma = 1/10 mu 67 evolQ.condition ( "0.0 5 0.05 0.001 0.001" ); //Zdenek default87 evolQ.condition ( "0.01 0.01" ); //Zdenek default 68 88 epdf& pfinit=evolQ._epdf(); 69 89 M.set_est ( pfinit ); 70 evolQ.set_parameters ( 1000.0 ); 90 evolQ.set_parameters ( 1000.0 ); 71 91 72 92 // … … 75 95 epdf& Mep = M._epdf(); 76 96 77 mat Xt=zeros ( 9,Ndat); //true state from simulator78 mat Dt=zeros ( 4,Ndat); //observation79 mat XtE=zeros ( 4,Ndat);80 mat XtM=zeros ( 8,Ndat); //Q + x97 mat Xt=zeros ( Ndat ,9 ); //true state from simulator 98 mat Dt=zeros ( Ndat,4+2 ); //observation 99 mat XtE=zeros ( Ndat, 4 ); 100 mat XtM=zeros ( Ndat,6 ); //Q + x 81 101 82 102 // SET SIMULATOR … … 86 106 static long k_rampa_tmp=0; 87 107 vec dt ( 2 ); 88 vec dtVS =zeros( 2 );89 vec xtVS =zeros(4);90 vec et ( 4 );91 vec wt ( 2 );92 108 vec ut ( 2 ); 93 mat XtV=zeros ( 4,Ndat );94 109 95 110 for ( int tK=1;tK<Ndat;tK++ ) { … … 112 127 dt ( 0 ) = KalmanObs[2]; 113 128 dt ( 1 ) = KalmanObs[3]; 114 115 // My own simulator for testing : Asuming ut is OK116 NorRNG.sample_vector ( 4,et );117 NorRNG.sample_vector ( 2,wt );118 xtVS = fxu.eval ( xtVS,ut ) + Q.sqrt_mult ( et );119 dtVS = hxu.eval ( xtVS,ut ) + R.sqrt_mult ( wt );120 129 121 130 //estimator … … 123 132 M.bayes ( concat ( dt,ut ) ); 124 133 125 Xt.set_col ( tK,vec ( x,9 ) ); //vec from C-array 126 Dt.set_col ( tK, concat ( dt,ut ) ); 127 XtE.set_col ( tK,KFEep.mean() ); 128 XtM.set_col ( tK,Mep.mean() ); 129 XtV.set_col ( tK,xtVS ); 134 Xt.set_row ( tK,vec ( x,9 ) ); //vec from C-array 135 Dt.set_row ( tK, concat ( dt,ut,vec_1(sqrt(pow(ut(0),2)+pow(ut(1),2))), vec_1(sqrt(pow(dt(0),2)+pow(dt(1),2))) ) ); 136 XtE.set_row ( tK,KFEep.mean() ); 137 XtM.set_row ( tK,Mep.mean() ); 130 138 } 131 139 … … 136 144 fou << Name ( "xthE" ) << XtE; 137 145 fou << Name ( "xthM" ) << XtM; 138 fou << Name ( "xthV" ) << XtV;139 146 //Exit program: 147 148 //////////////// 149 // Just Testing 150 NcFile nc ( "pmsm_sim.nc", NcFile::Replace ); // Create and leave in define mode 151 if ( ! nc.is_valid() ) { std::cerr << "creation of NCFile failed."<<endl;} 152 153 write_to_nc ( nc,Xt,"X","{isa isb om th }" ); 154 write_to_nc ( nc,XtM,"XtM","{q1 q2 isa isb om th }" ); 155 write_to_nc ( nc,XtE,"XE","{isa isb om th }" ); 156 write_to_nc ( nc,Dt,"Dt","{isa isb ua ub }" ); 140 157 return 0; 158 } 141 159 142 }