-
Notifications
You must be signed in to change notification settings - Fork 11
/
kftest.cpp
86 lines (64 loc) · 2.27 KB
/
kftest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
@file kftest.cpp
@brief
@date 2010
@author
Ryan Pavlik
<[email protected]> and <[email protected]>
http://academic.cleardefinition.com/
Iowa State University Virtual Reality Applications Center
Human-Computer Interaction Graduate Program
*/
// Internal Includes
#include "generateData.h"
// Library/third-party includes
#include <eigenkf/KalmanFilter.h>
// Standard includes
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
using namespace eigenkf;
#define COL 10
int main(int /*argc*/, char * /*argv*/[]) {
std::srand(std::time(NULL));
/// We want a simple 2d state
typedef SimpleState<2> state_t;
/// Our process model is the simplest possible: doesn't change the mean
typedef ConstantProcess<2, state_t> process_t;
/// Create a kalman filter instance with our chosen state and process types
KalmanFilter<state_t, process_t> kf;
/// Set our process model's variance
kf.processModel.sigma = state_t::VecState::Constant(6.5);
double dt = 0.5;
double sumSquaredError = 0;
const double measurementVariance = 9; //NOISE_AMPLITUDE / 2.0;
/// for sine curve
std::vector<StatePair> data = generateSineData(dt);
/// CSV header row
std::cout << "actual x,actual y,measurement x,measurement y,filtered x,filtered y,squared error" << std::endl;
double t = 0;
for (unsigned int i = 0; i < data.size(); ++i) {
/// Predict step: Update Kalman filter by predicting ahead by dt
kf.predict(dt);
/// "take a measurement" - in this case, noisify the actual measurement
Eigen::Vector2d pos = data[i].first;
Eigen::Vector2d m = data[i].second;
AbsoluteMeasurement<state_t> meas;
meas.measurement = m;
meas.covariance = Eigen::Vector2d::Constant(measurementVariance).asDiagonal();
/// Correct step: incorporate information from measurement into KF's state
kf.correct(meas);
/// Output info for csv
double squaredError = (pos[0] - kf.state.x[0]) * (pos[0] - kf.state.x[0]);
sumSquaredError += squaredError;
std::cout << pos[0] << "," << pos[1] << ",";
std::cout << meas.measurement[0] << "," << meas.measurement[1] << ",";
std::cout << kf.state.x[0] << "," << kf.state.x[1] << ",";
std::cout << squaredError;
std::cout << std::endl;
t += dt;
}
std::cerr << "Sum squared error: " << sumSquaredError << std::endl;
return 0;
}