Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IteratedExtendedKalman.cpp
1 #include "IteratedExtendedKalman.hh"
2 
3 using namespace std;
4 using namespace BIAS;
5 
6 IteratedExtendedKalman::IteratedExtendedKalman()
7  : bUseStable_(false), maxIters_(20), iterEps_(1e-4)
8 {
9  //defaults indicating nothing was set
10  systemStateDim_ = 0;
11  processNoiseDim_ = 0;
12 }
13 
15  double iterEps,
16  bool bUseStable)
17  : bUseStable_(bUseStable), maxIters_(maxIters), iterEps_(iterEps)
18 {
19  //defaults indicating no state was set
20  systemStateDim_ = 0;
21  processNoiseDim_ = 0;
22 }
23 
25  const BIAS::Matrix<double>& initialCov,
26  const BIAS::Matrix<double>& processNoiseCov)
27 {
28  statePrior_ = initialState;
29  statePost_ = initialState;
30  state_ = initialState;
31 
32  covariancePrior_ = initialCov;
33  covariancePost_ = initialCov;
34  covariance_ = initialCov;
35 
36  Q_ = processNoiseCov;
37 
38  processNoiseDim_ = processNoiseCov.num_rows();
39  systemStateDim_ = initialState.size();
40 
43 
44  A_.newsize(systemStateDim_, systemStateDim_);
45  W_.newsize(systemStateDim_, processNoiseDim_);
46 }
47 
49 {
50 #ifdef BIAS_DEBUG
51  BIASASSERT(systemStateDim_ != 0);
52  BIASASSERT(processNoiseDim_ != 0);
53 #endif //BIAS_DEBUG
54 
55  //resize jacobians if necessary
56  if (((unsigned int) A_.num_rows() != systemStateDim_) ||
57  ((unsigned int) A_.num_cols() != systemStateDim_))
59  if (((unsigned int) W_.num_rows() != systemStateDim_) ||
60  ((unsigned int) W_.num_cols() != processNoiseDim_))
62 
63  //get predicted state
65  //get prediction jacobians
66  GetTransitionJacobian(state_, control, A_, W_);
67  AT_ = A_.Transpose();
68  WT_ = W_.Transpose();
69 
70 #ifdef BIAS_DEBUG
71  BIASASSERT((unsigned int) A_.num_cols() == systemStateDim_);
72  BIASASSERT(A_.num_cols() == A_.num_rows());
73  BIASASSERT(W_.num_cols() == Q_.num_rows());
74  BIASASSERT((unsigned int) W_.num_rows() == systemStateDim_);
75 #endif //BIAS_DEBUG
76 
77  //project cov. ahead
79 
80  //set actual state
83 }
84 
86  const BIAS::Matrix<double>& measureNoise,
87  const unsigned int index)
88 {
89  const unsigned int measurementDim = measurement.size();
90 
91 #ifdef BIAS_DEBUG
92  BIASASSERT(systemStateDim_ != 0);
93  BIASASSERT(measurementDim != 0);
94  BIASASSERT(measurementDim == (unsigned int)measureNoise.num_cols());
95  BIASASSERT(measurementDim == (unsigned int)measureNoise.num_rows());
96 #endif //BIAS_DEBUG
97 
98  //resize matrices if necessary
99  if (((unsigned int) H_.num_rows() != measurementDim) ||
100  ((unsigned int) H_.num_cols() != systemStateDim_))
101  H_.newsize(measurementDim,systemStateDim_);
102  if (((unsigned int) V_.num_rows() != measurementDim)||
103  (V_.num_cols() != measureNoise.num_rows()))
104  V_.newsize(measurementDim,measureNoise.num_rows());
105  if ((unsigned int) Identity_.num_cols() != systemStateDim_) {
108  }
109 
110  //temporary vars for update iteration...
111  //copy state for iteration
112  BIAS::Vector<double> state_iter(state_);
113  //predicted measurement
114  BIAS::Vector<double> measurePred(measurementDim);
115  //difference of iterated and start state
117  //update for single iteration
119  //kalman gain matrix
120  BIAS::Matrix<double> K(systemStateDim_, measurementDim);
121  //inverse of (H_*covariance_*HT_ + V_*measureNoise*VT_)
122  BIAS::Matrix<double> invTmp(measurementDim, measurementDim);
123 
124  //cout << "iteration: ";
125 
126  for (unsigned int i=0; i<maxIters_; i++) {
127  delta_state = state_ - state_iter;
128 
129  //get jacobians for observation model
130  GetMeasureJacobian(state_iter, H_, V_, index);
131  HT_ = H_.Transpose();
132  VT_ = V_.Transpose();
133 
134  //create matrix for inversion of (H_*covariance_*HT_ + V_*measureNoise*VT_)
135  invTmp = H_ * covariance_ * HT_ + V_ * measureNoise * VT_;
136  invTmp = svd_.Invert(invTmp);
137  //compute Kalman gain
138  K = covariance_ * HT_ * invTmp;
139 
140  //update iteration state estimate...
141  //call virtual measurement-function
142  MeasurementPrediction(state_iter, measurePred, index);
143  //correct iteration state with measurement
144  update = delta_state + K*(measurement - measurePred - H_*delta_state);
145  state_iter = state_iter + update;
146 
147  //cout << i << " ";
148 
149  //stop iterating if change is sufficient small
150  if (update.NormL2() < iterEps_) break;
151  }
152 
153  //cout << endl;
154 
155  //update error-covariance
156  if (bUseStable_) {
157  BIAS::Matrix<double> IKH = (Identity_ - K * H_);
158  covariance_ = IKH * covariance_ * IKH.Transpose()
159  + K*measureNoise*K.Transpose();
160  } else {
161  covariance_ = (Identity_ - K * H_) * covariance_;
162  }
163 
164  state_ = state_iter;
165  statePost_ = state_;
167 }
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
unsigned int maxIters_
number of maximal iterations for iterated update
Subscript num_cols() const
Definition: cmat.h:320
unsigned int processNoiseDim_
dimension of process noise
IteratedExtendedKalman()
Standard constructor.
void Update(const BIAS::Vector< double > &measurement, const BIAS::Matrix< double > &measureNoise, const unsigned int index)
Implememts the iterative update step using measurement and its covariance.
Matrix< T > Transpose() const
transpose function, storing data destination matrix
Definition: Matrix.hh:823
BIAS::Matrix< double > A_
derived state-trans. matrix and transposed
BIAS::Vector< double > state_
contains the actual system estimate
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
BIAS::Matrix< double > covariancePost_
the covariance matrix corresponding to statePost_
BIAS::Matrix< double > covariancePrior_
the covariance matrix corresponding to statePrior_
void Init(const BIAS::Vector< double > &initialState, const BIAS::Matrix< double > &initialCov, const BIAS::Matrix< double > &processNoiseCov)
Initialize the kalman filter before using update/predict.
void Predict(const BIAS::Vector< double > &control)
Implements a single preditcion step (with respect to the control parameters – u[k] in [1])...
BIAS::Matrix< double > W_
state-trans. error jacobian and transposed
BIAS::Matrix< double > V_
measurement error jacobian for measurement model and its transpose
BIAS::Vector< double > statePrior_
contains the last prediction (a priori/initial) system estimate
void SetIdentity()
Converts matrix to identity matrix.
Definition: Matrix.cpp:383
BIAS::Matrix< double > Identity_
identity matrix frequently used in Update
unsigned int systemStateDim_
dimension of state space
BIAS::Matrix< double > covariance_
the covariance matrix corresponding to state_
Matrix< double > Invert()
returns pseudoinverse of A = U * S * V^T A^+ = V * S^+ * U^T
Definition: SVD.cpp:214
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 ...
Subscript size() const
Definition: vec.h:262
BIAS::Matrix< double > H_
measurement jacobian for measurement model and its transpose
BIAS::Matrix< double > Q_
system noise cov.