Skip to content

Commit f5beb9c

Browse files
committed
Better documentation for KalmanFilter class.
1 parent bfad77c commit f5beb9c

File tree

2 files changed

+164
-17
lines changed

2 files changed

+164
-17
lines changed

filterpy/changelog.txt

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
1-
Version 1.4.x
1+
Version 1.4.2
22
=============
33

44
* added a fast inverse of a diagonal matrix function common.inv_diagonal()
55

66
* added ability to order Q matrix by dimension or derivative with the helper
77
functions
88

9+
* mahalanobis, log-likelihood, and likelihood only computed when requested;
10+
`compute_log_likelihood` attribute removed.
11+
12+
* fix for bug where attributes not saved. GitHub #136
13+
14+
* Many minor typos in docstring and comments fixes
15+
16+
* Added saving of posterior state to classes
17+
18+
* Added exception to IMM if filter bank dimensions do not agree. GitHub #141
19+
20+
* Added Mahalanobis distance to classes that didn't support it
21+
22+
* added flatten() to saver as an convienent way to flatten column vectors
23+
so they will display nicely in the REPL.
24+
25+
* Partially removed dependence on Matplotlib. Matplotlib is only imported
26+
by functions that use plotting. Github #148
27+
28+
* Removed ability to pass in single matrix to batch_filter functions/methods.
29+
Github #147. In general it is impossible to tell if you have a list of matrices,
30+
or an single array, depending on the shape of the arrays and the size of the
31+
input. Rather than try and obscurely fail, make the user pass the correctly
32+
dimensioned data in.
33+
934

1035
Version 1.4.1
1136
=============

filterpy/kalman/kalman_filter.py

+138-16
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,138 @@ class KalmanFilter(object):
136136
various state variables to reasonable values; the defaults will
137137
not give you a functional filter.
138138
139+
For now the best documentation is my free book Kalman and Bayesian
140+
Filters in Python [2]_. The test files in this directory also give you a
141+
basic idea of use, albeit without much description.
142+
143+
In brief, you will first construct this object, specifying the size of
144+
the state vector with dim_x and the size of the measurement vector that
145+
you will be using with dim_z. These are mostly used to perform size checks
146+
when you assign values to the various matrices. For example, if you
147+
specified dim_z=2 and then try to assign a 3x3 matrix to R (the
148+
measurement noise matrix you will get an assert exception because R
149+
should be 2x2. (If for whatever reason you need to alter the size of
150+
things midstream just use the underscore version of the matrices to
151+
assign directly: your_filter._R = a_3x3_matrix.)
152+
153+
After construction the filter will have default matrices created for you,
154+
but you must specify the values for each. It’s usually easiest to just
155+
overwrite them rather than assign to each element yourself. This will be
156+
clearer in the example below. All are of type numpy.array.
157+
158+
159+
Examples
160+
--------
161+
162+
Here is a filter that tracks position and velocity using a sensor that only
163+
reads position.
164+
165+
First construct the object with the required dimensionality.
166+
167+
.. code::
168+
169+
from filterpy.kalman import KalmanFilter
170+
f = KalmanFilter (dim_x=2, dim_z=1)
171+
172+
173+
Assign the initial value for the state (position and velocity). You can do this
174+
with a two dimensional array like so:
175+
176+
.. code::
177+
178+
f.x = np.array([[2.], # position
179+
[0.]]) # velocity
180+
181+
or just use a one dimensional array, which I prefer doing.
182+
183+
.. code::
184+
185+
f.x = np.array([2., 0.])
186+
187+
188+
Define the state transition matrix:
189+
190+
.. code::
191+
192+
f.F = np.array([[1.,1.],
193+
[0.,1.]])
194+
195+
Define the measurement function:
196+
197+
.. code::
198+
199+
f.H = np.array([[1.,0.]])
200+
201+
Define the covariance matrix. Here I take advantage of the fact that
202+
P already contains np.eye(dim_x), and just multiply by the uncertainty:
203+
204+
.. code::
205+
206+
f.P *= 1000.
207+
208+
I could have written:
209+
210+
.. code::
211+
212+
f.P = np.array([[1000., 0.],
213+
[ 0., 1000.] ])
214+
215+
You decide which is more readable and understandable.
216+
217+
Now assign the measurement noise. Here the dimension is 1x1, so I can
218+
use a scalar
219+
220+
.. code::
221+
222+
f.R = 5
223+
224+
I could have done this instead:
225+
226+
.. code::
227+
228+
f.R = np.array([[5.]])
229+
230+
Note that this must be a 2 dimensional array, as must all the matrices.
231+
232+
Finally, I will assign the process noise. Here I will take advantage of
233+
another FilterPy library function:
234+
235+
.. code::
236+
237+
from filterpy.common import Q_discrete_white_noise
238+
f.Q = Q_discrete_white_noise(dim=2, dt=0.1, var=0.13)
239+
240+
241+
Now just perform the standard predict/update loop:
242+
243+
while some_condition_is_true:
244+
245+
.. code::
246+
247+
z = get_sensor_reading()
248+
f.predict()
249+
f.update(z)
250+
251+
do_something_with_estimate (f.x)
252+
253+
254+
**Procedural Form**
255+
256+
This module also contains stand alone functions to perform Kalman filtering.
257+
Use these if you are not a fan of objects.
258+
259+
**Example**
260+
261+
.. code::
262+
263+
while True:
264+
z, R = read_sensor()
265+
x, P = predict(x, P, F, Q)
266+
x, P = update(x, P, z, R, H)
267+
268+
See my book Kalman and Bayesian Filters in Python [2]_.
269+
270+
139271
You will have to set the following attributes after constructing this
140272
object for the filter to perform properly. Please note that there are
141273
various checks in place to ensure that you have made everything the
@@ -247,18 +379,15 @@ class KalmanFilter(object):
247379
filter's estimates. This formulation of the Fading memory filter
248380
(there are many) is due to Dan Simon [1]_.
249381
250-
References
251-
----------
252-
253-
.. [1] Dan Simon. "Optimal State Estimation." John Wiley & Sons.
254-
p. 208-212. (2006)
382+
References
383+
----------
255384
385+
.. [1] Dan Simon. "Optimal State Estimation." John Wiley & Sons.
386+
p. 208-212. (2006)
256387
257-
Examples
258-
--------
388+
.. [2] Roger Labbe. "Kalman and Bayesian Filters in Python"
389+
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
259390
260-
See my book Kalman and Bayesian Filters in Python
261-
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
262391
"""
263392

264393
def __init__(self, dim_x, dim_z, dim_u=0):
@@ -1013,13 +1142,6 @@ def alpha(self):
10131142
memory effect - previous measurements have less influence on the
10141143
filter's estimates. This formulation of the Fading memory filter
10151144
(there are many) is due to Dan Simon [1]_.
1016-
1017-
References
1018-
----------
1019-
1020-
.. [1] Dan Simon. "Optimal State Estimation." John Wiley & Sons.
1021-
p. 208-212. (2006)
1022-
10231145
"""
10241146
return self._alpha_sq**.5
10251147

0 commit comments

Comments
 (0)