Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExtendedKalman.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 __EXTKALMAN_HH__
27 #define __EXTKALMAN_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 ExtendedKalman
38  @ingroup g_stateestimation
39  @brief Classical ExtendedKalman-filter (EKF). Implemented according to:
40  www.cs.unc.edu/~tracker/media/pdf/SIGGRAPH2001_CoursePack_08.pdf [1]
41  Implements an Extended Kalman Filter (non-linear),estimating the state of a
42  system governed by a given non-linear statetransition-funcition.
43  Derive from this class an implement the virtual statetrans.- and
44  measurement-function. After setting the differentiation matrices
45  you will get an EKF for your specific problem.
46  @author apetersen 03.04.07
47  */
48 
49  class BIASStateEstimator_EXPORT ExtendedKalman : public Debug
50  {
51  public:
53 
54  virtual ~ExtendedKalman(){};
55 
56  /** Returns the predicted system state. This is the prediction, it is
57  to be called after the predict step!
58  */
59  inline void GetState(Vector<double>& state) const
60  { state = priorState_; }
61 
62  /** Returns the corrected system state, which is the update-corrected state.
63  This is to be called after the update step!
64  */
65  inline void GetStatePosteriori(Vector<double>& state) const
66  { state = posterioState_; }
67 
68  /** Returns the system state covariance. It could be the covariance of the
69  prediction, if called after the predict step or the covariance of the
70  posterior, if GetState is called after the update step.
71  */
72  inline void GetCovariance(Matrix<double>& cov) const
73  { cov = covariance_; }
74 
75  /** Implement this to correlate the actual state with the 'predicted'
76  measurement (h in [1])!
77  It should 'extract' the measurement from the state!
78  */
79  virtual void MeasurementFunction(const Vector<double>& state,
80  Vector<double>& measurePred) = 0;
81 
82  /** Does preditcion (with respect to the control parameters -- u in [1])
83  for a single timestep! (use in alternation with Update(), Update first)
84  */
85  int Predict(const Vector<double>& control);
86 
87  /** Use this function to set the initial state and system-covariance (x(0)
88  and P(0) in [1])!
89  */
90  inline void SetInitial(const Vector<double>& initialState,
91  const Matrix<double>& initialCov)
92  {
93  priorState_ = initialState;
94  covariance_ = initialCov;
95  systemStateDim_ = initialState.size();
96  }
97 
98  /** Same as above for measurement function! The DerivedMeasurement matrix
99  (H in [1]) contains the partial derivates of the measurement-function
100  (h in [1]) with respect to each state komponent (dhi/dxj(i,j)). The
101  matrix DerivedMeasurementError (V in [1]) contains the partial
102  derivatives of h with respect to each measurement-noise-komponent
103  (dhi/dvj(i,j)).
104 
105  These matrices may be changed for every timestep!
106  (according to the 'real' derivatives h)
107  */
108  inline void SetMeasureDerive(const Matrix<double>& derivedMeasurement,
109  const Matrix<double>& derivedMeasurementError)
110  {
111  H_ = derivedMeasurement; HT_ = derivedMeasurement.Transpose();
112  V_ = derivedMeasurementError; VT_ = derivedMeasurementError.Transpose();
113  }
114 
115  /** Set measurement noise covariance -- R in [1]
116  (may be updated for every timestep)!
117  Measurement noise is assumed to be white and normaly distributed.
118  */
119  inline void SetMeasureCov(const Matrix<double>& measureNoiseCov)
120  {R_ = measureNoiseCov;}
121 
122  /** Set the process noise covariance -- Q in [1]
123  (may be updated for every timestep)!
124  Process noise is assumed to be white and normaly distributed.
125  */
126  inline void SetProcessCov(const Matrix<double>& processNoiseCov)
127  {Q_ = processNoiseCov;}
128 
129  /** Set the differentiation-matrices for the EKF (see [1] for details).
130  The DerivedStateTrans matrix (A in [1]) contains the partial deriva-
131  tives of the statetrans.-function (f in [1]) with respect to each
132  state komponent (dfi/dxj(i,j)). The matrix DerivedStateTransError
133  (W in [1]) contains the partial derivatives of f with respect to each
134  process-noise-komponent (dfi/dwj(i,j)).
135 
136  These matrices may be changed for every timestep!
137  (according to the 'real' derivatives of f)
138  */
139  inline void SetStateDerive(const Matrix<double>& derivedStateTrans,
140  const Matrix<double>& derivedStateTransError)
141  {
142  A_ = derivedStateTrans; AT_ = derivedStateTrans.Transpose();
143  W_ = derivedStateTransError; WT_ = derivedStateTransError.Transpose();
144  }
145 
146  /** Implement the statetransition-function (f in [1]) here.
147  It should compute the state x(k) for timestep k from the state x(k-1)
148  for timestep k-1 and the control-params u(k)!
149  */
150  virtual void StateTransition(const Vector<double>& oldState,
151  const Vector<double>& control,
152  Vector<double>& newState) = 0;
153 
154  /** Calculates the update step.
155  (use in alternation with Predict(), Update first)
156  */
157  int Update(const Vector<double>& measurement);
158 
159  protected:
160  /// Contains the prediction (a priori/initial) system estimate
162 
163  /// Contains the corrected (a posteriori) system estimate
165 
166  /// The covariance matrix corresponding to State_
168 
169  /// derived measurement matrix and transposed
171 
172  /// measurement noise cov.
174 
175  /// derived state-trans. matrix and transposed
177 
178  /// system noise cov.
180 
181  /// derived state-trans. error matrix and transposed
183 
184  /// derived measurement error matrix and transposed
186 
187  /// identity matrix
189 
190  /// and finally the Kalman gain
192 
193  /// maybe needed for iterated update
195 
196  /// dimensions of system space and measurement space
197  int systemStateDim_, measurementDim_;
198 
199  /// needed for matrix inversion
201 
202  };
203 
204 
205 
206 } // namespace BIAS
207 
208 
209 #endif // __EXTKALMAN_HH__
SVD svd_
needed for matrix inversion
int systemStateDim_
dimensions of system space and measurement space
Matrix< double > Q_
system noise cov.
Matrix< double > AT_
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
Matrix< double > R_
measurement noise cov.
Matrix< T > Transpose() const
transpose function, storing data destination matrix
Definition: Matrix.hh:823
void SetStateDerive(const Matrix< double > &derivedStateTrans, const Matrix< double > &derivedStateTransError)
Set the differentiation-matrices for the EKF (see [1] for details).
Matrix< double > WT_
Vector< double > lastKalmanUpdate_
maybe needed for iterated update
Matrix< double > K_
and finally the Kalman gain
void GetCovariance(Matrix< double > &cov) const
Returns the system state covariance.
Matrix< double > VT_
void SetProcessCov(const Matrix< double > &processNoiseCov)
Set the process noise covariance – Q in [1] (may be updated for every timestep)! Process noise is ass...
Vector< double > posterioState_
Contains the corrected (a posteriori) system estimate.
Matrix< double > HT_
Vector< double > priorState_
Contains the prediction (a priori/initial) system estimate.
Matrix< double > Identity_
identity matrix
void GetStatePosteriori(Vector< double > &state) const
Returns the corrected system state, which is the update-corrected state.
void SetMeasureCov(const Matrix< double > &measureNoiseCov)
Set measurement noise covariance – R in [1] (may be updated for every timestep)! Measurement noise is...
void SetMeasureDerive(const Matrix< double > &derivedMeasurement, const Matrix< double > &derivedMeasurementError)
Same as above for measurement function! The DerivedMeasurement matrix (H in [1]) contains the partial...
Classical ExtendedKalman-filter (EKF).
void SetInitial(const Vector< double > &initialState, const Matrix< double > &initialCov)
Use this function to set the initial state and system-covariance (x(0) and P(0) in [1])! ...
void GetState(Vector< double > &state) const
Returns the predicted system state.
Subscript size() const
Definition: vec.h:262
Matrix< double > covariance_
The covariance matrix corresponding to State_.