Skip to content

Commit 97a87dc

Browse files
author
Gabriele
committed
fixed kf test
1 parent 6567a43 commit 97a87dc

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

tests/test_kalmanFilter.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,47 @@
77
class TestKalmanFilter(unittest.TestCase):
88

99
def test_KalmanFilter(self):
10-
1110
print('Run KF class test.')
1211

1312
# initialization of state matrices
1413
A = np.eye(2)
1514
X = np.array([[0.1], [0.1]])
16-
B = np.ones((X.shape[0], 1))
17-
U = 0.5
15+
B = np.ones((2, 1)) # 2 rows (same as X), 1 column
16+
U = np.array([[0.5]]) # single control input as a column vector
1817

1918
# process noise covariance matrix and initial state covariance
20-
Q = np.eye(X.shape[0])
19+
Q = np.eye(2)
2120
P = np.diag((0.01, 0.01))
2221

2322
# measurement matrices (state X plus a random gaussian noise)
24-
Y = np.array([X[0, 0] + 0.001, X[1, 0] + 0.001])
25-
Y = Y.reshape(-1, 1)
23+
Y = np.array([[X[0, 0] + 0.001], [X[1, 0] + 0.001]]) # 2x1 matrix
2624
C = np.eye(2)
2725

2826
# measurement noise covariance
29-
R = np.eye(Y.shape[0])
30-
31-
var = {}
32-
var.update({'X': X})
33-
var.update({'A': A})
34-
var.update({'B': B})
35-
var.update({'U': U})
36-
var.update({'Q': Q})
37-
var.update({'C': C})
38-
var.update({'R': R})
27+
R = np.eye(2)
28+
29+
var = {
30+
'X': X,
31+
'A': A,
32+
'B': B,
33+
'U': U,
34+
'Q': Q,
35+
'C': C,
36+
'R': R
37+
}
3938

4039
kf = KalmanFilter()
4140
kf.setup(var)
4241
kf.predict()
4342
x_est, y_predict = kf.update(Y)
4443

4544
# verify if the object of the class is correct
46-
self.assertEqual(x_est[0], 0.3505)
47-
self.assertEqual(x_est[1], 0.3505)
48-
self.assertEqual(y_predict[0], 0.07026195923972386)
45+
np.testing.assert_almost_equal(x_est[0], 0.1014, decimal=4)
46+
np.testing.assert_almost_equal(x_est[1], 0.1014, decimal=4)
47+
np.testing.assert_almost_equal(y_predict[0], 0.0001588, decimal=7)
4948

5049

5150
if __name__ == '__main__':
5251
suite = unittest.TestSuite()
5352
suite.addTest(TestKalmanFilter('test_KalmanFilter'))
53+
unittest.TextTestRunner().run(suite)

0 commit comments

Comments
 (0)