Shark machine learning library
About Shark
News!
Contribute
Credits and copyright
Downloads
Getting Started
Installation
Using the docs
Documentation
Tutorials
Class list
Global functions
FAQ
Showroom
Main Page
Related Pages
Modules
Classes
src
Models
KalmanFilter.cpp
Go to the documentation of this file.
1
//===========================================================================
2
/*!
3
* \file KalmanFilter.cpp
4
*
5
* \brief KalmanFilter
6
*
7
* \author O.Krause
8
* \date 2010-2011
9
*
10
* \par Copyright (c) 1998-2007:
11
* Institut für Neuroinformatik<BR>
12
* Ruhr-Universität Bochum<BR>
13
* D-44780 Bochum, Germany<BR>
14
* Phone: +49-234-32-25558<BR>
15
* Fax: +49-234-32-14209<BR>
16
* eMail: Shark-admin@neuroinformatik.ruhr-uni-bochum.de<BR>
17
* www: http://www.neuroinformatik.ruhr-uni-bochum.de<BR>
18
* <BR>
19
*
20
*
21
* <BR><HR>
22
* This file is part of Shark. This library is free software;
23
* you can redistribute it and/or modify it under the terms of the
24
* GNU General Public License as published by the Free Software
25
* Foundation; either version 3, or (at your option) any later version.
26
*
27
* This library is distributed in the hope that it will be useful,
28
* but WITHOUT ANY WARRANTY; without even the implied warranty of
29
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
* GNU General Public License for more details.
31
*
32
* You should have received a copy of the GNU General Public License
33
* along with this library; if not, see <http://www.gnu.org/licenses/>.
34
*
35
*/
36
//===========================================================================
37
#include <
shark/LinAlg/Inverse.h
>
38
#include <
shark/Models/KalmanFilter.h
>
39
40
using namespace
shark;
41
42
KalmanFilter::KalmanFilter
(
size_t
modelSize){
43
m_inputSize
=modelSize;
44
}
45
46
void
KalmanFilter::eval
(
const
RealVector& pattern,RealVector& output){
47
predict
();
48
updateEstimate
(pattern);
49
output =
m_state
;
50
}
51
void
KalmanFilter::setStateVector
(
const
RealVector& stateVec){
52
m_predictedState
= stateVec;
53
m_state
= stateVec;
54
}
55
56
void
KalmanFilter::setStateErrorCov
(
const
RealMatrix& state_err_cov){
57
m_predictedCovariance
= state_err_cov;
58
m_observedCovariance
= state_err_cov;
59
}
60
61
void
KalmanFilter::predict
(){
62
//------------------------------------------------------------
63
// prediction of state vector and estimation error covariance
64
// parameter(t-1) --> x_a(t)
65
// P_p(t-1) --> P_a(t)
66
m_predictedState
=
prod
(
m_stateTransition
,
m_state
);
// x_a(t) = F*parameter(t-1)
67
RealMatrix P_temp=
prod
(
m_observedCovariance
,
trans
(
m_stateTransition
));
68
m_predictedCovariance
=
prod
(
m_stateTransition
, P_temp) +
m_stateNoiseCov
;
// P_a(t) = F*P_p(t-1)*F' + Q
69
}
70
71
72
void
KalmanFilter::updateEstimate
(
const
RealVector& z){
73
// error in predicted measurement
74
m_predictionError
= z -
prod
(
m_observationModel
,
m_predictedState
);
75
76
//------------------------------------------------------------
77
// calculation of K(t) = P_a*H'*[H*P_a(t)*H' + R ]^(-1) "kalman gain"
78
//
79
RealMatrix KT1_temp=
prod
(
m_predictedCovariance
,
trans
(
m_observationModel
));
80
RealMatrix Kt1 =
prod
(
m_observationModel
, KT1_temp) +
m_observationNoise
;
81
RealMatrix K =
prod
(KT1_temp,
invert
(Kt1));
82
83
// a postiori state estimate
84
m_state
+=
prod
(K,
m_predictionError
);
85
86
//------------------------------------------------------------
87
// calculation of P_p(t) = (I-K(t)*H)P_a(t)*(I-K(t)*H)' + K(t)*R*K(t)'
88
//
89
// calculation of I-K(t)*H
90
RealMatrix ImKH = RealIdentity(
m_inputSize
)-
prod
(K,
m_observationModel
);
91
92
RealMatrix P_temp1=
prod
(
m_predictedCovariance
,
trans
(ImKH));
93
RealMatrix R_Kt=
prod
(
m_observationNoise
,
trans
(K));
94
m_observedCovariance
=
prod
(ImKH, P_temp1) +
prod
(
trans
(K), R_Kt);
95
96
// in case that 'newMeasurement' is called repeatedly in one time step
97
// (i.e. 'predict()' is not called between 'newMeasurement' calls).
98
m_predictedState
=
m_state
;
99
m_predictedCovariance
=
m_observedCovariance
;
100
}
101