Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions filterpy/kalman/kalman_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,22 @@
from filterpy.kalman import KalmanFilter
from filterpy.common import Q_discrete_white_noise, Saver

rng = np.random.RandomState(0)

r_std, q_std = 2., 0.003
dt = 1
cv = KalmanFilter(dim_x=2, dim_z=1)
cv.x = np.array([[0., 1.]]) # position, velocity
cv.F = np.array([[1, dt],[ [0, 1]])
cv.R = np.array([[r_std^^2]])
f.H = np.array([[1., 0.]])
f.P = np.diag([.1^^2, .03^^2)
f.Q = Q_discrete_white_noise(2, dt, q_std**2)
cv.x = np.array([[0., 1.]]).T # position, velocity
cv.F = np.array([[1, dt], [0, 1]])
cv.R = np.array([[r_std ** 2]])
cv.H = np.array([[1., 0.]])
cv.P = np.diag([.1 ** 2, .03 ** 2])
cv.Q = Q_discrete_white_noise(2, dt, q_std ** 2)

saver = Saver(cv)
for z in range(100):
cv.predict()
cv.update([z + randn() * r_std])
cv.update([z + rng.randn() * r_std])
saver.save() # save the filter's state

saver.to_array()
Expand All @@ -85,16 +88,18 @@

This code implements the same filter using the procedural form

x = np.array([[0., 1.]]) # position, velocity
F = np.array([[1, dt],[ [0, 1]])
R = np.array([[r_std^^2]])
from filterpy.kalman import predict, update
x = np.array([[0., 1.]]).T # position, velocity
F = np.array([[1, dt], [0, 1]])
R = np.array([[r_std ** 2]])
H = np.array([[1., 0.]])
P = np.diag([.1^^2, .03^^2)
Q = Q_discrete_white_noise(2, dt, q_std**2)
P = np.diag([.1 ** 2, .03 ** 2])
Q = Q_discrete_white_noise(2, dt, q_std ** 2)

xs = []
for z in range(100):
x, P = predict(x, P, F=F, Q=Q)
x, P = update(x, P, z=[z + randn() * r_std], R=R, H=H)
x, P = update(x, P, z=[z + rng.randn() * r_std], R=R, H=H)
xs.append(x[0, 0])
plt.plot(xs)

Expand Down Expand Up @@ -200,7 +205,7 @@ class KalmanFilter(object):

f.H = np.array([[1., 0.]])

Define the state's covariance matrix P.
Define the state's covariance matrix P.

.. code::

Expand Down Expand Up @@ -240,7 +245,7 @@ class KalmanFilter(object):
f.predict()
f.update(z)

do_something_with_estimate (f.x)
do_something_with_estimate(f.x)


**Procedural Form**
Expand Down