# Kalman filter : simple code example

I read lots of things about Kalman filtering, but in order to fully understand it, I would probably need to see it working on some data.

Would you have a minimal example (Python code or any other language) showing what it does on some real data $x[n]$, where $n$ is the time?

I created a simple example with Scilab/XCOS. It is a simulation of lowpass filter with noise. It is observed by a kalman filter. You can also insert some uncertainties in the system model. With XCOS you can simulate the system. The results will be plotted automatically. Here is the Downloadlink.

Little help with scilab: Go to the directory with standard unix command

cd /home/workingdir/Kalman_Example


There you should execute getd() to load all functions (.sci-files) in the directory. Then you can execute the modeling.sce file, which is just an initializing script:

exec('modelling.sce')


Then typexcos simulation_kalm1.zcos into the console. A graphic window will open. Press the play button and the simulation will start.

• With the console you can go to the directory with standard unix command cd /home/workingdir/
– peng
Jan 10 '16 at 21:25
• What does exec(modeling.sce) do? It should load initial variables to the workspace. The workspace is shown in the right side of the console window.
– peng
Jan 10 '16 at 22:57
• 1) Go to the dir. [with ls you see all necessary files?] 2) getd() to load fuctions 3) exec(modeling) to execute the script - the results appear at the console and in the workspace? 4) xcos simulation_kalm1.zcos opens the XCOS simulation 5) Press Play-Button starts the simulation and the graphs appear in a plot. Which step makes problems?
– peng
Jan 10 '16 at 23:18
• Now you can a) vary the noise covariance (or the assumtion about it in the kalman code) and you can add some uncertainties in the plant transfer function or in the state space model. I hope, that helps to understand, how it works.
– peng
Jan 11 '16 at 10:18

If R is OK to use, then try the various answers I've made here.

Kalman and Bayesian Filters in Python is interactive book about Kalman filter. It contain a lot of code on Pyhton from simple snippets to whole classes and modules. For simplest example see chapter about one dimentional Kalman filter.

but in order to fully understand it, I would probably need to see it working on some data

P.S. Also in my opinion there is not enought to see Kalman filter example to understand it. You also need understand a problem domain (process model).