-
Notifications
You must be signed in to change notification settings - Fork 78
/
KalmanFilter.java
139 lines (119 loc) · 3.31 KB
/
KalmanFilter.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Simple implementation of the Kalman Filter for 1D data.
* Originally written in JavaScript by Wouter Bulten
*
* Now rewritten into Java
* 2017
*
* @license MIT License
*
* @author Sifan Ye
* @author Andreas Eppler
*
* @see https://github.com/wouterbulten/kalmanjs
*
*/
public class KalmanFilter {
private double A = 1;
private double B = 0;
private double C = 1;
private double R;
private double Q;
private double cov = Double.NaN;
private double x = Double.NaN;
/**
* Constructor
*
* @param R Process noise
* @param Q Measurement noise
* @param A State vector
* @param B Control vector
* @param C Measurement vector
*/
public KalmanFilter(double R, double Q, double A, double B , double C){
this.R = R;
this.Q = Q;
this.A = A;
this.B = B;
this.C = C;
this.cov = Double.NaN;
this.x = Double.NaN; // estimated signal without noise
}
/**
* Constructor
*
* @param R Process noise
* @param Q Measurement noise
*/
public KalmanFilter(double R, double Q){
this.R = R;
this.Q = Q;
}
/**
* Filters a measurement
*
* @param measurement The measurement value to be filtered
* @param u The controlled input value
* @return The filtered value
*/
public double filter(double measurement, double u){
if (Double.isNaN(this.x)) {
this.x = (1 / this.C) * measurement;
this.cov = (1 / this.C) * this.Q * (1 / this.C);
}else {
double predX = (this.A * this.x) + (this.B * u);
double predCov = ((this.A * this.cov) * this.A) + this.R;
// Kalman gain
double K = predCov * this.C * (1 / ((this.C * predCov * this.C) + this.Q));
// Correction
this.x = predX + K * (measurement - (this.C * predX));
this.cov = predCov - (K * this.C * predCov);
}
return this.x;
}
/**
* Filters a measurement
*
* @param measurement The measurement value to be filtered
* @return The filtered value
*/
public double filter(double measurement){
double u = 0;
if (Double.isNaN(this.x)) {
this.x = (1 / this.C) * measurement;
this.cov = (1 / this.C) * this.Q * (1 / this.C);
}else {
double predX = (this.A * this.x) + (this.B * u);
double predCov = ((this.A * this.cov) * this.A) + this.R;
// Kalman gain
double K = predCov * this.C * (1 / ((this.C * predCov * this.C) + this.Q));
// Correction
this.x = predX + K * (measurement - (this.C * predX));
this.cov = predCov - (K * this.C * predCov);
}
return this.x;
}
/**
* Set the last measurement.
* @return The last measurement fed into the filter
*/
public double lastMeasurement(){
return this.x;
}
/**
* Sets measurement noise
*
* @param noise The new measurement noise
*/
public void setMeasurementNoise(double noise){
this.Q = noise;
}
/**
* Sets process noise
*
* @param noise The new process noise
*/
public void setProcessNoise(double noise){
this.R = noise;
}
}