Skip to content

Commit 3616e7d

Browse files
committed
Support state addition not using + issue #197
I've added state_add as an attribute to the class to support this use case.
1 parent c05dc4c commit 3616e7d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

filterpy/kalman/UKF.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ def residual(a, b):
136136
y += 2*np.pi
137137
return y
138138
139+
state_add: callable (x, y), optional, default np.add
140+
Function that subtracts two state vectors, returning a new
141+
state vector. Used during update to compute `x + K@y`
142+
You will have to supply this if your state variable does not
143+
suport addition, such as it contains angles.
144+
139145
Attributes
140146
----------
141147
@@ -278,7 +284,8 @@ def residual(a, b):
278284
def __init__(self, dim_x, dim_z, dt, hx, fx, points,
279285
sqrt_fn=None, x_mean_fn=None, z_mean_fn=None,
280286
residual_x=None,
281-
residual_z=None):
287+
residual_z=None,
288+
state_add=None):
282289
"""
283290
Create a Kalman filter. You are responsible for setting the
284291
various state variables to reasonable values; the defaults below will
@@ -327,6 +334,11 @@ def __init__(self, dim_x, dim_z, dt, hx, fx, points,
327334
else:
328335
self.residual_z = residual_z
329336

337+
if state_add is None:
338+
self.state_add = np.add
339+
else:
340+
self.state_add = state_add
341+
330342
# sigma points transformed through f(x) and h(x)
331343
# variables for efficiency so we don't recreate every update
332344

@@ -465,7 +477,7 @@ def update(self, z, R=None, UT=None, hx=None, **hx_args):
465477
self.y = self.residual_z(z, zp) # residual
466478

467479
# update Gaussian state estimate (x, P)
468-
self.x = self.x + dot(self.K, self.y)
480+
self.x = self.state_add(self.x, dot(self.K, self.y))
469481
self.P = self.P - dot(self.K, dot(self.S, self.K.T))
470482

471483
# save measurement and posterior state

0 commit comments

Comments
 (0)