-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate_model.py
94 lines (68 loc) · 2.24 KB
/
simulate_model.py
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
# https://towardsdatascience.com/wtf-is-sensor-fusion-part-2-the-good-old-kalman-filter-3642f321440
import numpy as np
import matplotlib.pyplot as plt
class MotionModel():
def __init__(self, A, Q):
self.A = A
self.Q = Q
(m, _) = Q.shape
self.zero_mean = np.zeros(m)
def __call__(self, x):
new_state = np.matmul(self.A, x) + np.random.multivariate_normal(self.zero_mean, self.Q)
return new_state
class MeasurementModel():
def __init__(self, H, R):
self.H = H
self.R = R
(n, _) = R.shape
self.zero_mean = np.zeros(n)
def __call__(self, x):
measurement = np.matmul(self.H, x) + np.random.multivariate_normal(self.zero_mean, self.R)
return measurement
def create_model_parameters(T=1, s2_x=0.1 ** 2, s2_y=0.1 ** 2, lambda2=0.3 ** 2):
# Motion model parameters
F = np.array([[1, T],
[0, 1]])
base_sigma = np.array([[T ** 3 / 3, T ** 2 / 2],
[T ** 2 / 2, T]])
sigma_x = s2_x * base_sigma
sigma_y = s2_y * base_sigma
zeros_2 = np.zeros((2, 2))
A = np.block([[F, zeros_2],
[zeros_2, F]])
Q = np.block([[sigma_x, zeros_2],
[zeros_2, sigma_y]])
# Measurement model parameters
H = np.array([[1, 0, 0, 0],
[0, 0, 1, 0]])
R = lambda2 * np.eye(2)
return A, H, Q, R
def simulate_system(K, x0):
(A, H, Q, R) = create_model_parameters()
# Create models
motion_model = MotionModel(A, Q)
meas_model = MeasurementModel(H, R)
(m, _) = Q.shape
(n, _) = R.shape
state = np.zeros((K, m))
meas = np.zeros((K, n))
# initial state
x = x0
for k in range(K):
x = motion_model(x)
z = meas_model(x)
state[k, :] = x
meas[k, :] = z
return state, meas
if __name__ == '__main__':
np.random.seed(21)
(state, meas) = simulate_system(K=20, x0=np.array([0, 0.1, 0, 0.1]))
plt.figure(figsize=(7, 5))
plt.plot(state[:, 0], state[:, 2], '-bo')
plt.plot(meas[:, 0], meas[:, 1], 'rx')
plt.xlabel('x [m]')
plt.ylabel('y [m]')
plt.legend(['true state', 'observed measurement'])
plt.axis('square')
plt.tight_layout(pad=0)
plt.show()