Skip to content

Commit

Permalink
Merge of simplex points.
Browse files Browse the repository at this point in the history
I changed the implementation to not use strings to determine the size
of num_sigmas, but instead a function in the simga classes.
  • Loading branch information
rlabbe committed Sep 15, 2016
1 parent 863616a commit 888957f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
5 changes: 1 addition & 4 deletions filterpy/kalman/UKF.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,7 @@ def residual(a, b):
self._dim_z = dim_z
self.points_fn = points
self._dt = dt
if self.points_fn.name == 'Simplex':
self._num_sigmas = dim_x + 1
else:
self._num_sigmas = 2*dim_x + 1
self._num_sigmas = points.num_sigmas()
self.hx = hx
self.fx = fx
self.x_mean = x_mean_fn
Expand Down
34 changes: 22 additions & 12 deletions filterpy/kalman/sigma_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def __init__(self, n, alpha, beta, kappa, sqrt_method=None, subtract=None):
"""

self.n = n
self.name = 'Merwe'
self.alpha = alpha
self.beta = beta
self.kappa = kappa
Expand All @@ -91,6 +90,11 @@ def __init__(self, n, alpha, beta, kappa, sqrt_method=None, subtract=None):
self.subtract = subtract


def num_sigmas(self):
""" Number of sigma points for each variable in the state x"""
return 2*self.n + 1


def sigma_points(self, x, P):
""" Computes the sigma points for an unscented Kalman filter
given the mean (x) and covariance(P) of the filter.
Expand Down Expand Up @@ -219,7 +223,6 @@ def __init__(self,n, kappa, sqrt_method=None, subtract=None):
"""

self.n = n
self.name = 'Julier'
self.kappa = kappa
if sqrt_method is None:
self.sqrt = cholesky
Expand All @@ -232,6 +235,11 @@ def __init__(self,n, kappa, sqrt_method=None, subtract=None):
self.subtract = subtract


def num_sigmas(self):
""" Number of sigma points for each variable in the state x"""
return 2*self.n + 1


def sigma_points(self, x, P):
r""" Computes the sigma points for an unscented Kalman filter
given the mean (x) and covariance(P) of the filter.
Expand Down Expand Up @@ -321,8 +329,8 @@ class SimplexSigmaPoints(object):

def __init__(self, n, alpha=1, sqrt_method=None, subtract=None):
"""
Generates sigma points and weights according to the simplex method presented in [1]
DOI: 10.1051/cocv/2010006
Generates sigma points and weights according to the simplex method
presented in [1] DOI: 10.1051/cocv/2010006
Parameters
----------
Expand Down Expand Up @@ -350,12 +358,12 @@ def __init__(self, n, alpha=1, sqrt_method=None, subtract=None):
References
----------
.. [1] Phillippe Moireau and Dominique Chapelle "Reduced-Order Unscented Kalman Filtering with Application to
Parameter Identification in Large-Dimensional Systems"
.. [1] Phillippe Moireau and Dominique Chapelle "Reduced-Order Unscented
Kalman Filtering with Application to Parameter Identification in
Large-Dimensional Systems"
"""

self.n = n
self.name = 'Simplex'
self.alpha = alpha
if sqrt_method is None:
self.sqrt = cholesky
Expand All @@ -368,6 +376,11 @@ def __init__(self, n, alpha=1, sqrt_method=None, subtract=None):
self.subtract = subtract


def num_sigmas(self):
""" Number of sigma points for each variable in the state x"""
return self.n + 1


def sigma_points(self, x, P):
""" Computes the implex sigma points for an unscented Kalman filter
given the mean (x) and covariance(P) of the filter.
Expand Down Expand Up @@ -412,7 +425,7 @@ def sigma_points(self, x, P):

U = self.sqrt(P)

lambda_ = n/(n+1)
lambda_ = n / (n + 1)
Istar = np.array([[-1/np.sqrt(2*lambda_), 1/np.sqrt(2*lambda_)]])
for d in range(2, n+1):
row = np.ones((1, Istar.shape[1] + 1)) * 1. / np.sqrt(lambda_*d*(d + 1))
Expand All @@ -422,9 +435,7 @@ def sigma_points(self, x, P):
I = np.sqrt(n)*Istar
scaled_unitary = U.dot(I)

# sigmas = np.zeros((n+1, n))
sigmas = self.subtract(x, -scaled_unitary)

return sigmas.T


Expand All @@ -442,8 +453,7 @@ def weights(self):
"""

n = self.n

c = 1 / (n + 1)
c = 1. / (n + 1)
W = np.full(n + 1, c)

return W, W
12 changes: 6 additions & 6 deletions filterpy/kalman/tests/test_ukf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from math import cos, sin


DO_PLOT = True
DO_PLOT = False


def test_sigma_plot():
Expand Down Expand Up @@ -840,13 +840,13 @@ def fx(x, dt):
if __name__ == "__main__":

DO_PLOT = True

#test_sigma_plot()
# test_linear_1d()
# test_batch_missing_data()
#
# test_linear_2d()
# test_julier_sigma_points_1D()
# test_simplex_sigma_points_1D()
#test_simplex_sigma_points_1D()
# test_fixed_lag()
# DO_PLOT = True
# test_rts()
Expand All @@ -866,7 +866,7 @@ def fx(x, dt):
xi,w = sigma_points (x,P,kappa)
xm, cov = unscented_transform(xi, w)'''
test_radar()
#test_radar()
# test_sigma_plot()
# test_julier_weights()
# test_scaled_weights()
Expand All @@ -877,6 +877,6 @@ def fx(x, dt):
print(xm)
print(cov)"""
# sigma_points ([5,2],9*np.eye(2), 2)
plt.legend()
plt.show()
#plt.legend()
#plt.show()

0 comments on commit 888957f

Please sign in to comment.