5 #include "filterModel.h" 16 template<
class VECTOR,
class MATRIX,
class T1>
23 VECTOR currentEstimate;
24 MATRIX currentCovariance;
29 return model->getStateCount();
33 return model->getSensorCount();
36 MATRIX getTransitionJacobian(VECTOR v, T1 t){
37 return model->transitionJacobian(v,t);
40 MATRIX getMeasurementJacobian(VECTOR v, T1 t){
41 return model->measurementJacobian(v,t);
44 VECTOR transition(VECTOR t, T1 time){
45 return model->transition(t,time);
48 VECTOR measure(VECTOR t, T1 time){
49 return model->measurement(t,time);
53 void setCurrentCovariance(MATRIX t){
54 currentCovariance = t;
57 void setCurrentTime(T1 t){
61 void setCurrentEstimate(VECTOR t){
65 MATRIX getProcessNoiseCovariance(VECTOR v, T1 t){
66 return model->getProcessNoiseCovariance(v,t);
69 MATRIX getSensorNoiseCovariance(VECTOR v, T1 t){
70 return model->getSensorNoiseCovariance(v,t);
73 MATRIX getProcessNoiseCovarianceSqrt(VECTOR v, T1 t){
74 return model->getProcessNoiseCovarianceSqrt(v,t);
77 MATRIX getSensorNoiseCovarianceSqrt(VECTOR v, T1 t){
78 return model->getSensorNoiseCovarianceSqrt(v,t);
83 MATRIX getCurrentCovariance(){
84 return currentCovariance;
87 VECTOR getCurrentEstimate(){
88 return currentEstimate;
97 VECTOR initialEstimate,
98 MATRIX initialCovariance,
101 currentTime = initialTime;
102 currentEstimate = initialEstimate;
103 currentCovariance = initialCovariance;
106 virtual void predict(T1 timeUnitsForward) = 0;
107 virtual void update(VECTOR measurement) = 0;
114 template<
class VECTOR,
class MATRIX>
121 VECTOR initialEstimate,
122 MATRIX initialCovariance,
124 KF::KalmanFilter(initialTime,
125 initialEstimate,initialCovariance,model){}
127 void predict(
int timeUnitsForward){
128 VECTOR est = KF::getCurrentEstimate();
129 int time = KF::getCurrentTime();
130 MATRIX jacob = KF::getTransitionJacobian(est,KF::getCurrentTime());
131 MATRIX covariance = KF::getCurrentCovariance();
133 KF::setCurrentEstimate(KF::transition(est,time));
134 KF::setCurrentCovariance(jacob*covariance*jacob.transpose() + KF::getProcessNoiseCovariance(est,time));
135 KF::setCurrentTime(time + 1);
138 void update(VECTOR measurement){
139 VECTOR est = KF::getCurrentEstimate();
140 MATRIX covariance = KF::getCurrentCovariance();
141 int time = KF::getCurrentTime();
143 MATRIX measurementJacobian = KF::getMeasurementJacobian(est,time);
144 MATRIX Sk = KF::getSensorNoiseCovariance(est,time) + measurementJacobian*covariance*measurementJacobian.transpose();
145 MATRIX KalmanGain = covariance*measurementJacobian.transpose()*Sk.inverse();
146 KF::setCurrentEstimate(est + KalmanGain*(measurement - KF::measure(est,time)));
147 KF::setCurrentCovariance((MATRIX::Identity(KF::getStateCount(),KF::getStateCount())-KalmanGain*measurementJacobian)*covariance);
152 template<
class VECTOR,
class MATRIX>
157 MATRIX getCurrentCovariance(){
158 return currentCovarianceSQRT*currentCovarianceSQRT.transpose();
162 MATRIX currentCovarianceSQRT;
164 MATRIX getCurrentCovarianceSQRT(){
165 return currentCovarianceSQRT;
171 VECTOR initialEstimate,
172 MATRIX initialCovariance,
174 KF::KalmanFilter(initialTime,
175 initialEstimate,initialCovariance,model){
176 currentCovarianceSQRT = initialCovariance;
177 MATRIX::lowerTriangulate(currentCovarianceSQRT);
180 void predict(
int timeUnitsForward){
181 int stateCount = KF::getStateCount();
182 int sensorCount = KF::getSensorCount();
183 VECTOR est = KF::getCurrentEstimate();
184 int time = KF::getCurrentTime();
185 MATRIX jacob = KF::getTransitionJacobian(est,KF::getCurrentTime());
186 MATRIX covarianceSQRT = getCurrentCovarianceSQRT();
188 MATRIX tmp(stateCount,stateCount*2);
189 tmp.block(0,0,stateCount,stateCount) = jacob*covarianceSQRT;
190 tmp.block(0,stateCount,stateCount,stateCount) = KF::getProcessNoiseCovarianceSqrt(est,time);
192 MATRIX::lowerTriangulate(tmp);
194 KF::setCurrentEstimate(KF::transition(est,time));
195 currentCovarianceSQRT = tmp.block(0,0,stateCount,stateCount);
196 KF::setCurrentTime(time + 1);
199 void update(VECTOR measurement){
200 int stateCount = KF::getStateCount();
201 int sensorCount = KF::getSensorCount();
202 VECTOR est = KF::getCurrentEstimate();
203 MATRIX covariance = KF::getCurrentCovariance();
204 int time = KF::getCurrentTime();
205 MATRIX measurementJacobian = KF::getMeasurementJacobian(est,time);
207 MATRIX tmp(stateCount + sensorCount, stateCount + sensorCount);
208 tmp.block(0,0,sensorCount,sensorCount) = KF::getSensorNoiseCovarianceSqrt(est,time);
209 tmp.block(0,sensorCount,sensorCount,stateCount) = measurementJacobian*currentCovarianceSQRT;
210 tmp.block(sensorCount,sensorCount,stateCount,stateCount) = currentCovarianceSQRT;
212 MATRIX::lowerTriangulate(tmp);
213 MATRIX tmp2 = tmp.block(0,0,sensorCount,sensorCount);
214 VECTOR ek = VECTOR::solve(tmp2,(measurement - KF::measure(est,time)));
215 KF::setCurrentEstimate(est+tmp.block(sensorCount,0,stateCount,sensorCount)*ek);
216 currentCovarianceSQRT = tmp.block(sensorCount,sensorCount,stateCount,stateCount);
Definition: filterModel.h:7
Definition: KalmanFilter.h:17
Definition: KalmanFilter.h:153
Definition: KalmanFilter.h:115