Skip to content

Commit a33110a

Browse files
committed
remove np.matrix
1 parent b51f611 commit a33110a

File tree

1 file changed

+44
-45
lines changed

1 file changed

+44
-45
lines changed

Localization/unscented_kalman_filter/unscented_kalman_filter.py

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
def calc_input():
3434
v = 1.0 # [m/s]
3535
yawrate = 0.1 # [rad/s]
36-
u = np.matrix([v, yawrate]).T
36+
u = np.array([[v, yawrate]]).T
3737
return u
3838

3939

@@ -44,12 +44,12 @@ def observation(xTrue, xd, u):
4444
# add noise to gps x-y
4545
zx = xTrue[0, 0] + np.random.randn() * Qsim[0, 0]
4646
zy = xTrue[1, 0] + np.random.randn() * Qsim[1, 1]
47-
z = np.matrix([zx, zy])
47+
z = np.array([[zx, zy]]).T
4848

4949
# add noise to input
50-
ud1 = u[0, 0] + np.random.randn() * Rsim[0, 0]
51-
ud2 = u[1, 0] + np.random.randn() * Rsim[1, 1]
52-
ud = np.matrix([ud1, ud2]).T
50+
ud1 = u[0] + np.random.randn() * Rsim[0, 0]
51+
ud2 = u[1] + np.random.randn() * Rsim[1, 1]
52+
ud = np.array([ud1, ud2])
5353

5454
xd = motion_model(xd, ud)
5555

@@ -58,52 +58,51 @@ def observation(xTrue, xd, u):
5858

5959
def motion_model(x, u):
6060

61-
F = np.matrix([[1.0, 0, 0, 0],
62-
[0, 1.0, 0, 0],
63-
[0, 0, 1.0, 0],
64-
[0, 0, 0, 0]])
61+
F = np.array([[1.0, 0, 0, 0],
62+
[0, 1.0, 0, 0],
63+
[0, 0, 1.0, 0],
64+
[0, 0, 0, 0]])
6565

66-
B = np.matrix([[DT * math.cos(x[2, 0]), 0],
67-
[DT * math.sin(x[2, 0]), 0],
68-
[0.0, DT],
69-
[1.0, 0.0]])
66+
B = np.array([[DT * math.cos(x[2]), 0],
67+
[DT * math.sin(x[2]), 0],
68+
[0.0, DT],
69+
[1.0, 0.0]])
7070

71-
x = F * x + B * u
71+
x = F @ x + B @ u
7272

7373
return x
7474

7575

7676
def observation_model(x):
77-
# Observation Model
78-
H = np.matrix([
77+
H = np.array([
7978
[1, 0, 0, 0],
8079
[0, 1, 0, 0]
8180
])
8281

83-
z = H * x
82+
z = H @ x
8483

8584
return z
8685

8786

8887
def generate_sigma_points(xEst, PEst, gamma):
8988
sigma = xEst
90-
Psqrt = np.matrix(scipy.linalg.sqrtm(PEst))
89+
Psqrt = scipy.linalg.sqrtm(PEst)
9190
n = len(xEst[:, 0])
9291
# Positive direction
9392
for i in range(n):
94-
sigma = np.hstack((sigma, xEst + gamma * Psqrt[:, i]))
93+
sigma = np.hstack((sigma, xEst + gamma * Psqrt[:, i:i + 1]))
9594

9695
# Negative direction
9796
for i in range(n):
98-
sigma = np.hstack((sigma, xEst - gamma * Psqrt[:, i]))
97+
sigma = np.hstack((sigma, xEst - gamma * Psqrt[:, i:i + 1]))
9998

10099
return sigma
101100

102101

103102
def predict_sigma_motion(sigma, u):
104103
# Sigma Points predition with motion model
105104
for i in range(sigma.shape[1]):
106-
sigma[:, i] = motion_model(sigma[:, i], u)
105+
sigma[:, i:i + 1] = motion_model(sigma[:, i:i + 1], u)
107106

108107
return sigma
109108

@@ -120,21 +119,21 @@ def predict_sigma_observation(sigma):
120119

121120
def calc_sigma_covariance(x, sigma, wc, Pi):
122121
nSigma = sigma.shape[1]
123-
d = sigma - x[0:sigma.shape[0], :]
122+
d = sigma - x[0:sigma.shape[0]]
124123
P = Pi
125124
for i in range(nSigma):
126-
P = P + wc[0, i] * d[:, i] * d[:, i].T
125+
P = P + wc[0, i] * d[:, i:i + 1] @ d[:, i:i + 1].T
127126
return P
128127

129128

130129
def calc_pxz(sigma, x, z_sigma, zb, wc):
131-
# function P=CalcPxz(sigma,xPred,zSigma,zb,wc)
132130
nSigma = sigma.shape[1]
133-
dx = np.matrix(sigma - x)
134-
dz = np.matrix(z_sigma - zb[0:2, :])
135-
P = np.matrix(np.zeros((dx.shape[0], dz.shape[0])))
131+
dx = sigma - x
132+
dz = z_sigma - zb[0:2]
133+
P = np.zeros((dx.shape[0], dz.shape[0]))
134+
136135
for i in range(nSigma):
137-
P = P + wc[0, i] * dx[:, i] * dz[:, i].T
136+
P = P + wc[0, i] * dx[:, i:i + 1] @ dz[:, i:i + 1].T
138137

139138
return P
140139

@@ -144,20 +143,20 @@ def ukf_estimation(xEst, PEst, z, u, wm, wc, gamma):
144143
# Predict
145144
sigma = generate_sigma_points(xEst, PEst, gamma)
146145
sigma = predict_sigma_motion(sigma, u)
147-
xPred = (wm * sigma.T).T
146+
xPred = (wm @ sigma.T).T
148147
PPred = calc_sigma_covariance(xPred, sigma, wc, Q)
149148

150149
# Update
151150
zPred = observation_model(xPred)
152-
y = z.T - zPred
151+
y = z - zPred
153152
sigma = generate_sigma_points(xPred, PPred, gamma)
154-
zb = (wm * sigma.T).T
153+
zb = (wm @ sigma.T).T
155154
z_sigma = predict_sigma_observation(sigma)
156155
st = calc_sigma_covariance(zb, z_sigma, wc, R)
157156
Pxz = calc_pxz(sigma, xPred, z_sigma, zb, wc)
158-
K = Pxz * np.linalg.inv(st)
159-
xEst = xPred + K * y
160-
PEst = PPred - K * st * K.T
157+
K = Pxz @ np.linalg.inv(st)
158+
xEst = xPred + K @ y
159+
PEst = PPred - K @ st @ K.T
161160

162161
return xEst, PEst
163162

@@ -179,9 +178,9 @@ def plot_covariance_ellipse(xEst, PEst):
179178
x = [a * math.cos(it) for it in t]
180179
y = [b * math.sin(it) for it in t]
181180
angle = math.atan2(eigvec[bigind, 1], eigvec[bigind, 0])
182-
R = np.matrix([[math.cos(angle), math.sin(angle)],
183-
[-math.sin(angle), math.cos(angle)]])
184-
fx = R * np.matrix([x, y])
181+
R = np.array([[math.cos(angle), math.sin(angle)],
182+
[-math.sin(angle), math.cos(angle)]])
183+
fx = R @ np.array([x, y])
185184
px = np.array(fx[0, :] + xEst[0, 0]).flatten()
186185
py = np.array(fx[1, :] + xEst[1, 0]).flatten()
187186
plt.plot(px, py, "--r")
@@ -197,8 +196,8 @@ def setup_ukf(nx):
197196
wc.append(1.0 / (2 * (nx + lamda)))
198197
gamma = math.sqrt(nx + lamda)
199198

200-
wm = np.matrix(wm)
201-
wc = np.matrix(wc)
199+
wm = np.array([wm])
200+
wc = np.array([wc])
202201

203202
return wm, wc, gamma
204203

@@ -207,18 +206,18 @@ def main():
207206
print(__file__ + " start!!")
208207

209208
nx = 4 # State Vector [x y yaw v]'
210-
xEst = np.matrix(np.zeros((nx, 1)))
211-
xTrue = np.matrix(np.zeros((nx, 1)))
209+
xEst = np.zeros((nx, 1))
210+
xTrue = np.zeros((nx, 1))
212211
PEst = np.eye(nx)
213-
xDR = np.matrix(np.zeros((nx, 1))) # Dead reckoning
212+
xDR = np.zeros((nx, 1)) # Dead reckoning
214213

215214
wm, wc, gamma = setup_ukf(nx)
216215

217216
# history
218217
hxEst = xEst
219218
hxTrue = xTrue
220219
hxDR = xTrue
221-
hz = np.zeros((1, 2))
220+
hz = np.zeros((2, 1))
222221

223222
time = 0.0
224223

@@ -234,11 +233,11 @@ def main():
234233
hxEst = np.hstack((hxEst, xEst))
235234
hxDR = np.hstack((hxDR, xDR))
236235
hxTrue = np.hstack((hxTrue, xTrue))
237-
hz = np.vstack((hz, z))
236+
hz = np.hstack((hz, z))
238237

239238
if show_animation:
240239
plt.cla()
241-
plt.plot(hz[:, 0], hz[:, 1], ".g")
240+
plt.plot(hz[0, :], hz[1, :], ".g")
242241
plt.plot(np.array(hxTrue[0, :]).flatten(),
243242
np.array(hxTrue[1, :]).flatten(), "-b")
244243
plt.plot(np.array(hxDR[0, :]).flatten(),

0 commit comments

Comments
 (0)