Skip to content

Commit 2a5bbdc

Browse files
authored
More intuitive way of plot_covariance_ellipse() (AtsushiSakai#407)
* More intuitive way of plot_covariance_ellipse() in EKF and UKF * Add the same changes in UKF as well * Modified rotation matrix to scipy.spatial.transform.Rotation * Modified angle of covariance matrix * Fixed typos
1 parent 2b31650 commit 2a5bbdc

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

Localization/ensemble_kalman_filter/ensemble_kalman_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def plot_covariance_ellipse(xEst, PEst): # pragma: no cover
167167

168168
x = [a * math.cos(it) for it in t]
169169
y = [b * math.sin(it) for it in t]
170-
angle = math.atan2(eig_vec[big_ind, 1], eig_vec[big_ind, 0])
170+
angle = math.atan2(eig_vec[1, big_ind], eig_vec[0, big_ind])
171171
rot = Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]
172172
fx = np.stack([x, y]).T @ rot
173173

Localization/extended_kalman_filter/extended_kalman_filter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import matplotlib.pyplot as plt
1212
import numpy as np
13+
from scipy.spatial.transform import Rotation as Rot
1314

1415
# Covariance for EKF simulation
1516
Q = np.diag([
@@ -147,9 +148,8 @@ def plot_covariance_ellipse(xEst, PEst): # pragma: no cover
147148
b = math.sqrt(eigval[smallind])
148149
x = [a * math.cos(it) for it in t]
149150
y = [b * math.sin(it) for it in t]
150-
angle = math.atan2(eigvec[bigind, 1], eigvec[bigind, 0])
151-
rot = np.array([[math.cos(angle), math.sin(angle)],
152-
[-math.sin(angle), math.cos(angle)]])
151+
angle = math.atan2(eigvec[1, bigind], eigvec[0, bigind])
152+
rot = Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]
153153
fx = rot @ (np.array([x, y]))
154154
px = np.array(fx[0, :] + xEst[0, 0]).flatten()
155155
py = np.array(fx[1, :] + xEst[1, 0]).flatten()

Localization/particle_filter/particle_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def plot_covariance_ellipse(x_est, p_est): # pragma: no cover
188188

189189
x = [a * math.cos(it) for it in t]
190190
y = [b * math.sin(it) for it in t]
191-
angle = math.atan2(eig_vec[big_ind, 1], eig_vec[big_ind, 0])
191+
angle = math.atan2(eig_vec[1, big_ind], eig_vec[0, big_ind])
192192
rot = Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]
193193
fx = rot.dot(np.array([[x, y]]))
194194
px = np.array(fx[0, :] + x_est[0, 0]).flatten()

Localization/unscented_kalman_filter/unscented_kalman_filter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import matplotlib.pyplot as plt
1212
import numpy as np
13+
from scipy.spatial.transform import Rotation as Rot
1314
import scipy.linalg
1415

1516
# Covariance for UKF simulation
@@ -180,9 +181,8 @@ def plot_covariance_ellipse(xEst, PEst): # pragma: no cover
180181
b = math.sqrt(eigval[smallind])
181182
x = [a * math.cos(it) for it in t]
182183
y = [b * math.sin(it) for it in t]
183-
angle = math.atan2(eigvec[bigind, 1], eigvec[bigind, 0])
184-
rot = np.array([[math.cos(angle), math.sin(angle)],
185-
[-math.sin(angle), math.cos(angle)]])
184+
angle = math.atan2(eigvec[1, bigind], eigvec[0, bigind])
185+
rot = Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]
186186
fx = rot @ np.array([x, y])
187187
px = np.array(fx[0, :] + xEst[0, 0]).flatten()
188188
py = np.array(fx[1, :] + xEst[1, 0]).flatten()

0 commit comments

Comments
 (0)