test_KalmanFilter.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2019, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 
6 See LICENSE for the license information
7 
8 KalmanFilter unit tests.
9 Author: Frank Dellaert & Duy Nguyen Ta (Python)
10 """
11 import unittest
12 
13 import numpy as np
14 
15 import gtsam
16 from gtsam.utils.test_case import GtsamTestCase
17 
18 
20 
21  def test_KalmanFilter(self):
22  F = np.eye(2)
23  B = np.eye(2)
24  u = np.array([1.0, 0.0])
25  modelQ = gtsam.noiseModel.Diagonal.Sigmas(np.array([0.1, 0.1]))
26  Q = 0.01 * np.eye(2)
27  H = np.eye(2)
28  z1 = np.array([1.0, 0.0])
29  z2 = np.array([2.0, 0.0])
30  z3 = np.array([3.0, 0.0])
31  modelR = gtsam.noiseModel.Diagonal.Sigmas(np.array([0.1, 0.1]))
32  R = 0.01 * np.eye(2)
33 
34  # Create the set of expected output TestValues
35  expected0 = np.array([0.0, 0.0])
36  P00 = 0.01 * np.eye(2)
37 
38  expected1 = np.array([1.0, 0.0])
39  P01 = P00 + Q
40  I11 = np.linalg.inv(P01) + np.linalg.inv(R)
41 
42  expected2 = np.array([2.0, 0.0])
43  P12 = np.linalg.inv(I11) + Q
44  I22 = np.linalg.inv(P12) + np.linalg.inv(R)
45 
46  expected3 = np.array([3.0, 0.0])
47  P23 = np.linalg.inv(I22) + Q
48  I33 = np.linalg.inv(P23) + np.linalg.inv(R)
49 
50  # Create an KalmanFilter object
51  KF = gtsam.KalmanFilter(n=2)
52 
53  # Create the Kalman Filter initialization point
54  x_initial = np.array([0.0, 0.0])
55  P_initial = 0.01 * np.eye(2)
56 
57  # Create an KF object
58  state = KF.init(x_initial, P_initial)
59  self.assertTrue(np.allclose(expected0, state.mean()))
60  self.assertTrue(np.allclose(P00, state.covariance()))
61 
62  # Run iteration 1
63  state = KF.predict(state, F, B, u, modelQ)
64  self.assertTrue(np.allclose(expected1, state.mean()))
65  self.assertTrue(np.allclose(P01, state.covariance()))
66  state = KF.update(state, H, z1, modelR)
67  self.assertTrue(np.allclose(expected1, state.mean()))
68  self.assertTrue(np.allclose(I11, state.information()))
69 
70  # Run iteration 2
71  state = KF.predict(state, F, B, u, modelQ)
72  self.assertTrue(np.allclose(expected2, state.mean()))
73  state = KF.update(state, H, z2, modelR)
74  self.assertTrue(np.allclose(expected2, state.mean()))
75 
76  # Run iteration 3
77  state = KF.predict(state, F, B, u, modelQ)
78  self.assertTrue(np.allclose(expected3, state.mean()))
79  state = KF.update(state, H, z3, modelR)
80  self.assertTrue(np.allclose(expected3, state.mean()))
81 
82 if __name__ == "__main__":
83  unittest.main()
static shared_ptr Sigmas(const Vector &sigmas, bool smart=true)
Definition: NoiseModel.cpp:270


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03