Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IteratedExtendedKalman.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __ITEREXTKALMAN_HH__
27 #define __ITEREXTKALMAN_HH__
28 
29 #include <Base/Debug/Debug.hh>
30 #include <Base/Math/Matrix.hh>
31 #include <Base/Math/Vector.hh>
32 #include <MathAlgo/SVD.hh>
33 
34 namespace BIAS {
35 
36  /**
37  @class IteratedExtendedKalman
38 
39  @ingroup g_stateestimation
40 
41  @brief Iterated Extended Kalman-filter (IEKF). Implemented according to:
42  www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf [1] with
43  additional iteration option. Implements an Iterated Extended Kalman
44  Filter, estimating the state of a system driven by a given possibly non-
45  linear statetransition-funcition. Derive from this class an implement the
46  virtual prediction functions (state transition, measurement prediction).
47  Jacobians for model functions and according noise models have to be given.
48 
49  @author apetersen 05.08.10
50  */
51 
52  class BIASStateEstimator_EXPORT IteratedExtendedKalman : public Debug
53  {
54  public:
55  /** Standard constructor. Initiates object using maxIters=20, iterEps=1e-4
56  and bUseStable=false.
57  */
59 
60  /** Constructor using given parameters.
61  @params maxIters maximum iterations for update
62  @params iterEps break update iteration if update < iterEps
63  @params bUseStable use stable (slower) update equation
64  */
65  IteratedExtendedKalman(unsigned int maxIters,
66  double iterEps,
67  bool bUseStable=false);
68 
69  /** Virtual standard destructor
70  */
72 
73  /** Returns the predicted (a priori) system state.
74  */
75  inline void GetPriorState(BIAS::Vector<double>& state) const
76  { state = statePrior_; }
77 
78  /** Returns the last predicted (a priori) system state covariance.
79  */
80  inline void GetPriorCovariance(BIAS::Matrix<double>& cov) const
81  { cov = covariancePrior_; }
82 
83  /** Returns the last corrected (a posteriori) system state.
84  */
85  inline void GetPosterioriState(BIAS::Vector<double>& state) const
86  { state = statePost_; }
87 
88  /** Returns the updated (a posteriori) system state covariance.
89  */
91  { cov = covariancePost_; }
92 
93  /** Set a modified a state. Use this if an external transformation has been
94  applied to the state (e.g. coordinate transformations) or the state
95  dimension has changed. Modify state covariance accordingly.
96  */
98  {
99 #ifdef BIAS_DEBUG
100  BIASASSERT(state.size() == cov.num_cols());
101  BIASASSERT(state.size() == cov.num_rows());
102 #endif //BIAS_DEBUG
103 
104  state_ = state;
105  covariance_ = cov;
106  systemStateDim_ = state_.size();
107  }
108 
109  /** Set the process noise covariance -- Q in [1]
110  --- May be updated for every timestep! ---
111  */
112  inline void SetProcessCov(const BIAS::Matrix<double>& processNoiseCov)
113  {
114 #ifdef BIAS_DEBUG
115  BIASASSERT(processNoiseCov.num_cols() == processNoiseCov.num_rows());
116 #endif //BIAS_DEBUG
117 
118  Q_ = processNoiseCov;
119  processNoiseDim_ = Q_.num_rows();
120  }
121 
122  /** Initialize the kalman filter before using update/predict.
123  @params initialState initial state
124  @params initialCov covariance for initial state
125  @params processNoiseCov process (prediction) noise covariance
126  */
127  void Init(const BIAS::Vector<double>& initialState,
128  const BIAS::Matrix<double>& initialCov,
129  const BIAS::Matrix<double>& processNoiseCov);
130 
131  /** Implements the statetransition-function (f in [1]). Predict the 'state'
132  using the transition function and 'control' vector to get 'prediction'!
133  Has to be implemented in child class!
134  @params state a posterior state
135  @params control vector containing parameters for prediction
136  @params prediction predicte a priori state
137  */
138  virtual void StateTransition(const BIAS::Vector<double>& state,
139  const BIAS::Vector<double>& control,
140  BIAS::Vector<double>& prediction)
141  {
142  BIASERR("StateTransition not implemented in base class!");
143  BIASABORT;
144  }
145 
146  /** Returns the jacobian matrices (by state and process noise) for state
147  transition function. The matrices will be atomatically initialized
148  according to the system state/process noise bevore passed to function
149  call.
150  Has to be implemented in child class!
151  @params state state used for prediction
152  @params control control vector used for prediction
153  @params transJacobian jacobian matrix of transition funciton (by state)
154  @params transErrorJacobian jacobian matrix of TF (by process noise)
155  */
156  virtual void GetTransitionJacobian(const BIAS::Vector<double>& state,
157  const BIAS::Vector<double>& control,
158  BIAS::Matrix<double>& transJacobian,
159  BIAS::Matrix<double>& transErrorJacobian)
160  {
161  BIASERR("GetTransitionJacobian not implemented in base class!");
162  BIASABORT;
163  }
164 
165  /** Implements a single preditcion step (with respect to the control
166  parameters -- u[k] in [1]). It can be called more than once between two
167  updates (iterativ prediction).
168  @params control control vector (u[k]) to be used for prediction
169  */
170  void Predict(const BIAS::Vector<double>& control);
171 
172  /** Implements the measurement prediction function (h in [1])! Since more
173  than one measurement is allowed, an index for determining the respective
174  function can be given.
175  Has to be implemented in child class!
176  @params state the state to predict the measurement from
177  @params measurePred the predicted measurement
178  @params index the index for the measurement funtion to be applied
179  */
180  virtual void MeasurementPrediction(const BIAS::Vector<double>& state,
181  BIAS::Vector<double>& measurePred,
182  const unsigned int index=1)
183  {
184  BIASERR("StateTransition not implemented in base class!");
185  BIASABORT;
186  }
187 
188  /** Returns the jacobian matrix of the measurement-function with respect to
189  the state (H in [1]) as well as the jacobian with respect to the noise
190  model (V in [1]). The matrices will be atomatically initialized
191  according to the measurement/measurement noise bevore passed to function
192  call.
193  Has to be implemented in child class.
194  @params state the state used for measurement prediction
195  @params measJacobian jacobian matrix of measurement funciton (by state)
196  @params measErrorJacobian jacobian matrix of MF (by process noise)
197  */
198  virtual void GetMeasureJacobian(const BIAS::Vector<double> &state,
199  BIAS::Matrix<double> &measJacobian,
200  BIAS::Matrix<double> &measErrorJacobian,
201  const unsigned int index=1)
202  {
203  BIASERR("GetMeasureJacobian not implemented in base class!");
204  BIASABORT;
205  }
206 
207  /** Implememts the iterative update step using measurement and its
208  covariance. Can be call more than once between two predicts. Index
209  indicates which measurement funciton shall be used.
210  @params measurement measurement vector
211  @params measureNoise covariance for measurement
212  @params index indicator for used meansurement function
213  */
214  void Update(const BIAS::Vector<double> &measurement,
215  const BIAS::Matrix<double> &measureNoise,
216  const unsigned int index);
217 
218  protected:
219 
220  /// flag for using numerically more stable computations for update
221  /// (higher computation effort)
222  const bool bUseStable_;
223 
224  /// number of maximal iterations for iterated update
225  unsigned int maxIters_;
226 
227  /// break update iteration if update improvement is smaller than iterEps
228  double iterEps_;
229 
230  /// contains the actual system estimate
232 
233  /// contains the last prediction (a priori/initial) system estimate
235 
236  /// contains the corrected (a posteriori) system estimate
238 
239  /// identity matrix frequently used in Update
241 
242  /// the covariance matrix corresponding to state_
244 
245  /// the covariance matrix corresponding to statePrior_
247 
248  /// the covariance matrix corresponding to statePost_
250 
251  /// measurement jacobian for measurement model and its transpose
253 
254  /// derived state-trans. matrix and transposed
256 
257  /// system noise cov.
259 
260  /// state-trans. error jacobian and transposed
262 
263  /// measurement error jacobian for measurement model and its transpose
265 
266  /// dimension of state space
267  unsigned int systemStateDim_;
268 
269  /// dimension of process noise
270  unsigned int processNoiseDim_;
271 
272  /// needed for matrix inversion
274 
275  };
276 
277 } // namespace BIAS
278 
279 
280 #endif // __ITEREXTKALMAN_HH__
BIAS::Vector< double > statePost_
contains the corrected (a posteriori) system estimate
virtual void GetTransitionJacobian(const BIAS::Vector< double > &state, const BIAS::Vector< double > &control, BIAS::Matrix< double > &transJacobian, BIAS::Matrix< double > &transErrorJacobian)
Returns the jacobian matrices (by state and process noise) for state transition function.
double iterEps_
break update iteration if update improvement is smaller than iterEps
void GetPosterioriState(BIAS::Vector< double > &state) const
Returns the last corrected (a posteriori) system state.
Iterated Extended Kalman-filter (IEKF).
unsigned int maxIters_
number of maximal iterations for iterated update
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
Subscript num_cols() const
Definition: cmat.h:320
unsigned int processNoiseDim_
dimension of process noise
BIAS::Vector< double > state_
contains the actual system estimate
void SetState(BIAS::Vector< double > &state, BIAS::Matrix< double > &cov)
Set a modified a state.
BIAS::Matrix< double > covariancePost_
the covariance matrix corresponding to statePost_
void SetProcessCov(const BIAS::Matrix< double > &processNoiseCov)
Set the process noise covariance – Q in [1] — May be updated for every timestep! —.
BIAS::Matrix< double > covariancePrior_
the covariance matrix corresponding to statePrior_
BIAS::Vector< double > statePrior_
contains the last prediction (a priori/initial) system estimate
virtual ~IteratedExtendedKalman()
Virtual standard destructor.
void GetPosterioriCovariance(BIAS::Matrix< double > &cov) const
Returns the updated (a posteriori) system state covariance.
BIAS::Matrix< double > Identity_
identity matrix frequently used in Update
void GetPriorCovariance(BIAS::Matrix< double > &cov) const
Returns the last predicted (a priori) system state covariance.
unsigned int systemStateDim_
dimension of state space
Subscript size() const
Definition: cmat.h:212
BIAS::Matrix< double > covariance_
the covariance matrix corresponding to state_
const bool bUseStable_
flag for using numerically more stable computations for update (higher computation effort) ...
virtual void StateTransition(const BIAS::Vector< double > &state, const BIAS::Vector< double > &control, BIAS::Vector< double > &prediction)
Implements the statetransition-function (f in [1]).
virtual void MeasurementPrediction(const BIAS::Vector< double > &state, BIAS::Vector< double > &measurePred, const unsigned int index=1)
Implements the measurement prediction function (h in [1])! Since more than one measurement is allowed...
BIAS::SVD svd_
needed for matrix inversion
Subscript num_rows() const
Definition: cmat.h:319
virtual void GetMeasureJacobian(const BIAS::Vector< double > &state, BIAS::Matrix< double > &measJacobian, BIAS::Matrix< double > &measErrorJacobian, const unsigned int index=1)
Returns the jacobian matrix of the measurement-function with respect to the state (H in [1]) as well ...
void GetPriorState(BIAS::Vector< double > &state) const
Returns the predicted (a priori) system state.
Subscript size() const
Definition: vec.h:262
BIAS::Matrix< double > Q_
system noise cov.