easykf-2.04
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
ekf_types.h
Go to the documentation of this file.
1 /* ekf_types.h
2  *
3  * Copyright (C) 2011-2014 Jeremy Fix
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #ifndef EKF_TYPES_H
21 #define EKF_TYPES_H
22 
23 #include <gsl/gsl_vector.h>
24 #include <gsl/gsl_matrix.h>
25 #include "ukf_math.h"
26 
30 namespace ekf
31 {
32 
33  class EvolutionNoise;
34 
39  typedef struct
40  {
41 
46 
51 
55  double prior_pk;
56 
60  int n;
61 
65  int no;
66 
71 
72  } ekf_param;
73 
74  typedef struct
75  {
79  gsl_vector * xk;
80 
84  gsl_vector * xkm;
85 
89  gsl_matrix * Pxk;
90 
94  gsl_matrix *Fxk;
95 
99  gsl_matrix * Rv;
100 
104  gsl_vector * yk;
105 
109  gsl_vector * ino_yk;
110 
114  gsl_matrix *Hyk;
115 
119  gsl_matrix * Rn;
120 
124  gsl_matrix *Kk;
125 
129  gsl_matrix * temp_n_n;
130  gsl_matrix * temp_n_1;
131  gsl_matrix * temp_no_no;
132  gsl_matrix * temp_n_no;
133  gsl_matrix * temp_2_n_n;
134  gsl_vector * temp_no;
135 
139  gsl_vector * params;
140 
141 
142 
143  } ekf_state;
144 
149  {
150  protected:
152  public:
153  EvolutionNoise(double initial_value) : _initial_value(initial_value) {};
154  void init(ekf_param &p, ekf_state &s)
155  {
156  gsl_matrix_set_identity(s.Rv);
157  gsl_matrix_scale(s.Rv, _initial_value);
158  }
159 
160  virtual void updateEvolutionNoise(ekf_param &p, ekf_state &s) = 0;
161  };
162 
167  {
168  double _decay, _lower_bound;
169  public:
170  EvolutionAnneal(double initial_value, double decay, double lower_bound) : EvolutionNoise(initial_value),
171  _decay(decay),
172  _lower_bound(lower_bound)
173  { };
174 
176  {
177  for(int i = 0 ; i < p.n ; ++i)
178  {
179  for(int j = 0 ; j < p.n ; ++j)
180  {
181  if(i == j)
182  gsl_matrix_set(s.Rv, i, i, ukf::math::max(_decay * gsl_matrix_get(s.Rv,i,i),_lower_bound));
183  else
184  gsl_matrix_set(s.Rv, i, j, 0.0);
185  }
186  }
187  }
188  };
189 
194  {
195  double _decay;
196  public:
197  EvolutionRLS(double initial_value, double decay) : EvolutionNoise(initial_value), _decay(decay)
198  {
199  if(ukf::math::cmp_equal(_decay, 0.0))
200  printf("Forgetting factor should not be null !!\n");
201  };
202 
204  {
205  gsl_matrix_memcpy(s.Rv, s.Pxk);
206  gsl_matrix_scale(s.Rv, 1.0 / _decay - 1.0);
207  }
208  };
209 
214  {
215  double _alpha;
216  public:
217  EvolutionRobbinsMonro(double initial_value, double alpha) : EvolutionNoise(initial_value),
218  _alpha(alpha)
219  { };
220 
222  {
223  // Compute Kk * ino_yk
224  gsl_matrix_view mat_view = gsl_matrix_view_array(s.ino_yk->data, p.no, 1);
225  gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, s.Kk, &mat_view.matrix, 0.0, s.temp_n_1);
226  // Compute : Rv = (1 - alpha) Rv + alpha . (Kk * ino_yk) * (Kk * ino_yk)^T
227  gsl_blas_dgemm(CblasNoTrans, CblasTrans, _alpha, s.temp_n_1, s.temp_n_1, (1.0 - _alpha),s.Rv);
228  }
229  };
230 
231 }
232 
233 #endif // EKF_TYPES_H
double _initial_value
Definition: ekf_types.h:151
void updateEvolutionNoise(ekf_param &p, ekf_state &s)
Definition: ekf_types.h:221
EvolutionNoise(double initial_value)
Definition: ekf_types.h:153
Mother class from which the evolution noises inherit.
Definition: ekf_types.h:148
Robbins-Monro evolution noise.
Definition: ekf_types.h:213
EvolutionRobbinsMonro(double initial_value, double alpha)
Definition: ekf_types.h:217
double observation_noise
Covariance of the observation noise.
Definition: ekf_types.h:50
virtual void updateEvolutionNoise(ekf_param &p, ekf_state &s)=0
int no
Dimension of the output.
Definition: ekf_types.h:65
gsl_matrix * Rv
Variance covariance of the evolution noise.
Definition: ekf_types.h:99
gsl_vector * ino_yk
Current innovations.
Definition: ekf_types.h:109
gsl_matrix * temp_n_no
Definition: ekf_types.h:132
gsl_vector * yk
Current observation.
Definition: ekf_types.h:104
Structure holding the parameters of the statistical linearization.
Definition: ekf_types.h:39
Definition: ekf_types.h:74
EvolutionRLS(double initial_value, double decay)
Definition: ekf_types.h:197
gsl_matrix * Rn
Variance covariance of the observation noise.
Definition: ekf_types.h:119
Forgetting type evolution noise.
Definition: ekf_types.h:193
double max(double a, double b)
Definition: ukf_math.h:40
gsl_vector * xkm
Predicted state.
Definition: ekf_types.h:84
gsl_vector * xk
Current estimate of the state.
Definition: ekf_types.h:79
EvolutionNoise * evolution_noise
Evolution noise type.
Definition: ekf_types.h:45
gsl_matrix * temp_n_n
Temporary matrices.
Definition: ekf_types.h:129
gsl_matrix * Hyk
Jacobian of the observation function.
Definition: ekf_types.h:114
gsl_matrix * Kk
Kalman gain.
Definition: ekf_types.h:124
EvolutionAnneal(double initial_value, double decay, double lower_bound)
Definition: ekf_types.h:170
gsl_vector * temp_no
Definition: ekf_types.h:134
gsl_matrix * temp_no_no
Definition: ekf_types.h:131
gsl_matrix * temp_n_1
Definition: ekf_types.h:130
Annealing type evolution noise.
Definition: ekf_types.h:166
void updateEvolutionNoise(ekf_param &p, ekf_state &s)
Definition: ekf_types.h:175
gsl_matrix * Fxk
Jacobian of the evolution function.
Definition: ekf_types.h:94
double prior_pk
Prior estimate of the covariance matrix.
Definition: ekf_types.h:55
void init(ekf_param &p, ekf_state &s)
Definition: ekf_types.h:154
gsl_matrix * Pxk
Variance-Covariance of the state.
Definition: ekf_types.h:89
bool cmp_equal(double x, double y)
Definition: ukf_math.h:50
gsl_matrix * temp_2_n_n
Definition: ekf_types.h:133
void updateEvolutionNoise(ekf_param &p, ekf_state &s)
Definition: ekf_types.h:203
int n
Number of parameters to estimate.
Definition: ekf_types.h:60
bool observation_gradient_is_diagonal
Is the observation gradient diagonal ? In that case, simplifications can be introduced.
Definition: ekf_types.h:70
gsl_vector * params
Optional parameters (this must be allocated and initialized from the user side!
Definition: ekf_types.h:139