Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleKalman.cpp
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 /** @example ExampleKalman.cpp
25  @relates Kalman
26  @ingroup g_examples
27  @ingroup g_stateestimation
28  @brief simple example for kalman filter usage
29  @author woelk 04/2006 */
30 
31 #include <Base/Math/Random.hh>
32 #include <StateEstimator/Kalman.hh>
33 
34 using namespace BIAS;
35 using namespace std;
36 
37 const double meas_noise = 2.0;
38 const double process_noise = 1.0;
39 
40 /** computes the next ground truth system state */
41 Vector<double> Process(const Matrix<double>& A, const Vector<double>& state,
42  const Matrix<double>& process_covariance)
43 {
44  Vector<double> res;
45  res = A * state;
46  const int dim = res.Size();
47  Vector<double> noise(dim);
48  static Random random;
49  for (int i=0; i<dim; i++){
50  noise[i] =
51  random.GetNormalDistributed(0.0, sqrt(process_covariance[i][i]));
52  }
53  return res + noise;
54 }
55 
56 /** computes noisy measuremenst using the ground truth system state */
57 void GenerateNoisyMeasurements(const Vector<double>& state,
58  const Matrix<double>& H,
59  Vector<double>& meas,
60  Matrix<double>& meas_cov)
61 {
62  Vector<double> gt_meas = H * state;
63  static Random random;
64  const int meas_dim = meas.Size();
65  for (int r=0; r<meas_dim; r++){
66  double std_dev = random.GetUniformDistributed(0.5, meas_noise*3);
67  meas_cov[r][r] = std_dev * std_dev;
68  meas[r] = gt_meas[r] +
69  random.GetNormalDistributed(0.0, std_dev);
70  //cout << "meas gt: "<<gt_meas[r]<<" noisy: "<<meas[r]<<endl;
71  }
72 
73 }
74 
75 int main(int /*argc*/, char ** /*argv*/ )
76 {
77  const int state_dim = 2;
78  const int meas_dim = 2;
79  // the system matrix
80  const Matrix<double> A(state_dim, state_dim, "1.1 0.1 -0.2 1.1");
81  // the measurement matrix measurement = H * system_state
82  const Matrix<double> H(meas_dim, state_dim, "1.0 0.0 0.0 1.0");
83  //cout << "H: "<<H<<endl;
84  //cout << "A: "<<A<<endl;
85 
86  Random random;
87  Vector<double> pred(state_dim), meas(meas_dim), update(state_dim),
88  state(state_dim);
89  Matrix<double> pred_cov(state_dim, state_dim),
90  meas_cov(meas_dim, meas_dim),
91  update_cov(state_dim, state_dim),
92  process_covariance(state_dim, state_dim);
93 
94  // fill process_covariance randomly
95  double std_dev;
96  for (int r=0; r<state_dim; r++){
97  std_dev = random.GetUniformDistributed(0.5, process_noise);
98  process_covariance[r][r] = std_dev * std_dev;
99  }
100 
101  // initialize the system state and covariance matrix
102  pred_cov.SetZero();
103  for (int r=0; r<state_dim; r++){
104  state[r] = random.GetUniformDistributed(-5.0, 5.0);
105  std_dev = random.GetUniformDistributed(0.5, meas_noise);
106  pred_cov[r][r] = std_dev * std_dev;
107  pred[r] = state[r] +
108  random.GetNormalDistributed(0.0, std_dev);
109  }
110  cout << "# ground truth system state: " << state
111  << "\n# intial system state guess: " << pred
112  << "\n# covariance matrix of initial system state guess: "
113  <<pred_cov<<endl;
114 
115  Kalman kafi;
116  kafi.Init(A, H, pred, pred_cov);
117 
118  const int num_cycles=100;
119  cout <<"# time\tstate\t\tpred\t\tpred_cov\tmeas\t\tmeas-cov\tup\t\tup-cov\n";
120  for (int i=0; i<num_cycles; i++){
121  // predict the ground truth
122  state = Process(A, state, process_covariance);
123 
124  // predict the estimator
125  kafi.Predict(process_covariance);
126  kafi.GetState(pred);
127  kafi.GetCovariance(pred_cov);
128 
129  // generate some synthetic measurements
130  GenerateNoisyMeasurements(state, H, meas, meas_cov);
131 
132  // update
133  kafi.Update(meas, meas_cov);
134  kafi.GetState(update);
135  kafi.GetCovariance(update_cov);
136 
137  cout <<i;
138  for (int d=0; d<state_dim; d++){
139  cout <<"\t"<<state[d] << "\t" << pred[d]
140  <<"\t"<<sqrt(pred_cov[d][d])<< "\t";
141  if (d<meas_dim){
142  cout << meas[d] << "\t" << sqrt(meas_cov[d][d]) << "\t" ;
143  } else {
144  cout << "nan" << "\t" << "nan" << "\t";
145  }
146  cout << update[d] << "\t"
147  << sqrt(update_cov[d][d]);
148  }
149  cout << endl;
150  }
151 
152  return 0;
153 }
void Init(const Matrix< double > &SystemMatrix, const Matrix< double > &MeasurementMatrix, const Vector< double > &InitialSystemState, const Matrix< double > &InitialSystemCovariance)
Definition: Kalman.cpp:27
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
Classical Kalman filter.
Definition: Kalman.hh:54
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
void Update(const Vector< double > &Measurement, const Matrix< double > &MeasurementCovariance)
Calculates the update step.
Definition: Kalman.cpp:72
void GetState(Vector< double > &state) const
Returns the system state.
Definition: Kalman.hh:89
void Predict(const Matrix< double > &ProcessCovariance)
Calculates the prediction using the system matrix A.
Definition: Kalman.cpp:51
void GetCovariance(Matrix< double > &cov) const
Returns the system state covariance.
Definition: Kalman.hh:96
double GetNormalDistributed(const double mean, const double sigma)
on succesive calls return normal distributed random variable with mean and standard deviation sigma ...
Definition: Random.hh:71
class for producing random numbers from different distributions
Definition: Random.hh:51