-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKalmanFilter.cs
296 lines (255 loc) · 8.47 KB
/
KalmanFilter.cs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © Pablo Guzman Sanchez, 2013
// pablogsanchez at gmail.com
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
// This code originated as a contribution by Pablo Sanches, originally based on
// Student Dave's tutorial on Object Tracking in Images Using 2D Kalman Filters,
// shared under the LGPL by explicit written permissions from both authors:
//
// http://studentdavestutorials.weebly.com/object-tracking-2d-kalman-filter.html
//
namespace Accord.Statistics.Running
{
using System;
using Accord.Math;
using Accord.Compat;
/// <summary>
/// Kalman filter for 2D coordinate systems.
/// </summary>
///
/// <remarks>
/// <para>
/// References:
/// <list type="bullet">
/// <item><description><a href="http://studentdavestutorials.weebly.com/object-tracking-2d-kalman-filter.html">
/// Student Dave's tutorial on Object Tracking in Images Using 2D Kalman Filters.
/// Available on: http://studentdavestutorials.weebly.com/object-tracking-2d-kalman-filter.html
/// </a></description></item>
/// </list></para>
/// </remarks>
///
/// <example>
/// <code source="Unit Tests\Accord.Tests.Statistics\KalmanFilterTest.cs" region="doc_push" />
/// </example>
///
[Serializable]
public class MyKalmanFilter2D : IRunning<DoublePoint>, IRunning<double[]>
{
double samplingRate = 1;
double acceleration = 0.0005f;
double accelStdDev = 0.1f;
double[,] Q_estimate; // (location_0, location_1, vel_0, vel_1)
double[,] A;
double[,] B;
double[,] C;
double[,] Ez;
double[,] Ex;
double[,] P;
double[,] K;
double[,] Aux;
static readonly double[,] diagonal =
{
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
};
/// <summary>
/// Gets or sets the current X position of the object.
/// </summary>
///
public double X
{
get { return Q_estimate[0, 0]; }
set { Q_estimate[0, 0] = value; }
}
/// <summary>
/// Gets or sets the current Y position of the object.
/// </summary>
///
public double Y
{
get { return Q_estimate[1, 0]; }
set { Q_estimate[1, 0] = value; }
}
/// <summary>
/// Gets or sets the current object's velocity in the X axis.
/// </summary>
///
public double XAxisVelocity
{
get { return Q_estimate[2, 0]; }
set { Q_estimate[2, 0] = value; }
}
/// <summary>
/// Gets or sets the current object's velocity in the Y axis.
/// </summary>
///
public double YAxisVelocity
{
get { return Q_estimate[3, 0]; }
set { Q_estimate[3, 0] = value; }
}
/// <summary>
/// Gets or sets the observational noise
/// of the current object's in the X axis.
/// </summary>
///
public double NoiseX
{
get { return Ez[0, 0]; }
set { Ez[0, 0] = value; }
}
/// <summary>
/// Gets or sets the observational noise
/// of the current object's in the Y axis.
/// </summary>
///
public double NoiseY
{
get { return Ez[1, 1]; }
set { Ez[1, 1] = value; }
}
/// <summary>
/// Initializes a new instance of the <see cref="KalmanFilter2D"/> class.
/// </summary>
///
public MyKalmanFilter2D()
{
initialize();
}
/// <summary>
/// Initializes a new instance of the <see cref="KalmanFilter2D"/> class.
/// </summary>
///
/// <param name="samplingRate">The sampling rate.</param>
/// <param name="acceleration">The acceleration.</param>
/// <param name="accelerationStdDev">The acceleration standard deviation.</param>
///
public MyKalmanFilter2D(double samplingRate, double acceleration, double accelerationStdDev)
{
this.acceleration = acceleration;
this.accelStdDev = accelerationStdDev;
this.samplingRate = samplingRate;
initialize();
}
private void initialize()
{
double dt = samplingRate;
A = new double[,]
{
{ 1, 0, dt, 0 },
{ 0, 1, 0, dt },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
};
B = new double[,]
{
{ (dt * dt) / 2 },
{ (dt * dt) / 2 },
{ dt },
{ dt }
};
C = new double[,]
{
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 }
};
Ez = new double[,]
{
{ 1.0, 0.0 },
{ 0.0, 1.0 }
};
double dt2 = dt * dt;
double dt3 = dt2 * dt;
double dt4 = dt2 * dt2;
double aVar = accelStdDev * accelStdDev;
Ex = new double[4, 4]
{
{ dt4 / 4, 0, dt3 / 2, 0 },
{ 0, dt4 / 4, 0, dt3 / 2 },
{ dt3 / 2, 0, dt2, 0 },
{ 0, dt3 / 2, 0, dt2 }
};
Ex.Multiply(aVar, result: Ex);
Q_estimate = new double[4, 1];
P = Ex.MemberwiseClone();
}
/// <summary>
/// Registers the occurrence of a value.
/// </summary>
///
/// <param name="value">The value to be registered.</param>
///
public void Push(double[] value)
{
if (value.Length != 2)
throw new DimensionMismatchException("value");
Push(value[0], value[1]);
}
/// <summary>
/// Registers the occurrence of a value.
/// </summary>
///
/// <param name="value">The value to be registered.</param>
///
public void Push(DoublePoint value)
{
Push(value.X, value.Y);
}
/// <summary>
/// Registers the occurrence of a value.
/// </summary>
///
/// <param name="x">The x-coordinate of the value to be registered.</param>
/// <param name="y">The y-coordinate of the value to be registered.</param>
///
public void Push(double x, double y)
{
double[,] Qloc = { { x }, { y } };
// Predict next state
Q_estimate = Matrix.Dot(A, Q_estimate).Add(B.Multiply(acceleration));
// Predict Covariances
P = Matrix.Dot(A, P.DotWithTransposed(A)).Add(Ex);
Aux = Matrix.Dot(C, P.DotWithTransposed(C)).Add(Ez).PseudoInverse();
// Kalman Gain
K = P.Dot(C.TransposeAndDot(Aux));
Q_estimate = Q_estimate.Add(K.Dot(Qloc.Subtract(C.Dot(Q_estimate))));
// Update P (Covariances)
P = Matrix.Dot(diagonal.Subtract(Matrix.Dot(K, C)), P);
}
/// <summary>
/// Clears all measures previously computed.
/// </summary>
///
public void Clear()
{
this.NoiseX = 0;
this.NoiseY = 0;
this.XAxisVelocity = 0;
this.YAxisVelocity = 0;
this.X = 0;
this.Y = 0;
}
}
}