Copyright 2015 Roger R Labbe Jr.

FilterPy library. http://github.com/rlabbe/filterpy

Documentation at: https://filterpy.readthedocs.org

Supporting book at: https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python

This is licensed under an MIT license. See the readme.MD file for more information.

class filterpy.leastsq.LeastSquaresFilter(dt, order, noise_sigma=0.0)[source]

Implements a Least Squares recursive filter. Formulation is per Zarchan [R2121].

Filter may be of order 0 to 2. Order 0 assumes the value being tracked is a constant, order 1 assumes that it moves in a line, and order 2 assumes that it is tracking a second order polynomial.

It is implemented to be directly callable like a function. See examples.


[R2121](1, 2) Zarchan and Musoff. “Fundamentals of Kalman Filtering: A Practical Approach.” Third Edition. AIAA, 2009.



from filterpy.leastsq import LeastSquaresFilter

lsq = LeastSquaresFilter(dt=0.1, order=1, noise_sigma=2.3)

while True:
    z = sensor_reading()  # get a measurement
    x = lsq(z)            # get the filtered estimate.
    print('error: {}, velocity error: {}'.format(lsq.error, lsq.derror))


n (int) step in the recursion. 0 prior to first call, 1 after the first call, etc.
K1,K2,K3 (float) Gains for the filter. K1 for all orders, K2 for orders 0 and 1, and K3 for order 2
x, dx, ddx: type(z) estimate(s) of the output. ‘d’ denotes derivative, so ‘dx’ is the first derivative of x, ‘ddx’ is the second derivative.
__init__(dt, order, noise_sigma=0.0)[source]

Least Squares filter of order 0 to 2.


dt : float

time step per update

order : int

order of filter 0..2

noise_sigma : float

sigma (std dev) in x. This allows us to calculate the error of the filter, it does not influence the filter output.


reset filter back to state at time of construction


Computes and returns the error and standard deviation of the filter at this time step.


error : np.array size 1xorder+1

std : np.array size 1xorder+1