Kalman Filter, Example in python



Modern control
Modern Control

Equation of state
Derivation of state equation

observer

Release date:2023/2/11         

 ・In Japanese
<premise knowledge>
 ・Observer
 ・Bilinear transformation
 ・Observable canonical form


■What is Kalman filter?

A Kalman filter is a type of state estimation method using an observer in modern control theory, and even if the input contains noise (called system noise or drive source noise), it is possible to accurately estimate the state by using the Kalman gain. It is different from the disturbance observer.

Here, the explanation focuses on the form of the Kalman filter and an implementation example in python. How to derive the Kalman gain is explained here.

<Normal observer form>
as below. Note that the observer is a function that estimates the state, so it cannot feed back the controlled object by itself. State feedback method using observer is explained here.



<Kalman filter form>
as below. In contrast to the normal observer form, it takes into account noise in the observed values and system. In addition, it is a sequential estimation type that estimates the current value based on the previous estimated value and the observed value.



   

System noise and observation noise are expressed as white noise with a normal distribution with an average value of 0.


■Image of Kalman filter

In order to understand the Kalman filter, formula (2) is transformed as follows.



This is the exponential moving average formula for sequential estimation, which means that the Kalman gain adjusts which of the observed values and the estimated values is more reliable to update the estimated values. And the Kalman gain is (4) below.



The method of deriving the formula is explained here, but to get an idea, think of it as follows. (4) has an observation noise term w in the denominator and a system noise term v in the numerator. If the observation noise is large, the denominator will be large, so the value of the Kalman gain g will be small, and the observed value will not be trusted and the estimated value will be strongly reflected. Conversely, if the system noise increases, the gain g increases, and the estimated value is not trusted, and the observed value is strongly reflected.

<Block Diagram>
Below is a block diagram showing what has been explained so far.


■Example of Kalman filter in python

The Kalman filter is used to estimate the motion of an object with a first-order lag transfer function.



<Convert s-function to z-function>
The above transfer function is transformed by bilinear transformation as follows.



<Convert to observable canonical form>
The observable canonical form of the z function uses the formula explained here. (6) is as follows.


<Python program code>
Now you have all the information you need. Here is the program code.

The simulation results are below. The system noise variance (Q) and observation noise variance (R) are the tuning factors. This time, we set Q=1 and R=5 so that the observation noise is larger. Therefore, the movement becomes insensitive to the observed value.











List of related articles



Modern control
Modern Control

Equation of state
Derivation of state equation

observer