-
Notifications
You must be signed in to change notification settings - Fork 11
/
09_DeDvKalman.py
122 lines (97 loc) · 3.05 KB
/
09_DeDvKalman.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
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
'''
Filename: 09_DeDvKalman.py
Created on: April,3, 2021
Author: dhpark
'''
import numpy as np
from numpy.linalg import inv
import matplotlib.pyplot as plt
np.random.seed(0)
firstRun = True
X, P = np.array([[0,0]]).transpose(), np.zeros((2,2)) # X : Previous State Variable Estimation, P : Error Covariance Estimation
A, H = np.array([[0,0], [0,0]]), np.array([[0,0]])
Q, R = np.array([[0,0], [0,0]]), 0
firstRun2 = True
X2, P2 = np.array([[0,0]]).transpose(), np.zeros((2,2)) # X : Previous State Variable Estimation, P : Error Covariance Estimation
A2, H2 = np.array([[0,0], [0,0]]), np.array([[0,0]])
Q2, R2 = np.array([[0,0], [0,0]]), 0
Posp, Velp = None, None
def GetPos():
global Posp, Velp
if Posp == None:
Posp = 0
Velp = 80
dt = 0.1
w = 0 + 10 * np.random.normal()
v = 0 + 10 * np.random.normal()
z = Posp + Velp * dt + v # Position measurement
Posp = z - v
Velp = 80 + w
return z, Posp, Velp
'''
Estimate velocity through displacement
'''
def DvKalman(z):
global firstRun
global A, Q, H, R
global X, P
if firstRun:
dt = 0.1
A = np.array([[1, dt], [0, 1]])
H = np.array([[1, 0]])
Q = np.array([[1, 0], [0, 3]])
R = np.array([10])
X = np.array([0, 20]).transpose()
P = 5 * np.eye(2)
firstRun = False
else:
Xp = A @ X # Xp : State Variable Prediction
Pp = A @ P @ A.T + Q # Error Covariance Prediction
K = (Pp @ H.T) @ inv(H@Pp@H.T + R) # K : Kalman Gain
X = Xp + K@(z - H@Xp) # Update State Variable Estimation
P = Pp - K@H@Pp # Update Error Covariance Estimation
pos = X[0]
vel = X[1]
return pos, vel
def DeDvKalman(z):
global firstRun2
global A2, Q2, H2, R2
global X2, P2
if firstRun2:
dt = 0.1
A2 = np.array([[1, dt], [0, 1]])
H2 = np.array([[1, 0]])
Q2 = np.array([[1, 0], [0, 3]])
R2 = np.array([10])
X2 = np.array([0, 20]).transpose()
P2 = 5 * np.eye(2)
firstRun2 = False
else:
Xp = A2 @ X2 # Xp : State Variable Prediction
Pp = A2 @ P2 @ A2.T + Q2 # Error Covariance Prediction
#K = (Pp @ H.T) @ inv(H@Pp@H.T + R) # K : Kalman Gain
K = 1/(np.array(Pp[0,0]) + R2) * np.array([Pp[0,0], Pp[1,0]]).transpose()
K = K[:, np.newaxis] # maintain axis
X2 = Xp + K@(z - H@Xp) # Update State Variable Estimation
P2 = Pp - K@H2@Pp # Update Error Covariance Estimation
pos = X2[0]
vel = X2[1]
return pos, vel
t = np.arange(0, 10, 0.1)
Nsamples = len(t)
Xsaved = np.zeros([Nsamples, 2])
DeXsaved = np.zeros([Nsamples, 2])
for i in range(Nsamples):
Z, pos_true, vel_true = GetPos()
pos, vel = DvKalman(Z)
dpos, dvel = DeDvKalman(Z)
Xsaved[i] = [pos, vel]
DeXsaved[i] = [dpos, dvel]
plt.figure()
plt.plot(t, Xsaved[:,1], 'b*', label = 'Matrix')
plt.plot(t, DeXsaved[:,1], 'r-', label='Decomposed')
plt.legend(loc='upper left')
plt.ylabel('Velocity [m/s]')
plt.xlabel('Time [sec]')
plt.savefig('result/09_DeDvKalman.png')
plt.show()