22 arma::uword Nsamp = 120;
41 arma::mat x = {0, 5, 10, 50, -0.5, -9.8};
42 arma::inplace_trans(x);
43 arma::mat x_init(x * 0.5);
47 arma::mat A = {{1, 0, dT, 0, dT * dT / 2, 0},
48 {0, 1, 0, dT, 0, dT * dT / 2},
64 arma::mat Q = Q0 * arma::diagmat(arma::vec(
"1 1 0.1 0.1 0.01 0.01"));
68 fcn_t h0 =
FCN_XUW {
return sqrt(x(0) * x(0) + x(1) * x(1)); };
87 arma::mat P = P0 * arma::eye<arma::mat>(N, N);
91 arma::mat R = {{1, 0}, {0, 0.005}};
96 arma::mat z(M, Nsamp, arma::fill::zeros);
97 arma::mat z0(M, Nsamp, arma::fill::zeros);
98 for (arma::uword n = 0; n < Nsamp; n++)
100 arma::mat v0(N, 1, arma::fill::zeros);
103 x = A * x + Q * arma::randn(N, 1);
110 z.col(n) =
eval_fcn(h, z0.col(n)) + 0.5 * R * arma::randn(M, 1);
113 arma::mat xhat_log(N, Nsamp);
114 arma::mat e_log(M, Nsamp);
115 arma::cube P_log(N, N, Nsamp);
116 arma::mat xs_log(M, Nsamp);
117 arma::cube Ps_log(N, N, Nsamp);
118 arma::cube K_log(N, M, Nsamp);
121 for (arma::uword n = 0; n < Nsamp; n++)
130 e_log.col(n) = kalman.
get_err();
136 kalman.
rts_smooth(xhat_log, P_log, xs_log, Ps_log);
140 gp0.
window(
"Plot", 10, 10, 500, 500);
142 gp0.
plot_add(z0.row(0), z0.row(1),
"True Y",
143 "lines dashtype 2 linecolor \"black\"");
144 gp0.
plot_add(arma::mat(z.row(0) % cos(z.row(1))),
145 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);
void rts_smooth(const arma::mat &Xf, const arma::cube &Pf, arma::mat &Xs, arma::cube &Ps)
Rauch-Tung-Striebel smoother. See http://www.lce.hut.fi/~ssarkka/course_k2011/pdf/course_booklet_2011...
arma::mat get_state_vec(void)
std::function< double(arma::mat, arma::mat, arma::mat)> fcn_t
Unscented Kalman filter class.
void plot_show(void)
Show plots.
std::vector< fcn_t > fcn_v
arma_inline arma::mat eval_fcn(const fcn_v f, const arma::mat &x, const arma::mat &u, const arma::mat &w)
Evaluate function f(x,u,w)
void predict(const arma::mat u)
Predict the internal states using a control input.
void set_state_vec(const arma::mat &_x)
void set_meas_fcn(fcn_v _h)
void set_proc_noise(const arma::mat &_Q)
arma::mat get_kalman_gain(void)
void set_term(const char *ttype)
Set output terminal.
void set_err_cov(const arma::mat &_P)
void set_trans_mat(const arma::mat &_A)
void window(const int fig, const char *name, const int x, const int y, const int width, const int height)
Configure the figure used Windows environment.
void plot_add(const T1 &x, const T2 &y, const std::string lb, const std::string ls="lines")
Push plot y vs. x with label and linespec.
void update(const arma::mat z)
Correct and update the internal states. UKF.
void set_meas_noise(const arma::mat &_R)
arma::mat get_err_cov(void)