KalmanFilter
filterModel.h
1 #ifndef FILTERMODEL
2 #define FILTERMODEL
3 #include "model.h"
4 #include <random>
5 
6 template<class VECTOR, class MATRIX, class T1>
7 class filterModel{
8  public:
9  virtual VECTOR transition(VECTOR t, T1 timeUnit) = 0;
10  virtual MATRIX transitionJacobian(VECTOR t, T1 timeUnit) = 0;
11  virtual VECTOR measurement(VECTOR t, T1 timeUnit) = 0;
12  virtual MATRIX measurementJacobian(VECTOR t, T1 timeUnit) = 0;
13  virtual MATRIX getProcessNoiseCovariance(VECTOR t,T1 time) = 0;
14  virtual MATRIX getSensorNoiseCovariance(VECTOR t,T1 time) = 0;
15  virtual MATRIX getProcessNoiseCovarianceSqrt(VECTOR t,T1 time) = 0;
16  virtual MATRIX getSensorNoiseCovarianceSqrt(VECTOR t,T1 time) = 0;
17  virtual int getSensorCount() = 0;
18  virtual int getStateCount() = 0;
19 };
20 
21 template<class VECTOR,class MATRIX>
22 class discreteDiscreteFilterModel: public filterModel<VECTOR,MATRIX,int>{
23  private:
24  discreteModel<VECTOR> *transitionModel;
25  jacobianDiscrete<VECTOR,MATRIX> * transitionJac;
27  discreteModel<VECTOR> *measurementModel;
28  jacobianDiscrete<VECTOR,MATRIX> * measurementJac;
30  int stateCount;
31  int sensorCount;
32 
33  public:
34 
36  discreteModel<VECTOR> *measurementModel,
39  jacobianDiscrete<VECTOR,MATRIX>* transitionJac,
40  jacobianDiscrete<VECTOR,MATRIX>* measurementJac,
41  int stateCount,
42  int sensorCount):
43  transitionModel(transitionModel),measurementModel(measurementModel),
44  processNoise(processNoise),sensorNoise(sensorNoise),
45  transitionJac(transitionJac),measurementJac(measurementJac),
46  stateCount(stateCount),sensorCount(sensorCount){}
47 
48  int getStateCount(){
49  return stateCount;
50  }
51 
52  int getSensorCount(){
53  return sensorCount;
54  }
55 
56  VECTOR transition(VECTOR t, int time){
57  return transitionModel->function(t,time);
58  }
59 
60  MATRIX transitionJacobian(VECTOR t, int time){
61  return transitionJac->function(t,time);
62  }
63 
64  VECTOR measurement(VECTOR t, int time){
65  return measurementModel->function(t,time);
66  }
67 
68  MATRIX measurementJacobian(VECTOR t, int time){
69  return measurementJac->function(t,time);
70  }
71 
72  MATRIX getProcessNoiseCovariance(VECTOR t, int time){
73  return processNoise->function(t,time);
74  }
75 
76 
77  MATRIX getProcessNoiseCovarianceSqrt(VECTOR t, int time){
78  return processNoise->sqrt(t,time);
79  }
80 
81  MATRIX getSensorNoiseCovariance(VECTOR t, int time){
82  return sensorNoise->function(t,time);
83  }
84 
85  MATRIX getSensorNoiseCovarianceSqrt(VECTOR t, int time){
86  return sensorNoise->sqrt(t,time);
87  }
88 
89 };
90 
91 
92 
93 
94 template<class VECTOR,class MATRIX>
96  private:
98  VECTOR currentState;
99  VECTOR measurement;
100  int time;
101 
102  VECTOR * solvedMeasurements;
103  VECTOR * solvedStates;
104  bool usedMeasurementsPointer;
105  bool usedStatesPointer;
106  uint32_t seed;
107 
108 
109  public:
111  solvedMeasurements = NULL;
112  solvedStates = NULL;
113  usedMeasurementsPointer = true;
114  usedStatesPointer = true;
115  }
116 
117  discreteDiscreteFilterSolver(discreteDiscreteFilterModel<VECTOR,MATRIX> * model,int initialTime, VECTOR initialCondition)
118  :model(model),time(initialTime),currentState(initialCondition){
119  uint32_t seed = std::random_device()();
120  solvedMeasurements = NULL;
121  solvedStates = NULL;
122  usedMeasurementsPointer = true;
123  usedStatesPointer = true;
124  }
125 
126  VECTOR * getSolvedMeasurements(){
127  usedMeasurementsPointer = true;//users responsibility to delete now
128  return solvedMeasurements;
129  }
130 
131  VECTOR * getSolvedStates(){
132  usedStatesPointer = true;
133  return solvedStates;
134  }
135 
136  void setSeed(uint32_t s){
137  seed = s;
138  }
139 
140  uint32_t getSeed(){
141  return seed;
142  }
143 
144  VECTOR * solve(int steps){
145  int stateCount = model->getStateCount();
146  int sensorCount = model->getSensorCount();
147  if(usedMeasurementsPointer==false){
148  delete [] solvedMeasurements;//TODO mention it is users responsibility to delete
149  }
150  if(usedStatesPointer==false){
151  delete [] solvedStates;
152  }
153  usedMeasurementsPointer = false;
154  usedStatesPointer = false;
155 
156  solvedMeasurements = new VECTOR[steps];
157 
158  solvedStates = new VECTOR[steps];
159 
160 
161  for(int i = 0; i < steps; i++){
162  currentState = model->transition(currentState,time + i);
163  currentState = currentState +
164  model->getProcessNoiseCovarianceSqrt(currentState,time + i)*VECTOR::randomVector(stateCount,seed+i);
165  measurement = model->measurement(currentState,time + i);
166  measurement = measurement +
167  model->getSensorNoiseCovarianceSqrt(currentState,time)*VECTOR::randomVector(sensorCount,seed-i);
168 
169  solvedStates[i] = currentState;
170  solvedMeasurements[i] = measurement;
171  }
172 
173  }
174 };
175 #endif
Definition: filterModel.h:7
Definition: model.h:118
Definition: model.h:148
virtual T function(const T &val, const int index) const =0
virtual MATRIX function(const VECTOR &val, int t)=0
Pure virtual method returning Jacobian of some function.
Definition: filterModel.h:22
virtual MATRIX function(const VECTOR &val, int t)=0
Pure virtual method returning Jacobian of some function.
Definition: filterModel.h:95
Definition: model.h:14