Index: /bdm/stat/libDS_ui.h
===================================================================
--- /bdm/stat/libDS_ui.h (revision 280)
+++ /bdm/stat/libDS_ui.h (revision 342)
@@ -122,3 +122,23 @@
 };
 UIREGISTER ( UIstateDS );
+
+/*! \brief UI for reading data from files
+
+\code
+\endcode
+*/
+
+class UIITppFileDS : public UIbuilder {
+	public:
+		UIITppFileDS() :UIbuilder ( "ITppFileDS" ) {};
+		bdmroot* build ( Setting &S ) const {
+			RV* rvtmp;
+			UIbuild(S["rv"], rvtmp);
+			ITppFileDS *DS=new ITppFileDS(S["filename"],S["varname"]);
+			DS->set_drv(*rvtmp,RV());
+			return DS;
+		}
+};
+UIREGISTER ( UIITppFileDS );
+
 #endif // DS_UI_H
Index: /bdm/stat/libDS.h
===================================================================
--- /bdm/stat/libDS.h (revision 313)
+++ /bdm/stat/libDS.h (revision 342)
@@ -68,4 +68,8 @@
 	//! returns number of data in the file;
 	int ndat(){return Data.cols();}
+	//! no sense to log this type
+	void log_add ( logger &L ) {};
+	//! no sense to log this type
+	void logit ( logger &L ) {};
 };
 
@@ -76,8 +80,8 @@
 
 */
-class ItppFileDS: public FileDS {
-
-public:
-	ItppFileDS ( const string &fname, const string &varname ) :FileDS() {
+class ITppFileDS: public FileDS {
+
+public:
+	ITppFileDS ( const string &fname, const string &varname ) :FileDS() {
 		it_file it ( fname );
 		it << Name ( varname ); 
@@ -85,5 +89,5 @@
 		time = 0;
 		//rowid and delays are ignored
-	}
+	};
 };
 
Index: /bdm/stat/libBM.h
===================================================================
--- /bdm/stat/libBM.h (revision 340)
+++ /bdm/stat/libBM.h (revision 342)
@@ -629,4 +629,6 @@
 	//!access function
 	const RV& _urv() const {return Urv;}
+	//! set random rvariables
+	virtual void set_drv (const  RV &drv, const RV &urv) { Drv=drv;Urv=urv;}
 };
 
Index: /library/mex/mexlog.h
===================================================================
--- /library/mex/mexlog.h (revision 293)
+++ /library/mex/mexlog.h (revision 342)
Index: /library/mex/mexds.h
===================================================================
--- /library/mex/mexds.h (revision 314)
+++ /library/mex/mexds.h (revision 342)
Index: /applications/pmsm/pmsm_ui.h
===================================================================
--- /applications/pmsm/pmsm_ui.h (revision 332)
+++ /applications/pmsm/pmsm_ui.h (revision 342)
@@ -127,2 +127,34 @@
 };
 UIREGISTER ( UIpmsmCRB );
+
+class UIpmsmCRBMz: public UIbuilder {
+	public:
+		UIpmsmCRBMz():UIbuilder("pmsmCRBMz"){};
+		bdmroot* build ( Setting &S ) const {
+			diffbifn* IM; UIbuild(S["IM"],IM);
+			diffbifn* OM; UIbuild(S["OM"],OM);
+		
+		//parameters
+			pmsmCRBMz* E; E=new pmsmCRBMz;
+		
+		//statistics
+			int dim=IM->dimension();
+			vec mu0;
+			mat P0;
+			if (S.exists("mu0")){mu0=getvec(S["mu0"]);}else{mu0=zeros(dim);};
+			if (S.exists("dP0")){P0=diag(getvec(S["dP0"]));}else{
+				if (S.exists("P0")){mu0=getmat(S["P0"],dim);}else{P0=eye(dim);};
+			}
+			E->set_statistics(mu0,P0);
+		
+			E->set_parameters(IM, OM, diag(getvec(S["dQ"])), diag(getvec(S["dR"])));
+
+		//connect
+			RV* drv; UIbuild(S["drv"],drv);
+			E->set_drv(*drv);
+			RV* rv; UIbuild(S["rv"],rv);
+			E->set_rv(*rv);
+			return E;
+		}	
+};
+UIREGISTER ( UIpmsmCRBMz );
Index: /applications/pmsm/Barcelona/QandR.m
===================================================================
--- /applications/pmsm/Barcelona/QandR.m (revision 338)
+++ /applications/pmsm/Barcelona/QandR.m (revision 342)
@@ -30,15 +30,19 @@
 R11 = (istep+istep)^2/12
 
-%diffs
-Tl=10;
-th=[0 pi/2 pi];
+% %diffs
+ Tl=10;
+% th=[0 pi/2 pi];
+% dom = p/J*Tl*dt
+% dia = Fmag/Ls*dt*dom*sin(th)
+% dib = -Fmag/Ls*dt*dom*cos(th)
+% dth = dom*dt
+% 
+% % from om=0;
+% dom2= dom + kp*p*p * Fmag/J*dt* ( dib .* cos ( th +dth)- dia .* sin ( th +dth) ) + dom
+% dia = Fmag/Ls*dt*dom2.*sin(th+dth)
+% dib = -Fmag/Ls*dt*dom2.*cos(th+dth)
+% dth = dom2*dt
+% 
+
 dom = p/J*Tl*dt
-dia = Fmag/Ls*dt*dom*sin(th)
-dib = -Fmag/Ls*dt*dom*cos(th)
-dth = dom*dt
-
-% from om=0;
-dom2= dom + kp*p*p * Fmag/J*dt* ( dib .* cos ( th +dth)- dia .* sin ( th +dth) ) + dom
-dia = Fmag/Ls*dt*dom2.*sin(th+dth)
-dib = -Fmag/Ls*dt*dom2.*cos(th+dth)
-dth = dom2*dt
+Qrat=[Fmag/Ls*dt,Fmag/Ls*dt, 1, dt].^2
Index: /applications/pmsm/Barcelona/Mz.cfg
===================================================================
--- /applications/pmsm/Barcelona/Mz.cfg (revision 338)
+++ /applications/pmsm/Barcelona/Mz.cfg (revision 342)
@@ -2,5 +2,5 @@
 
 experiment={
-	ndat = 90000;
+	ndat = 120000;
 };
 
@@ -9,6 +9,7 @@
 	params = {type = "external";filename = "../cfg/zcu.cfg";path = "pmsm107";};
 	tstep = 1.0; // steps for profile in [s]
-	profileW = [1, 5, 10, 10, 3, 0, -3, -10, -10, 3, 0];
-	profileM = [0, 10, 0, 0, 10, 0, 10, 0, 10, 0];
+	profileW = [0, 1, 5, 10, 10, 3, 0, 0, -3, -3, -10, -10, -4, 0, 0];
+	profileM = [0, 10, 0, 0, 10, 0, 0, 0, 0, 0, 10, 0, 0];
+	options = "modelu";
 };
 
@@ -20,12 +21,17 @@
 	 };
 	 OM = {type="pmsmOM";};
-	 dQ=[0.17, 0.17, 0.004, 1e-6, 1e-1];
+/*	 dQ : 
+	{
+	elements = [ 0.001738643873, 0.001738643873, 1.00000004, 1.562501e-08, 1 ];
+	cols = 4;
+	};*/
+	dQ=[0.0017, 0.0017, 0.00000004, 1e-14, 1e-1];
 	 dR=[0.025, 0.025];
-	 dP0=[1., 1., 1., 1., 1e-2];
+	 dP0=[0., 0., 0., 0., 0.];
 	 //connect
 	 drv = {type="rv"; names=["o_ia","o_ib","o_ua","o_ub"];};  // observed u
 	 //drv = {type="rv"; names=["o_ia","o_ib","t_ua","t_ub"];}; // true u
 	 rv = {type="rv"; names=["EKFMz"]; sizes=[5];};
-	 options="logll";
+	 options="logll,logbounds";
 	 },
 				 //
@@ -36,5 +42,5 @@
 	 };
 	 OM = {type="pmsmOM";};
-	 dQ=[0.17, 0.17, 0.00004, 1e-8];
+	 dQ=[0.017, 0.017, .1, 1e-6];
 	 dR=[0.025, 0.025];
 	 dP0=[1., 1., 1., 1.];
@@ -43,5 +49,5 @@
 	 //drv = {type="rv"; names=["o_ia","o_ib","t_ua","t_ub"];}; // true u
 	 rv = {type="rv"; names=["EKF"]; sizes=[4];};
-	 options="logll";
+	 options="logll,logbounds";
 	 },
 				 				 //
@@ -52,5 +58,5 @@
 	 };
 	 OM = {type="pmsmOM";};
-	 dQ=[0.17, 0.17, 0.004, 1e-5];
+	 dQ=[0.017, 0.017, .1, 1e-6];//[0.017, 0.017, 100., 1e-4];
 	 dR=[0.025, 0.025];
 	 dP0=[1., 1., 1., 1.];
@@ -59,5 +65,22 @@
 	 //drv = {type="rv"; names=["o_ia","o_ib","t_ua","t_ub"];}; // true u
 	 rv = {type="rv"; names=["EKFw"]; sizes=[4];};
-	 options="logll";
+	 options="logll,logbounds";
+	 },
+
+	 {type="pmsmCRBMz";
+	 IM = {type="pmsmIM";
+		params = {type = "external";filename = "../cfg/zcu.cfg";path = "pmsm107";};
+		variant = "Mz";
+	 };
+	 OM = {type="pmsmOM";};
+	dQ=[0.0017, 0.0017, 0.00000004, 1e-14, 1e-1];
+//	 dQ=[0.017, 0.017, .1, 1e-6];//[0.017, 0.017, 100., 1e-4];
+	 dR=[0.025, 0.025];
+	 dP0=[0., 0., 0., 0., 0.];
+	 //connect
+	 drv = {type="rv"; names=["o_ia","o_ib","t_ua","t_ub"];};  // observed u
+	 //drv = {type="rv"; names=["o_ia","o_ib","t_ua","t_ub"];}; // true u
+	 rv = {type="rv"; names=["CRB"]; sizes=[5];};
+	 options="logll,logbounds";
 	 }
 
@@ -67,5 +90,5 @@
 logger= {
 	type="dirfilelog";
-	maxlen = 1;
+	maxlen = 100;
 	dirname = "exp/Mz";
 };
Index: /applications/pmsm/Barcelona/MM.cfg
===================================================================
--- /applications/pmsm/Barcelona/MM.cfg (revision 342)
+++ /applications/pmsm/Barcelona/MM.cfg (revision 342)
@@ -0,0 +1,60 @@
+// settings for pmsm_estim.cpp
+
+experiment={
+	ndat = 60000;
+};
+
+system= {
+	type = "pmsmDS";
+	params = {type = "external";filename = "../cfg/zcu.cfg";path = "pmsm107";};
+	tstep = 1.0; // steps for profile in [s]
+	profileW = [1, 5, 10, 10, 10, 3, 0, 0];
+	profileM = [0, 0, 10, 0, 10, 0, 10, 0];
+};
+
+E1={type="EKF";
+	 IM = {type="pmsmIM";
+		params = {type = "external";filename = "../cfg/zcu.cfg";path = "pmsm107";};
+		variant = "2o";
+	 };
+	 OM = {type="pmsmOM";};
+	 dQ=[1.7, 1.7, 0.004, 1e-5];
+	 dR=[0.025, 0.025];
+	 dP0=[1., 1., 1., 1.];
+	 //connect
+	 drv = {type="rv"; names=["o_ia","o_ib","o_ua","o_ub"];};  // observed u
+	 //drv = {type="rv"; names=["o_ia","o_ib","t_ua","t_ub"];}; // true u
+	 rv = {type="rv"; names=["EKF"]; sizes=[4];};
+	 options="logll";
+	 };
+E2= {type="EKF";
+	 IM = {type="pmsmIM";
+		params = {type = "external";filename = "../cfg/zcu.cfg";path = "pmsm107";};
+		variant = "Stat";
+	 };
+	 OM = {type="pmsmOM";};
+	 dQ=[1.7, 1.7, 0.004, 1e-5];
+	 dR=[0.025, 0.025];
+	 dP0=[1., 1., 1., 1.];
+	 //connect
+	 drv = {type="rv"; names=["o_ia","o_ib","o_ua","o_ub"];};  // observed u
+	 //drv = {type="rv"; names=["o_ia","o_ib","t_ua","t_ub"];}; // true u
+	 rv = {type="rv"; names=["EKFw"]; sizes=[4];};
+	 options="logll";
+	 };
+	 
+estimator = (
+	{type="internal";path="E1";},
+	{type="internal";path="E2";},
+	{type="MultiModel";
+	 models=(
+	 	{type="internal";path="E1";},
+		{type="internal";path="E2";});
+		options="logll";
+	});
+	
+logger= {
+`	type="dirfilelog";
+	maxlen = 1;
+	dirname = "MM";
+};
Index: /applications/pmsm/pmsmDS.h
===================================================================
--- /applications/pmsm/pmsmDS.h (revision 332)
+++ /applications/pmsm/pmsmDS.h (revision 342)
@@ -35,5 +35,5 @@
 public:
 	//! Constructor with fixed sampling period
-	pmsmDS ()   {Dt=125; Drv=RV ( "{o_ua o_ub o_ia o_ib t_ua t_ub o_om o_th }" );}
+	pmsmDS ()   {Dt=125; Drv=RV ( "{o_ua o_ub o_ia o_ib t_ua t_ub o_om o_th Mz }" );}
 	void set_parameters ( double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0 ) {
 		pmsmsim_set_parameters ( Rs0, Ls0, Fmag0, Bf0, p0, kp0, J0, Uc0, DT0, dt0 );
@@ -41,7 +41,7 @@
 	//! parse options: "modelu" => opt_modu=true;
 	void set_options ( string &opt ) {
-		opt_modu = ( opt.find ( "modelu" ) ==string::npos );
-	}
-	void getdata ( vec &dt ) {dt.set_subvector(0,vec ( KalmanObs,6 ));dt(6)=x[2];dt(7)=x[3];}
+		opt_modu = ( opt.find ( "modelu" ) !=string::npos );
+	}
+	void getdata ( vec &dt ) {dt.set_subvector(0,vec ( KalmanObs,6 ));dt(6)=x[2];dt(7)=x[3];dt(8)=x[8];}
 	void write ( vec &ut ) {}
 
@@ -82,5 +82,5 @@
 		
 		//discretization
-		double ustep=0.6;
+		double ustep=1.2;
 		KalmanObs [ 0 ] = ustep*itpp::round( KalmanObs [ 0 ]/ ustep) ;
 		KalmanObs [ 1 ] = ustep*itpp::round(KalmanObs [ 1 ]/ ustep);
@@ -177,2 +177,30 @@
 	}
 };
+
+//! This class behaves like BM but it is evaluating EKF
+class pmsmCRBMz : public EKFfull{
+	protected:
+		int L_CRB;
+	public:
+	//! constructor
+		pmsmCRBMz():EKFfull(){}
+
+		void bayes(const vec &dt){
+//assume we know state exactly:
+			vec true_state(5);
+			true_state.set_subvector(0,vec(x,4)); // read from pmsm
+			true_state(4)=x[8];
+			
+			E.set_mu(true_state);
+			mu = true_state;
+			//hack for ut
+			EKFfull::bayes(dt);
+		}
+	
+		void log_add(logger &L, const string &name="" ){
+			L_CRB=L.add(concat(rx,RV("Mz",1,0)),"crb");
+		}
+		void logit(logger &L){
+			L.logit(L_CRB,diag(_R()));
+		}
+};
Index: /applications/pmsm/pmsm_estim.cpp
===================================================================
--- /applications/pmsm/pmsm_estim.cpp (revision 318)
+++ /applications/pmsm/pmsm_estim.cpp (revision 342)
@@ -10,4 +10,5 @@
 #include "stat/loggers_ui.h"
 #include "estim/KF_ui.h"
+#include "stat/libDS_ui.h"
 
 using namespace bdm;
