39 lambda = alpha*alpha * L - L;
40 lambda2 = 1 - alpha*alpha + beta;
42 state = cvCreateMat(state_n, 1, CV_64F); cvSetZero(
state);
45 stateD = cvCreateMat(state_n, state_n, CV_64F); cvSetZero(
stateD);
46 stateU = cvCreateMat(state_n, state_n, CV_64F); cvSetZero(
stateU);
47 stateV = cvCreateMat(state_n, state_n, CV_64F); cvSetZero(
stateV);
63 for (
int i = 0; i <
sigma_n; i++) {
89 for (
int i = 0; i <
sigma_n; i++) {
104 printf(
"stateCovariance:\n");
105 for (
int i = 0; i < 5; i++)
106 printf(
"%+.10f %+.10f %+.10f %+.10f %+.10f\n",
118 double scale = L +
lambda;
119 for (
int i = 0; i <
state_n; i++) {
120 double d = cvGetReal2D(
stateD, i, i);
121 cvSetReal2D(
stateD, i, i, sqrt(scale*d));
126 printf(
"sqrtStateCovariance:\n");
127 for (
int i = 0; i < 5; i++)
128 printf(
"%+.10f %+.10f %+.10f %+.10f %+.10f\n",
135 printf(
"sqrtStateCovariance^2:\n");
136 for (
int i = 0; i < 5; i++)
137 printf(
"%+.10f %+.10f %+.10f %+.10f %+.10f\n",
148 for (
int i = 0; i <
state_n; i++) {
164 double totalWeight = 0;
165 for (
int i = 0; i <
sigma_n; i++) {
166 double weight = i == 0
169 totalWeight += weight;
171 for (
int i = 0; i <
sigma_n; i++) {
173 process_model->
f(sigma);
174 double weight = i == 0
177 double scale = weight / totalWeight;
178 cvAddWeighted(sigma, scale,
state, 1., 0.,
state);
184 for (
int i = 0; i <
sigma_n; i++) {
185 double weight = i == 0
188 totalWeight += weight;
190 for (
int i = 0; i <
sigma_n; i++) {
191 double weight = i == 0
194 double scale = weight / totalWeight;
205 printf(
"predicted state: ");
206 for (
int i = 0; i <
state_n; i++) printf(
"%f ", cvGetReal1D(
state, i));
208 printf(
"predicted stateCovariance:\n");
209 for (
int i = 0; i <
state_n; i++) {
221 int obs_n = innovation->rows;
222 if (obs_n > this->obs_n) {
223 printf(
"Observation exceeds maximum size!\n");
228 CvMat
predObs = cvMat(obs_n, 1, CV_64F, this->predObs->data.db);
230 for (
int i = 0; i <
sigma_n; i++) {
231 CvMat sigma_h = cvMat(obs_n, 1, CV_64F,
sigma_predObs[i]->data.db);
232 double scale = i == 0
236 cvAddWeighted(&sigma_h, scale, &predObs, 1., 0., &predObs);
241 this->predObsCovariance->data.db);
243 this->statePredObsCrossCorrelation->data.db);
244 CvMat
predObsDiff = cvMat(obs_n, 1, CV_64F, this->predObsDiff->data.db);
245 cvSetZero(&predObsCovariance);
246 cvSetZero(&statePredObsCrossCorrelation);
247 for (
int i = 0; i <
sigma_n; i++) {
248 CvMat sigma_h = cvMat(obs_n, 1, CV_64F,
sigma_predObs[i]->data.db);
249 double scale = i == 0
253 cvSub(&sigma_h, &predObs, &predObsDiff);
254 cvGEMM(&predObsDiff, &predObsDiff, scale, &predObsCovariance, 1., &predObsCovariance,
256 cvGEMM(
stateDiff, &predObsDiff, scale, &statePredObsCrossCorrelation, 1.,
257 &statePredObsCrossCorrelation, CV_GEMM_B_T);
262 if (noise) cvAdd(&predObsCovariance, noise, &predObsCovariance);
265 printf(
"real observation: ");
266 for (
int i = 0; i <
obs_n; i++) printf(
"%+f ", cvGetReal1D(innovation ,i));
268 printf(
"predicted observation: ");
269 for (
int i = 0; i <
obs_n; i++) printf(
"%+f ", cvGetReal1D(&predObs,i));
271 printf(
"predicted observation co-variance\n");
272 for (
int i = 0; i <
obs_n; i++) {
273 for (
int j = 0; j <
obs_n; j++) printf(
"%+f ", cvGetReal2D(&predObsCovariance,i,j));
276 printf(
"state observation cross-correlation\n");
277 for (
int i = 0; i <
state_n; i++) {
278 for (
int j = 0; j <
obs_n; j++) printf(
"%+f ", cvGetReal2D(&statePredObsCrossCorrelation,i,j));
290 this->invPredObsCovariance->data.db);
291 CvMat
kalmanGain = cvMat(state_n, obs_n, CV_64F, this->kalmanGain->data.db);
292 CvMat
kalmanTmp = cvMat(state_n, obs_n, CV_64F, this->kalmanTmp->data.db);
294 cvSub(innovation, &predObs, innovation);
301 printf(
"innovation: ");
302 for (
int i = 0; i <
obs_n; i++) printf(
"%f ", cvGetReal1D(innovation,i));
304 double inn_norm = cvNorm(innovation);
305 printf(
"innivation norm: %f\n", inn_norm);
308 cvInvert(&predObsCovariance, &invPredObsCovariance, CV_SVD_SYM);
309 cvMatMul(&statePredObsCrossCorrelation, &invPredObsCovariance, &kalmanGain);
310 cvGEMM(&kalmanGain, innovation, 1.,
state, 1.,
state);
311 cvMatMul(&kalmanGain, &predObsCovariance, &kalmanTmp);
315 printf(
"estimated state: ");
316 for (
int i = 0; i <
state_n; i++) printf(
"%f ", cvGetReal1D(
state, i));
CvMat * predObsCovariance
This file implements an unscented Kalman filter.
CvMat * statePredObsCrossCorrelation
void update(UnscentedObservation *observation)
Updates the state by an observation.
virtual void h(CvMat *z, CvMat *state)=0
observation model: z = h(state)
virtual CvMat * getObservationNoise()=0
Returns the observation noise covariance matrix.
CvMat * sqrtStateCovariance
virtual void f(CvMat *state)=0
process model: state+1 = f(state)
UnscentedKalman(int state_n, int obs_n, int state_k=0, double alpha=0.001, double beta=2.0)
Initializes Unscented Kalman filter.
void initialize()
(Re-)initialize UKF internal state.
void predict(UnscentedProcess *process_model)
Updated the state by predicting.
Observation model for an unscented kalman filter.
CvMat * invPredObsCovariance
virtual CvMat * getProcessNoise()=0
Returns the process noise covariance.
virtual CvMat * getObservation()=0
Returns the current measurement vector.
Process model for an unscented kalman filter.