ACT-CV - Machine Vision for Cognitive Modeling
KalmanTest.h
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2 
3 /*
4 ACT-CV - Machine Vision for Cognitive Modeling
5 Copyright (c) 2008 Marc Halbruegge
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
22 
27 #ifndef KALMANTEST_H_
28 #define KALMANTEST_H_
29 
30 #include <iostream>
31 #include <general/OpenCVKalman.h>
32 #include "TestCase.h"
33 
34 namespace tests {
35 
40 class KalmanTest : public TestCase {
41 public:
42  const char* GetName() const {
43  return "KalmanTest";
44  }
45 
46  void Run() {
47 
48  KalmanFilter<1> kalman;
49 
50  for (int runs=0;runs < 100; runs++) {
51  float x = (rand() % 1001) - 500;
52  //std::cout << x << "\t";
53 
54  kalman.InitRandom(x, 1000.0, 1e-0, 1e-2);
55  float state[2];
56  float var[2];
57 
58  kalman.GetCorrectedState(state);
59  kalman.GetVariance(var);
60 
61  AssertEqual(state[0], x, __FILE__, __LINE__);
62  AssertEqual(state[1], 0, __FILE__, __LINE__);
63  AssertEqual(var[0], 1000, __FILE__, __LINE__);
64  AssertEqual(var[1], 1000, __FILE__, __LINE__);
65 
66  float trueVel = (rand() % 21) - 10;
67  //std::cout << trueVel << "\t";
68 
69  for (int i=1;i<100;i++) {
70  state[0]=trueVel*i+x;
71  state[1]=0;
72 
73  // test cvKalmanCorrect
74  kalman.AddMeasure(state);
75  kalman.GetCorrectedState(state);
76  kalman.GetVariance(var);
77 
78  float zPos = (state[0]-(x+trueVel*i)) / sqrt(var[0]);
79  float zVel = (state[1]-trueVel) / sqrt(var[1]);
80  //std::cout << zPos << " " << zVel << std::endl;
81 
82  if (i>0) {
83  AssertBelow(fabs(zPos), 1, __FILE__, __LINE__);
84  AssertBelow(fabs(zVel), 1, __FILE__, __LINE__);
85  }
86 
87  // test cvKalmanPredict
88  kalman.Predict(state);
89  kalman.GetVariance(var);
90 
91  zPos = (state[0]-(x+trueVel*(i+1))) / sqrt(var[0]);
92  zVel = (state[1]-trueVel) / sqrt(var[1]);
93  //std::cout << zPos << " " << zVel << std::endl;
94 
95  if (i>1) {
96  AssertBelow(fabs(zPos), 1, __FILE__, __LINE__);
97  AssertBelow(fabs(zVel), 1, __FILE__, __LINE__);
98  }
99 
100 #if 0
101  std::cout
102  << x << " "
103  << state[0] << " "
104  << state[1] << " "
105  << std::endl;
106  std::cout
107  << var[0] << " "
108  << var[1] << " "
109  << std::endl;
110 #endif
111  }
112  }
113  }
114 };
115 
116 }; // end of namespace tests
117 
118 #endif /*KALMANTEST_H_*/
119 


ACT-CV - Machine Vision for Cognitive Modeling
© 2015 Marc Halbruegge (actcvlibrary@googlemail.com)