rlabbe / filterpy

Python Kalman filtering and optimal estimation library. Implements Kalman filter, particle filter, Extended Kalman filter, Unscented Kalman filter, g-h (alpha-beta), least squares, H Infinity, smoothers, and more. Has companion book 'Kalman and Bayesian Filters in Python'.
MIT License
3.22k stars 612 forks source link

Batch filter not working as expected #282

Closed u3Izx9ql7vW4 closed 1 year ago

u3Izx9ql7vW4 commented 1 year ago

Trying to following the examples provided in the documentations, but it's not really working as expected:

Using the example of filterpy.kalman.batch_filter, I get module 'filterpy.kalman' has no attribute 'F':

zs = [t + random.randn()*4 for t in range (40)]
Fs = [kf.F for t in range (40)]
Hs = [kf.H for t in range (40)]

(mu, cov, _, _) = kf.batch_filter(zs, Rs=R_list, Fs=Fs, Hs=Hs, Qs=None,
                                  Bs=None, us=None, update_first=False)
(xs, Ps, Ks) = kf.rts_smoother(mu, cov, Fs=Fs, Qs=None)

If I use the example in batch_filter in filterpy.kalman.KalmanFilter(dim_x, dim_z, dim_u=0):

kf = KalmanFilter(dim_x=2, dim_z=1)

kf.x = np.array([2., 0.])
kf.F = np.array([[1.,1.], [0.,1.]])
kf.H = np.array([[1.,0.]])
kf.P *= 1.
kf.R = 5

from filterpy.common import Q_discrete_white_noise
kf.Q = Q_discrete_white_noise(dim=2, dt=.1, var=0.1)

zs = [t + random.randn()*4 for t in range (40)]

# Omitted Fs, since dts is not defined anywhere
# Fs = [np.array([[1., dt], [0, 1]] for dt in dts]

(mu, cov, _, _) = kf.batch_filter(zs)
(xs, Ps, Ks, _) = kf.rts_smoother(mu, cov)

This runs but gives me the following plot:

output

What I'm trying to do is to fit an existing curve, like so:

Capture d’écran 2022-10-30 à 11 21 34