Skip to content

Commit 5549876

Browse files
Update np.matrix to np.array
1 parent d1e96d8 commit 5549876

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

Localization/extended_kalman_filter/extended_kalman_filter.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
def calc_input():
2828
v = 1.0 # [m/s]
2929
yawrate = 0.1 # [rad/s]
30-
u = np.matrix([v, yawrate]).T
30+
u = np.array([[v, yawrate]]).T
3131
return u
3232

3333

@@ -38,12 +38,12 @@ def observation(xTrue, xd, u):
3838
# add noise to gps x-y
3939
zx = xTrue[0, 0] + np.random.randn() * Qsim[0, 0]
4040
zy = xTrue[1, 0] + np.random.randn() * Qsim[1, 1]
41-
z = np.matrix([zx, zy])
41+
z = np.array([[zx, zy]])
4242

4343
# add noise to input
4444
ud1 = u[0, 0] + np.random.randn() * Rsim[0, 0]
4545
ud2 = u[1, 0] + np.random.randn() * Rsim[1, 1]
46-
ud = np.matrix([ud1, ud2]).T
46+
ud = np.array([[ud1, ud2]]).T
4747

4848
xd = motion_model(xd, ud)
4949

@@ -52,29 +52,29 @@ def observation(xTrue, xd, u):
5252

5353
def motion_model(x, u):
5454

55-
F = np.matrix([[1.0, 0, 0, 0],
55+
F = np.array([[1.0, 0, 0, 0],
5656
[0, 1.0, 0, 0],
5757
[0, 0, 1.0, 0],
5858
[0, 0, 0, 0]])
5959

60-
B = np.matrix([[DT * math.cos(x[2, 0]), 0],
60+
B = np.array([[DT * math.cos(x[2, 0]), 0],
6161
[DT * math.sin(x[2, 0]), 0],
6262
[0.0, DT],
6363
[1.0, 0.0]])
6464

65-
x = F * x + B * u
65+
x = F.dot(x) + B.dot(u)
6666

6767
return x
6868

6969

7070
def observation_model(x):
7171
# Observation Model
72-
H = np.matrix([
72+
H = np.array([
7373
[1, 0, 0, 0],
7474
[0, 1, 0, 0]
7575
])
7676

77-
z = H * x
77+
z = H.dot(x)
7878

7979
return z
8080

@@ -96,7 +96,7 @@ def jacobF(x, u):
9696
"""
9797
yaw = x[2, 0]
9898
v = u[0, 0]
99-
jF = np.matrix([
99+
jF = np.array([
100100
[1.0, 0.0, -DT * v * math.sin(yaw), DT * math.cos(yaw)],
101101
[0.0, 1.0, DT * v * math.cos(yaw), DT * math.sin(yaw)],
102102
[0.0, 0.0, 1.0, 0.0],
@@ -107,7 +107,7 @@ def jacobF(x, u):
107107

108108
def jacobH(x):
109109
# Jacobian of Observation Model
110-
jH = np.matrix([
110+
jH = np.array([
111111
[1, 0, 0, 0],
112112
[0, 1, 0, 0]
113113
])
@@ -126,10 +126,10 @@ def ekf_estimation(xEst, PEst, z, u):
126126
jH = jacobH(xPred)
127127
zPred = observation_model(xPred)
128128
y = z.T - zPred
129-
S = jH * PPred * jH.T + R
130-
K = PPred * jH.T * np.linalg.inv(S)
131-
xEst = xPred + K * y
132-
PEst = (np.eye(len(xEst)) - K * jH) * PPred
129+
S = jH.dot(PPred).dot(jH.T) + R
130+
K = PPred.dot(jH.T).dot(np.linalg.inv(S))
131+
xEst = xPred + K.dot(y)
132+
PEst = (np.eye(len(xEst)) - K.dot(jH)).dot(PPred)
133133

134134
return xEst, PEst
135135

@@ -151,9 +151,9 @@ def plot_covariance_ellipse(xEst, PEst):
151151
x = [a * math.cos(it) for it in t]
152152
y = [b * math.sin(it) for it in t]
153153
angle = math.atan2(eigvec[bigind, 1], eigvec[bigind, 0])
154-
R = np.matrix([[math.cos(angle), math.sin(angle)],
154+
R = np.array([[math.cos(angle), math.sin(angle)],
155155
[-math.sin(angle), math.cos(angle)]])
156-
fx = R * np.matrix([x, y])
156+
fx = R.dot(np.array([[x, y]]))
157157
px = np.array(fx[0, :] + xEst[0, 0]).flatten()
158158
py = np.array(fx[1, :] + xEst[1, 0]).flatten()
159159
plt.plot(px, py, "--r")
@@ -165,11 +165,11 @@ def main():
165165
time = 0.0
166166

167167
# State Vector [x y yaw v]'
168-
xEst = np.matrix(np.zeros((4, 1)))
169-
xTrue = np.matrix(np.zeros((4, 1)))
168+
xEst = np.array(np.zeros((4, 1)))
169+
xTrue = np.array(np.zeros((4, 1)))
170170
PEst = np.eye(4)
171171

172-
xDR = np.matrix(np.zeros((4, 1))) # Dead reckoning
172+
xDR = np.array(np.zeros((4, 1))) # Dead reckoning
173173

174174
# history
175175
hxEst = xEst

0 commit comments

Comments
 (0)