@@ -136,6 +136,138 @@ class KalmanFilter(object):
136
136
various state variables to reasonable values; the defaults will
137
137
not give you a functional filter.
138
138
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
+
139
271
You will have to set the following attributes after constructing this
140
272
object for the filter to perform properly. Please note that there are
141
273
various checks in place to ensure that you have made everything the
@@ -247,18 +379,15 @@ class KalmanFilter(object):
247
379
filter's estimates. This formulation of the Fading memory filter
248
380
(there are many) is due to Dan Simon [1]_.
249
381
250
- References
251
- ----------
252
-
253
- .. [1] Dan Simon. "Optimal State Estimation." John Wiley & Sons.
254
- p. 208-212. (2006)
382
+ References
383
+ ----------
255
384
385
+ .. [1] Dan Simon. "Optimal State Estimation." John Wiley & Sons.
386
+ p. 208-212. (2006)
256
387
257
- Examples
258
- --------
388
+ .. [2] Roger Labbe. "Kalman and Bayesian Filters in Python"
389
+ https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
259
390
260
- See my book Kalman and Bayesian Filters in Python
261
- https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python
262
391
"""
263
392
264
393
def __init__ (self , dim_x , dim_z , dim_u = 0 ):
@@ -1013,13 +1142,6 @@ def alpha(self):
1013
1142
memory effect - previous measurements have less influence on the
1014
1143
filter's estimates. This formulation of the Fading memory filter
1015
1144
(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
-
1023
1145
"""
1024
1146
return self ._alpha_sq ** .5
1025
1147
0 commit comments