Changeset 133
- Timestamp:
- 07/07/08 19:45:36 (17 years ago)
- Location:
- matlab/mex
- Files:
-
- 1 added
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
matlab/mex/linefit.cpp
r116 r133 1 #include <itpp/itcomm.h>2 1 #include <itpp/itmex.h> 3 2 … … 9 8 // Check the number of inputs and output arguments 10 9 if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); 11 if ( n_input!= 1) mexErrMsgTxt ( "Wrong number of input variables!" );10 if ( n_input!=3 ) mexErrMsgTxt ( "Wrong number of input variables!" ); 12 11 13 12 // Convert input variables to IT++ format 14 13 mat Data = mxArray2mat ( input[0] ); 14 if (Data.rows()!=2) 15 { if (Data.cols()==2) Data=Data.T(); else mexErrMsgTxt("Data are not 2D!");} 16 int ilow = mxArray2int ( input[1] ); 17 int ihi = mxArray2int ( input[2] ); 15 18 16 19 // ------------------ Start of routine --------------------------- 17 20 int ndat=Data.cols(); 18 int npsi=Data.rows() ;21 int npsi=Data.rows()+1; //add a constant 19 22 RV thr ( "1","{ theta_r }",vec_1 ( npsi ),vec_1 ( 0 ) ); 20 23 21 mat V0=1e-8*eye ( npsi ); V0 ( 0,0 ) *=100.0; 22 double nu0=npsi; 24 mat V0=1e-8*eye ( npsi ); V0 ( 0,0 ) =10.0; 25 vec rgr(npsi); 26 double nu0=npsi+2; 27 // fitting a linear part => third coef is "1" 28 rgr(2) = 1.0; 29 30 // AR model 23 31 ARX Ar ( thr,V0,nu0 ); 24 // estimate 25 for (int i=0;i<ndat;i++ ) {Ar.bayes ( Data.get_col ( i ) ); } 32 // start at i0 33 bool gohi=true; bool golow=true; 34 // certain period 35 for (int i=ilow;i<ihi;i++){ 36 rgr.set_subvector(0,Data.get_col ( i )); 37 Ar.bayes ( rgr ); 38 } 39 double tll=Ar._tll(); 40 // go hi 41 do { 42 rgr.set_subvector(0,Data.get_col ( ++ihi )); 43 Ar.bayes (rgr); 44 if (Ar._tll()>tll) tll=Ar._tll(); 45 else gohi=false; 46 } while (gohi); 47 // go low 48 do { 49 rgr.set_subvector(0,Data.get_col ( --ilow )); 50 Ar.bayes (rgr); 51 if (Ar._tll()>tll) tll=Ar._tll(); 52 else golow=false; 53 } while (golow); 26 54 27 55 vec th = Ar._epdf().mean(); 56 28 57 29 58 // ------------------ End of routine ----------------------------- … … 35 64 36 65 if ( n_output>1 ) { 37 double ll=Ar._tll();38 66 output[1] = mxCreateDoubleMatrix ( 1,1, mxREAL ); 39 double2mxArray ( ll, output[1] ); 67 double2mxArray ( ilow, output[1] ); 68 } 69 if ( n_output>1 ) { 70 output[2] = mxCreateDoubleMatrix ( 1,1, mxREAL ); 71 double2mxArray ( ihi, output[2] ); 40 72 } 41 73 42 if ( n_output>2 ) {43 ivec str = Ar.structure_est ( egiw ( thr,V0,nu0 ) );44 output[2] = mxCreateDoubleMatrix ( 1,str.length(), mxREAL );45 ivec2mxArray ( str, output[2] );46 }47 74 }