Skip to content

Latest commit

 

History

History
176 lines (139 loc) · 7.32 KB

File metadata and controls

176 lines (139 loc) · 7.32 KB

Kalman filter

Theoretical background

LTI system

The implementation of the code is based on the tutorial from this site.

The Kalman filter starts from the state-space representation of a linear time invariant (LTI) system:

figures/lti-system.png

The corresponding discrete form is written as:

figures/lti-system-discrete.png

The transformation of the continuous and discrete state-space representations can be given by:

figures/continuous-to-discrete.png

The measurement of the system is given by:

figures/measurement-of-lti-system.png

In the concept of Kalman filter, additional uncertainty is introduced to the state-space representation and observation of the system:

figures/lti-system-with-uncertainty.png

Kalman filter

The Kalman filter is acutally a state observer of the LTI-system.

The Kalman filter operates in a “predict-correct” loop. In the “predict” step, the estimation of the state vector of the next timestep is calculated along with its unceratinty. And in the “correct” (or “update”) step, the measurement of the output vector is obtained and it is used to update the predicted state vector and its unceratinty.

After given an inital estimation of the state vector and its unceratinty, the Kalman filter is able to make its first prediction. Then, after figures/dt.png, the measurement of the system output is obtained, the Kalman filter can correct its last prediction by the measured data, get the updated state variables, and make new prediction for the next timestep. The following table provides an intuitive explaination of how kalman filter works:

<c><c><c>
indextimestepoperation
00initial estimation of figures/x00.png and figures/P00.png
10predict: make predictions of figures/x10.png and figures/P10.png
21get the measured system output figures/z1.png and its unceratinty figures/R1.png
31update: update current state figures/x11.png and unceratinty figures/P11.png
41predict: make predictions of figures/x21.png and figures/P21.png
52get the measured system output figures/z2.png and its unceratinty figures/R2.png
62update: update current state figures/x22.png and unceratinty figures/P22.png
72predict: make predictions of figures/x32.png and figures/P32.png
3n-1nget the measured system output figures/zn.png and its unceratinty figures/Rn.png
3nnupdate: update current state figures/xnn.png and unceratinty figures/Pnn.png
3n+1npredict: make predictions of figures/xnp1n.png and figures/Pnp1n.png

The corresponding equations for the kalman filter are listed below.

  • update:

figures/update.png

  • predict:

figures/predict.png

Note that the last equation in the prediction procedure can also be written as: figures/simplified-corvariance-update.png

Obviously, it is possible to update the state vector several times before it makes its prediction if the Kalman filter receives several measurements at one timestep.

Usage

The Kalman filter is implemented in kalman.py. This file contains a class Kalman, which constructs a kalman filter for a system with state-space representation.

Initialization

The state transition matrix F, input transition matrix G, and obervation matrix H can be defined when creating the Kalman instance. These three matrixes can also be defined or modified after the class instance is created. That is:

kalman_filter = Kalman(F, G, H)

or

kalman_filter = Kalman()
kalman_filter.F = F
kalman_filter.G = G
kalman_filter.H = H

Note that it is not necessary to set G if the system does not have input.

The state vector and its uncertainty matrix can be accessed by attribute x and P. The initial values of these two variables should be manually defined after instantiation:

kalman_filter.x = x
kalman_filter.P = P

Predict and update

After setting the system matrixes and initial values, the filter is able to predict or update the state vector of the system.

To make predictions, the member function predict can be called with control input u and its covariance matrix Q:

def predict(self,
            u: npt.ArrayLike | None = None,
            Q: npt.ArrayLike | None = None
            ) -> Kalman: ...

If the system has no control input, u can be set to None. If Q is not given, the lastest setted value for Q will be used.

To update the state vector and its uncertainty, the member function update can be called with measured output z and its uncertainty matrix R:

def update(self,
           z: npt.NDArray,
           R: npt.NDArray | None = None
           ) -> Kalman: ...

If R is not given, its latest setted value will be used.

Filter property

The following attributes of the kalman filter can be obtained by direct access to its property name:

namedescriptioncomment
Fstate transition matrixcan be set at any time
Ginput transition matrixcan be set at any time
Hobservation matrixcan be set at any time
xstate vectorcan only be set once
Pstate vector covariancecan only be set once
KKalman gain matrixread-only

Examples

Numerical examples concerning the Kalman filter are given in examples/examples.py. Here are some snapshots.

  • The temperature of heating liquid

examples/example8-1.svg examples/example8-2.svg

  • The position of a moving vechicle

examples/example9.svg

  • The altitude of a rocket:

examples/example10-1.svg examples/example10-2.svg