-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathKalmanFilterTest.cs
More file actions
414 lines (343 loc) · 15.4 KB
/
Copy pathKalmanFilterTest.cs
File metadata and controls
414 lines (343 loc) · 15.4 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using System;
using System.Collections.Generic;
using System.IO;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;
using NUnit.Framework;
namespace MathNet.Filtering.Kalman.UnitTests
{
[TestFixture]
public class KalmanFilterTest
{
// Array of measured ranges from cartesian center for a track (noisy - 0.5 units)
double[] rM = {99.9901, 90.2995, 80.0453, 71.5741, 61.7019, 54.0062, 44.4181, 37.3228,
30.8536, 26.2474, 25.0199, 29.1189, 34.3914, 42.5675, 50.7917, 60.1946 };
// Array of measured bearings from cartesian center for a track (noisy - 2deg)
double[] bM = { 0.7848, 0.7551, 0.7213, 0.6654, 0.6565, 0.4747, 0.4465, 0.3066,
0.0720, -0.2540, -0.6531, -0.9779, -1.2390, -1.4442, -1.5699, -1.6535 };
double re = 0.5;
double the = 2d * Math.PI / 180d;
double T = 10d;
double q = 0.01;
public static readonly double DefaultTolerance = 1e-8;
[Test]
public void TestDiscreteKalmanFilter()
{
// Test constants
double r = 30.0; // Measurement covariance
double T = 20.0; // Time interval between measurements
double q = 0.1; // Plant noise constant
double tol = 0.0001; // Accuracy tolerance
// Reference values to test against (generated from a known filter)
// Reference Measurements
double[] zs = { 290.16851039,654.55633793,968.97141280,1325.09197161,1636.35947675,1974.39053148,2260.80770553,2574.36119750,2901.32285462,3259.14709098};
// Expected position estimates (predictions)
double[] posp = {1018.94416547,1237.00029618,1754.97092716,1855.62596430,2400.27521403,2446.47067625,2978.94381631,3173.63724675};
// Expected velocity estimates (predictions)
double[] velp = { 18.21939138,13.38351136,21.52280841,10.92729947,21.32868461,9.24370334,20.26482836,13.59419761 };
// Expected position estimates (after measurement update)
double[] posu = { 969.33006892,1324.51475894,1637.07997492,1973.70152187,2261.59660945,2573.64724909,2901.75329465,3258.67447647 };
// Expected velocity estimates (after measurement update)
double[] velu = { 13.38351136,21.52280841,10.92729947,21.32868461,9.24370334,20.26482836,13.59419761,20.93270702 };
// Initial estimate based on two point differencing
double z0 = zs[0];
double z1 = zs[1];
Matrix<double> x0 = Matrix<double>.Build.Dense(2, 1, new[] { z1, (z1 - z0)/T });
Matrix<double> P0 = Matrix<double>.Build.Dense(2, 2, new[] { r, r/T, r/T, 2*r/(T*T) });
// Setup a DiscreteKalmanFilter to filter
DiscreteKalmanFilter dkf = new DiscreteKalmanFilter(x0, P0);
Matrix<double> F = Matrix<double>.Build.Dense(2, 2, new[] { 1d, 0d, T, 1 }); // State transition matrix
Matrix<double> G = Matrix<double>.Build.Dense(2, 1, new[] { (T * T) / 2d, T }); // Plant noise matrix
Matrix<double> Q = Matrix<double>.Build.Dense(1, 1, new[] { q }); // Plant noise variance
Matrix<double> R = Matrix<double>.Build.Dense(1, 1, new[] { r }); // Measurement variance matrix
Matrix<double> H = Matrix<double>.Build.Dense(1, 2, new[] { 1d, 0d }); // Measurement matrix
// Test the performance of this filter against the stored data from a proven
// Kalman Filter of a one dimenional tracker.
for (int i = 2; i < zs.Length; i++)
{
// Perform the prediction
dkf.Predict(F, G, Q);
// Test against the prediction state/covariance
Assert.IsTrue(dkf.State[0,0].AlmostEqual(posp[i-2], tol), "State Prediction (" + i + ")");
Assert.IsTrue(dkf.State[1, 0].AlmostEqual(velp[i-2], tol), "Covariance Prediction (" + i + ")");
// Perform the update
Matrix<double> z = Matrix<double>.Build.Dense(1, 1, new[] { zs[i] });
dkf.Update(z, H, R);
// Test against the update state/covariance
// Test against the prediction state/covariance
Assert.IsTrue(dkf.State[0, 0].AlmostEqual(posu[i-2], tol), "State Prediction (" + i + ")");
Assert.IsTrue(dkf.State[1, 0].AlmostEqual(velu[i-2], tol), "Covariance Prediction (" + i + ")");
}
}
[Test]
public void TestDiscreteKalmanFilterWrap()
{
// Test constants
double r = 30.0; // Measurement covariance
double T = 20.0; // Time interval between measurements
double q = 0.1; // Plant noise constant
double tol = 0.0001; // Accuracy tolerance
// Reference values to test against (generated from a known filter)
// Reference Measurements
double[] zs = { 0, (Math.PI + 0.1), 2*(Math.PI + 0.1), 3*(Math.PI + 0.1), 4*(Math.PI + 0.1), 5*(Math.PI + 0.1),6*(Math.PI + 0.1)};
// Expected position estimates (predictions)
double[] posp = {0.2, 0.3-Math.PI, 0.4, 0.5-Math.PI, 0.6};
// Expected velocity estimates (predictions)
double[] velp = { (Math.PI + 0.1)/T, (Math.PI + 0.1)/T, (Math.PI + 0.1)/T, (Math.PI + 0.1)/T, (Math.PI + 0.1)/T };
// Expected position estimates (after measurement update)
double[] posu = { 0.2, 0.3-Math.PI, 0.4, 0.5-Math.PI, 0.6 };
// Expected velocity estimates (after measurement update)
double[] velu = { (Math.PI + 0.1)/T, (Math.PI + 0.1)/T, (Math.PI + 0.1)/T, (Math.PI + 0.1)/T, (Math.PI + 0.1)/T };
// Initial estimate based on two point differencing
double z0 = zs[0];
double z1 = zs[1];
Matrix<double> x0 = Matrix<double>.Build.Dense(2, 1, new[] { z1, (z1 - z0)/T });
Matrix<double> P0 = Matrix<double>.Build.Dense(2, 2, new[] { r, r/T, r/T, 2*r/(T*T) });
// Setup a DiscreteKalmanFilter to filter
DiscreteKalmanFilter dkf = new DiscreteKalmanFilter(x0, P0, new int[] {0}, new int[] {0});
Matrix<double> F = Matrix<double>.Build.Dense(2, 2, new[] { 1d, 0d, T, 1 }); // State transition matrix
Matrix<double> G = Matrix<double>.Build.Dense(2, 1, new[] { (T * T) / 2d, T }); // Plant noise matrix
Matrix<double> Q = Matrix<double>.Build.Dense(1, 1, new[] { q }); // Plant noise variance
Matrix<double> R = Matrix<double>.Build.Dense(1, 1, new[] { r }); // Measurement variance matrix
Matrix<double> H = Matrix<double>.Build.Dense(1, 2, new[] { 1d, 0d }); // Measurement matrix
// Test the performance of this filter against the stored data from a proven
// Kalman Filter of a one dimenional tracker.
for (int i = 2; i < zs.Length; i++)
{
// Perform the prediction
dkf.Predict(F, G, Q);
// Test against the prediction state/covariance
Assert.IsTrue(dkf.State[0,0].AlmostEqual(posp[i-2], tol), "State Prediction (" + i + ")");
Assert.IsTrue(dkf.State[1, 0].AlmostEqual(velp[i-2], tol), "Covariance Prediction (" + i + ")");
// Perform the update
Matrix<double> z = Matrix<double>.Build.Dense(1, 1, new[] { zs[i] });
dkf.Update(z, H, R);
// Test against the update state/covariance
// Test against the prediction state/covariance
Assert.IsTrue(dkf.State[0, 0].AlmostEqual(posu[i-2], tol), "State Prediction (" + i + ")");
Assert.IsTrue(dkf.State[1, 0].AlmostEqual(velu[i-2], tol), "Covariance Prediction (" + i + ")");
}
}
[Test]
public void TestInformationFilter()
{
System.Console.WriteLine("Filter 1 - DiscreteKalmanFilter, Filter 2 - InformationFilter");
Matrix<double> x0 = RangeBearingTracker.TwoPointDifferenceState(rM[0], rM[1], bM[0], bM[1], T);
Matrix<double> P0 = RangeBearingTracker.TwoPointDifferenceCovariance(rM[0], rM[1], bM[0], bM[1], re, the, T);
DiscreteKalmanFilter dkf = new DiscreteKalmanFilter(x0, P0);
InformationFilter inf = new InformationFilter(x0, P0);
Assert.IsTrue(RunTest(dkf, inf, DefaultTolerance));
}
[Test]
public void TestSquareRootFilter()
{
System.Console.WriteLine("Filter 1 - DiscreteKalmanFilter, Filter 2 - SquareRootFilter");
Matrix<double> x0 = RangeBearingTracker.TwoPointDifferenceState(rM[0], rM[1], bM[0], bM[1], T);
Matrix<double> P0 = RangeBearingTracker.TwoPointDifferenceCovariance(rM[0], rM[1], bM[0], bM[1], re, the, T);
DiscreteKalmanFilter dkf = new DiscreteKalmanFilter(x0, P0);
SquareRootFilter sqrf = new SquareRootFilter(x0, P0);
Assert.IsTrue(RunTest(dkf, sqrf, DefaultTolerance));
}
private bool RunTest(IKalmanFilter filter1, IKalmanFilter filter2, double tolerance)
{
List<double> xf1 = new List<double>();
List<double> yf1 = new List<double>();
List<double> xf2 = new List<double>();
List<double> yf2 = new List<double>();
Matrix<double> ZeroCov = Matrix<double>.Build.Dense(filter1.Cov.RowCount, filter1.Cov.RowCount);
Matrix<double> ZeroState = Matrix<double>.Build.Dense(filter1.State.RowCount, 1);
RangeBearingTracker rbt1 = new RangeBearingTracker(filter1);
RangeBearingTracker rbt2 = new RangeBearingTracker(filter2);
bool withinTolerance = true;
// Predict the filters
rbt1.Predict(this.T, this.q);
rbt2.Predict(this.T, this.q);
for (int i = 2; i < this.bM.Length; i++)
{
rbt1.Update(this.rM[i], this.bM[i], this.re, this.the);
rbt2.Update(this.rM[i], this.bM[i], this.re, this.the);
xf1.Add(rbt1.State[0,0]);
yf1.Add(rbt1.State[2,0]);
xf2.Add(rbt2.State[0,0]);
yf2.Add(rbt2.State[2,0]);
if (!ZeroCov.AlmostEqual((rbt2.Cov - rbt1.Cov), tolerance))
withinTolerance = false;
else if (!ZeroState.AlmostEqual((rbt2.State - rbt1.State), tolerance))
withinTolerance = false;
rbt1.Predict(this.T, this.q);
rbt2.Predict(this.T, this.q);
}
// Create strings
string x1s = ""; string y1s = ""; string x2s = ""; string y2s = "";
for (int i=0; i < xf1.Count; i++)
{
x1s += xf1[i].ToString("#00.00") + "\t";
y1s += yf1[i].ToString("#00.00") + "\t";
x2s += xf2[i].ToString("#00.00") + "\t";
y2s += yf2[i].ToString("#00.00") + "\t";
}
System.Console.WriteLine("Filter 1 Estimates");
System.Console.WriteLine(x1s);
System.Console.WriteLine(y1s);
System.Console.WriteLine("Filter 2 Estimates");
System.Console.WriteLine(x2s);
System.Console.WriteLine(y2s);
return withinTolerance;
}
private static Matrix<double>[] GetMatrices(byte[] input, int cols)
{
// Create a test reader for the given byte array
MemoryStream stream = new MemoryStream(input);
StreamReader reader = new StreamReader(stream);
// Each item in the dblLines should be a line of doubles read from the bytes
List<double[]> dblLines = new List<double[]>();
while (!reader.EndOfStream)
{
//
List<double> dblVals = new List<double>();
string thisLine = reader.ReadLine();
string[] allVals = thisLine.Split("\t".ToCharArray());
for (int i=0; i<allVals.Length; i++)
{
double tmp = Double.Parse(allVals[i], System.Globalization.NumberFormatInfo.InvariantInfo);
dblVals.Add(tmp);
}
dblLines.Add(dblVals.ToArray());
}
Matrix<double> bigMat = Matrix<double>.Build.DenseOfRowArrays(dblLines);
int num_matrices = dblLines[0].Length / cols;
Matrix<double>[] outMatrices = new Matrix<double>[num_matrices];
for (int i=0; i<num_matrices; i++)
{
outMatrices[i] = bigMat.SubMatrix(0, dblLines.Count, i*cols, cols);
}
return outMatrices;
}
}
internal class OneDimensionTracker
{
public IKalmanFilter Filter
{
get { return this.filter; }
}
IKalmanFilter filter;
private Matrix<double> F;
private Matrix<double> Q;
private Matrix<double> G;
private Matrix<double> H;
private Matrix<double> R;
public OneDimensionTracker(IKalmanFilter filter)
{
this.filter = filter;
this.F = Matrix<double>.Build.Dense(2, 2, new[] { 1d, 0d, -1d, 1d });
this.Q = Matrix<double>.Build.Dense(1, 1);
this.G = Matrix<double>.Build.Dense(2, 1);
this.H = Matrix<double>.Build.Dense(1, 2, new[] { 1d, 0d });
this.R = Matrix<double>.Build.Dense(1, 1);
}
public void Predict(double dT, double q)
{
this.F[0,1] = dT;
this.G[0,0] = 0.5 * (dT * dT);
this.G[1,0] = dT;
this.Q[0,0] = q;
filter.Predict(this.F, this.G, this.Q);
}
public void Update(double x, double r)
{
Matrix<double> z = Matrix<double>.Build.Dense(1, 1, new[] { x });
this.R[0,0] = r;
filter.Update(z, this.H, this.R);
}
}
internal class RangeBearingTracker
{
private IKalmanFilter kf;
public Matrix<double> State
{
get { return kf.State; }
}
public Matrix<double> Cov
{
get { return kf.Cov; }
}
public RangeBearingTracker(IKalmanFilter kf)
{
this.kf = kf;
}
public void Predict(double T, double q)
{
Matrix<double> F = GenerateTransition(T);
Matrix<double> G = GenerateNoiseCoupling(T);
Matrix<double> Q = Matrix<double>.Build.Dense(1, 1, new[] { q });
//System.Console.WriteLine("GQG' " + G * Q * Matrix.Transpose(G));
//kf.Predict(F);
//kf.Predict(F, G * Q * Matrix.Transpose(G));
kf.Predict(F, G, Q);
}
public void Update(double range, double bearing, double range_error, double bearing_error)
{
Matrix<double> Z = Matrix<double>.Build.Dense(2, 1, new[] { range*Math.Cos(bearing), range*Math.Sin(bearing) });
Matrix<double> H = Matrix<double>.Build.Dense(2, 4);
H[0,0] = 1.0d;
H[1,2] = 1.0d;
Matrix<double> R = GenerateCartesianCovariance(range, bearing, range_error, bearing_error);
this.kf.Update(Z, H, R);
}
public static Matrix<double> TwoPointDifferenceCovariance(double r1, double r2, double th1, double th2, double rs, double thetas, double T)
{
Matrix<double> R1 = GenerateCartesianCovariance(r1, th1, rs, thetas);
Matrix<double> R2 = GenerateCartesianCovariance(r2, th2, rs, thetas);
double x1 = r1 * Math.Cos(th1);
double y1 = r1 * Math.Sin(th1);
double x2 = r2 * Math.Cos(th2);
double y2 = r2 * Math.Sin(th2);
double rx = R2[0,0];
double ry = R2[1,1];
double rxy = R2[1,0];
Matrix<double> xc = Matrix<double>.Build.Dense(2, 2, new[] { rx, rx/T, rx/T, (2*rx)/(T*T) });
Matrix<double> yc = Matrix<double>.Build.Dense(2, 2, new[] { ry, ry/T, ry/T, (2*ry)/(T*T) });
Matrix<double> xyc = Matrix<double>.Build.Dense(2, 2, new[] { rxy, rxy/T, rxy/T, (2*rxy)/(T*T) });
return Matrix<double>.Build.DenseOfMatrixArray(new[,] { { xc, xyc }, { xyc, yc } });
}
public static Matrix<double> TwoPointDifferenceState(double r1, double r2, double th1, double th2, double T)
{
double x1 = r1 * Math.Cos(th1);
double y1 = r1 * Math.Sin(th1);
double x2 = r2 * Math.Cos(th2);
double y2 = r2 * Math.Sin(th2);
double[] x0 = new double[4];
x0[0] = x2;
x0[1] = (x2 - x1) / T;
x0[2] = y2;
x0[3] = (y2 - y1) / T;
return Matrix<double>.Build.Dense(4, 1, x0);
}
public static Matrix<double> GenerateCartesianCovariance(double r, double theta, double rs, double thetas)
{
double sinSqth = Math.Sin(theta) * Math.Sin(theta);
double cosSqth = Math.Cos(theta) * Math.Cos(theta);
double[] R = new double[4];
R[0] = ((r * r) * thetas * sinSqth) + (rs * cosSqth);
R[3] = ((r * r) * thetas * cosSqth) + (rs * sinSqth);
R[1] = R[2] = (rs - (r*r*thetas))*Math.Sin(theta)*Math.Cos(theta);
return Matrix<double>.Build.Dense(2, 2, R);
}
private static Matrix<double> GenerateTransition(double T)
{
double[,] Fp = new double[4,4];
Fp[0,0] = 1; Fp[0,1] = T; Fp[1,1] = 1;
Fp[2,2] = 1; Fp[2,3] = T; Fp[3,3] = 1;
return Matrix<double>.Build.DenseOfArray(Fp);
}
private static Matrix<double> GenerateNoiseCoupling(double T)
{
double[] G = new double[4];
G[0] = T * T / 2d;
G[1] = T;
G[2] = T * T/ 2d;
G[3] = T;
return Matrix<double>.Build.Dense(4, 1, G);
}
}
}