23 arma::uword Nsamp = 120;
31 UKF kalman( N, M, L );
42 arma::mat x = { 0, 5, 10, 50, -0.5, -9.8 };
43 arma::inplace_trans( x );
44 arma::mat x_init( x * 0.5 );
48 arma::mat A = { { 1, 0, dT, 0, dT * dT / 2, 0 }, { 0, 1, 0, dT, 0, dT * dT / 2 }, { 0, 0, 1, 0, dT, 0 }, { 0, 0, 0, 1, 0, dT }, { 0, 0, 0, 0, 1, 0 }, { 0, 0, 0, 0, 0, 1 } };
60 arma::mat Q = Q0 * arma::diagmat( arma::vec(
"1 1 0.1 0.1 0.01 0.01" ) );
66 return sqrt( x( 0 ) * x( 0 ) + x( 1 ) * x( 1 ) );
70 return atan2( x( 1 ), x( 0 ) );
89 arma::mat P = P0 * arma::eye<arma::mat>( N, N );
93 arma::mat R = { { 1, 0 }, { 0, 0.005 } };
98 arma::mat z( M, Nsamp, arma::fill::zeros );
99 arma::mat z0( M, Nsamp, arma::fill::zeros );
100 for( arma::uword n = 0; n < Nsamp; n++ )
102 arma::mat v0( N, 1, arma::fill::zeros );
105 x = A * x + Q * arma::randn( N, 1 );
112 z.col( n ) =
eval_fcn( h, z0.col( n ) ) + 0.5 * R * arma::randn( M, 1 );
115 arma::mat xhat_log( N, Nsamp );
116 arma::mat e_log( M, Nsamp );
117 arma::cube P_log( N, N, Nsamp );
118 arma::mat xs_log( M, Nsamp );
119 arma::cube Ps_log( N, N, Nsamp );
120 arma::cube K_log( N, M, Nsamp );
123 for( arma::uword n = 0; n < Nsamp; n++ )
126 kalman.
update( z.col( n ) );
132 e_log.col( n ) = kalman.
get_err();
138 kalman.
rts_smooth( xhat_log, P_log, xs_log, Ps_log );
142 gp0.
window(
"Plot", 10, 10, 500, 500 );
144 gp0.
plot_add( z0.row( 0 ), z0.row( 1 ),
"True Y",
"lines dashtype 2 linecolor \"black\"" );
145 gp0.
plot_add( arma::mat( z.row( 0 ) % cos( z.row( 1 ) ) ), arma::mat( z.row( 0 ) % sin( z.row( 1 ) ) ),
"Meas Y",
"points" );
146 gp0.
plot_add( xhat_log.row( 0 ), xhat_log.row( 1 ),
"Kalman x hat" );
147 gp0.
plot_add( xs_log.row( 0 ), xs_log.row( 1 ),
"RTS smooth" );
151 gp1.
window(
"Plot", 800, 10, 500, 500 );
153 gp1.
plot_add( xhat_log.row( 4 ),
"Acc x" );
154 gp1.
plot_add( xhat_log.row( 5 ),
"Acc y" );
158 gp2.
window(
"Plot", 800, 10, 500, 500 );
161 arma::mat ppp = P_log.tube( 0, 0 );
163 ppp = P_log.tube( 1, 1 );
165 ppp = P_log.tube( 2, 2 );
167 ppp = P_log.tube( 3, 3 );
169 ppp = P_log.tube( 4, 4 );
171 ppp = P_log.tube( 5, 5 );