Introduction and Overview

This implements a square root Kalman filter. No real attempt has been made to make this fast; it is a pedalogical exercise. The idea is that by computing and storing the square root of the covariance matrix we get about double the significant number of bits. Some authors consider this somewhat unnecessary with modern hardware. Of course, with microcontrollers being all the rage these days, that calculus has changed. But, will you really run a Kalman filter in Python on a tiny chip? No. So, this is for learning.

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.kalman.SquareRootKalmanFilter(dim_x, dim_z, dim_u=0)[source]


x filter state vector.
P covariance matrix
Q Process uncertainty
R measurement uncertainty
H Measurement function
F state transition matrix
B control transition matrix
__init__(dim_x, dim_z, dim_u=0)[source]

Create a Kalman filter which uses a square root implementation. This uses the square root of the state covariance matrix, which doubles the numerical precision of the filter, Therebuy reducing the effect of round off errors.

It is likely that you do not need to use this algorithm; we understand divergence issues very well now. However, if you expect the covariance matrix P to vary by 20 or more orders of magnitude then perhaps this will be useful to you, as the square root will vary by 10 orders of magnitude. From my point of view this is merely a ‘reference’ algorithm; I have not used this code in real world software. Brown[1] has a useful discussion of when you might need to use the square root form of this algorithm.

You are responsible for setting the various state variables to reasonable values; the defaults below will not give you a functional filter.


dim_x : int

Number of state variables for the Kalman filter. For example, if you are tracking the position and velocity of an object in two dimensions, dim_x would be 4.

This is used to set the default size of P, Q, and u

dim_z : int

Number of of measurement inputs. For example, if the sensor provides you with position in (x,y), dim_z would be 2.

dim_u : int (optional)

size of the control input, if it is being used. Default value of 0 indicates it is not used.


[1] Robert Grover Brown. Introduction to Random Signals and Applied
Kalman Filtering. Wiley and sons, 2012.
update(z, R2=None)[source]

Add a new measurement (z) to the kalman filter. If z is None, nothing is changed.


z : np.array

measurement for this update.

R2 : np.array, scalar, or None

Sqrt of meaaurement noize. Optionally provide to override the measurement noise for this one call, otherwise self.R2 will be used.


Predict next position.


u : np.array

Optional control vector. If non-zero, it is multiplied by B to create the control input into the system.


returns the residual for the given measurement (z). Does not alter the state of the filter.


Helper function that converts a state into a measurement.


x : np.array

kalman state vector


z : np.array

measurement corresponding to the given state


Sqrt Process uncertainty


Process uncertainty


sqrt of covariance matrix


covariance matrix


sqrt of measurement uncertainty


measurement uncertainty


Measurement function


state transition matrix


control transition matrix


filter state vector.


Kalman gain


measurement residual (innovation)