00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <iostream>
00033 #include "fastlib/fastlib.h"
00034 #include "kalman_helper.h"
00035 #include "kalman.h"
00036 using namespace std;
00037
00070 int main(int argc, char* argv[]) {
00071
00072 fx_init(argc, argv, NULL);
00073
00075
00076
00077
00078 const char* t_in = fx_param_str(NULL, "t_in", "t_in");
00079 Matrix t_tot_mat; data::Load(t_in, &t_tot_mat);
00080 int t_tot = (int)t_tot_mat.get(0, 0);
00081
00082
00083
00084
00085
00086 const char* a_in = fx_param_str(NULL, "a_in", "a_in");
00087 const char* b_in = fx_param_str(NULL, "b_in", "b_in");
00088 const char* c_in = fx_param_str(NULL, "c_in", "c_in");
00089 const char* q_in = fx_param_str(NULL, "q_in", "q_in");
00090 const char* r_in = fx_param_str(NULL, "r_in", "r_in");
00091 const char* s_in = fx_param_str(NULL, "s_in", "s_in");
00092
00093 ssm lds;
00094 data::Load(a_in, &lds.a_mat);
00095 data::Load(b_in, &lds.b_mat);
00096 data::Load(c_in, &lds.c_mat);
00097 data::Load(q_in, &lds.q_mat);
00098 data::Load(r_in, &lds.r_mat);
00099 data::Load(s_in, &lds.s_mat);
00100
00101
00102
00103
00104 const char* x_pred_0_in = fx_param_str(NULL, "x_pred_0_in", "x_pred_0_in");
00105 const char* p_pred_0_in = fx_param_str(NULL, "p_pred_0_in", "p_pred_0_in");
00106 const char* y_pred_0_in = fx_param_str(NULL, "y_pred_0_in", "y_pred_0_in");
00107 const char* inno_cov_0_in = fx_param_str(NULL, "inno_cov_0_in",
00108 "inno_cov_0_in");
00109
00110 Matrix x_pred_0; data::Load(x_pred_0_in, &x_pred_0);
00111 Matrix y_pred_0; data::Load(y_pred_0_in, &y_pred_0);
00112 Matrix p_pred_0; data::Load(p_pred_0_in, &p_pred_0);
00113 Matrix inno_cov_0; data::Load(inno_cov_0_in, &inno_cov_0);
00114
00115
00116
00117
00118
00119 int nx = lds.a_mat.n_rows();
00120 int ny = lds.c_mat.n_rows();
00121 int nu = lds.b_mat.n_cols();
00122 Matrix x(nx, t_tot + 1); x.SetZero();
00123 Matrix y(ny, t_tot + 1);
00124 Matrix u(nu, t_tot + 1);
00125 Matrix w(nx, t_tot + 1);
00126 Matrix v(ny, t_tot + 1);
00127
00128
00129
00130
00131
00132
00133 Matrix x_pred(nx, t_tot + 1);
00134 Matrix x_hat(nx, t_tot + 1);
00135 Matrix y_pred(ny, t_tot + 1);
00136 Matrix p_pred[t_tot + 1];
00137 Matrix p_hat[t_tot + 1];
00138 Matrix inno_cov[t_tot + 1];
00139 Matrix k_gain[t_tot + 1];
00140 set_portion_of_matrix(x_pred_0, 0, nx-1, 0, 0, &x_pred);
00141 set_portion_of_matrix(y_pred_0, 0, ny-1, 0, 0, &y_pred);
00142 for (int t =0; t<=t_tot; t++ ) {
00143 p_pred[t].Init(nx, nx);
00144 p_hat[t].Init(nx, nx);
00145 k_gain[t].Init(nx, ny);
00146 inno_cov[t].Init(ny, ny);
00147 };
00148 p_pred[0] = p_pred_0;
00149 inno_cov[0] = inno_cov_0;
00150
00151
00152
00153 Matrix noise_mat(nx + ny, nx + ny), s_trans;
00154 la:: TransposeInit(lds.s_mat, &s_trans);
00155 set_portion_of_matrix(lds.q_mat, 0, nx - 1, 0, nx - 1, &noise_mat);
00156 set_portion_of_matrix(lds.s_mat, 0, nx - 1, nx, nx + ny -1, &noise_mat);
00157 set_portion_of_matrix(s_trans, nx, nx + ny - 1,0, nx - 1, &noise_mat);
00158 set_portion_of_matrix(lds.r_mat, nx, nx + ny - 1, nx, nx + ny - 1, &noise_mat);
00159
00160
00162 for (int t=0; t<=t_tot; t++) {
00163
00164
00165
00166
00167
00168
00169
00170 Vector w_t; w.MakeColumnVector(t, &w_t);
00171 Vector v_t; v.MakeColumnVector(t, &v_t);
00172 Vector w_v_t; w_v_t.Init(nx+ny);
00173 RandVector(noise_mat, w_v_t);
00174 extract_sub_vector_of_vector(w_v_t, 0, nx-1, &w_t);
00175 extract_sub_vector_of_vector(w_v_t, nx, nx+ny-1, &v_t);
00176
00177
00178
00179 Vector x_t; x.MakeColumnVector(t, &x_t);
00180 Vector y_t; y.MakeColumnVector(t, &y_t);
00181 propagate_one_step(lds.c_mat, x_t, v_t, &y_t);
00182
00183
00184
00185
00186
00187
00188 Vector x_pred_t; x_pred.MakeColumnVector(t, &x_pred_t);
00189 Vector y_pred_t; y_pred.MakeColumnVector(t, &y_pred_t);
00190 Vector x_hat_t; x_hat.MakeColumnVector(t, &x_hat_t);
00191 KalmanFiltTimeInvariantMstUpdate(lds, y_t, x_pred_t, p_pred[t],
00192 y_pred_t, inno_cov[t], x_hat_t,
00193 p_hat[t], k_gain[t]);
00194
00195
00196
00197
00198
00199 Vector u_t;
00200 u.MakeColumnVector(t, &u_t);
00201 RandVector(u_t);
00202
00203
00204
00205
00206
00207
00208
00209 if (t < t_tot) {
00210 Vector x_pred_t_next; x_pred.MakeColumnVector(t + 1, &x_pred_t_next);
00211 Vector y_pred_t_next; y_pred.MakeColumnVector(t + 1, &y_pred_t_next);
00212 KalmanFiltTimeInvariantTimeUpdate(lds, x_hat_t, p_hat[t], y_t, u_t,
00213 x_pred_t_next, y_pred_t_next,
00214 p_pred[t + 1], inno_cov[t + 1]);
00215
00216 };
00217
00218
00219
00220
00221 if (t < t_tot) {
00222 Vector x_t_next; x.MakeColumnVector(t+1, &x_t_next);
00223 propagate_one_step(lds.a_mat, lds.b_mat, x_t, u_t, w_t, &x_t_next);
00224 };
00225 };
00226
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 Matrix w_trans, v_trans;
00244 Matrix u_trans, x_trans, y_trans;
00245 Matrix x_pred_trans;
00246 Matrix x_hat_trans;
00247 Matrix y_pred_trans;
00248 Matrix k_gain_end_trans;
00249
00250 la::TransposeInit(w, &w_trans);
00251 la::TransposeInit(v, &v_trans);
00252 la::TransposeInit(u, &u_trans);
00253 la::TransposeInit(x, &x_trans);
00254 la::TransposeInit(y, &y_trans);
00255 la::TransposeInit(x_pred, &x_pred_trans);
00256 la::TransposeInit(x_hat, &x_hat_trans);
00257 la::TransposeInit(y_pred, &y_pred_trans);
00258 la::TransposeInit(k_gain[t_tot], &k_gain_end_trans);
00259
00260 data::Save("w_out", w_trans);
00261 data::Save("v_out", v_trans);
00262 data::Save("u_out", u_trans);
00263 data::Save("x_out", x_trans);
00264 data::Save("y_out", y_trans);
00265 data::Save("x_pred_out", x_pred_trans);
00266 data::Save("p_pred_end_out", p_pred[t_tot]);
00267 data::Save("x_hat_out", x_hat_trans);
00268 data::Save("p_hat_end_out", p_hat[t_tot]);
00269 data::Save("y_pred_out", y_pred_trans);
00270 data::Save("inno_cov_end_out", inno_cov[t_tot]);
00271 data::Save("k_gain_end_out", k_gain_end_trans);
00272
00273 fx_done(fx_root);
00274 };