4.46377

4.5 | 70 ratings Rate this file 1513 downloads (last 30 days) File Size: 7.16 KB File ID: #5377

Learning the Kalman Filter

by Michael Kleder

 

30 Jun 2004 (Updated 12 Oct 2005)

No BSD License  

Basic Kalman filter, heavily commented, for beginners to Kalman filtering.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

Download Now | Watch this File

File Information
Description

When I first studied Kalman filtering, I saw many advanced signal processing submissions here at the MATLAB Central File exchange, but I didn't see a heavily commented, basic Kalman filter present to allow someone new to Kalman filters to learn about creating them. So, a year later, I've written a very simple, heavily commented discrete filter.

Acknowledgements
This submission has inspired the following:
Learning the Kalman Filter in Simulink, Learning the Extended Kalman Filter, Kalman Filter Tutorial, Learning the Unscented Kalman Filter
MATLAB release MATLAB 5.3.1 (R11.1)
Other requirements None.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (102)
23 Sep 2004 Giap Do van

great!

05 Oct 2004 Vassilios Moussas

Compact, well documented, very good initialization and use of structures.

29 Nov 2004 Thomas Byrne

Excellent!! Thank You.

11 Jan 2005 Hooman Dejnabadi  
14 Jan 2005 Simon Tippler

Very helpful. Thanks!!

21 Jan 2005 Giuliano Scimone  
25 Jan 2005 ARLINDA SAQELLARI  
23 Mar 2005 Liu Baolong

You are a great tutor !
I really appreciate it .
Thank you very much.

23 Mar 2005 Nathir Rawashdeh

This is a very good example and shows how easy it is to apply the Kalman filter. It does, however, require some background knowledge. It would be nice to have a more complicated example with non-zero u and where H and A are not =1. Thanks Michael!

30 Mar 2005 Daniel O. Fufa

very good
Thanks Daniel
Aalborg university

19 May 2005 ARLINDA SAQELLARI

very good, I like the idea

16 Jun 2005 Flop Flop  
29 Jun 2005 Tim Gebbie

Very fun. Very neat.

30 Jun 2005 lee yk

Would you like to give me sample 's' value?
I still don't understand it perfectly.

08 Jul 2005 Rentian Xiong

Nice work. It would be better if there is an example for vector state. Also it would be very cool if someone can put Kalman filter algorithm in simulink so that we can see the estimation of states dynamically. And of course, an extended kalman filter for nonlinear system would be also very useful.

10 Jul 2005 ammar saleem  
03 Aug 2005 Tsanko Tsankov

A good try to explain something. Keep up the good work! There are, however, some mistakes (at the autoinitialization step). Anyway I still don't quite understand the use of Kalman filter. The given example is good, but I'm still confused how to apply this filter to my data.

21 Oct 2005 shobi kumar

excellent apporach
but i m unable to run this prog
plz help me

29 Oct 2005 Zahid Ullah Khan

Its nice, no doubt.

15 Nov 2005 nemesio CARDENAS LOPES

ok

20 Nov 2005 Rafal G.

Simple, pretty, excellent :-))) Thanks a lot!!

25 Dec 2005 Mehdi Sanaatiyan

Well,it's good for first time.No at all

07 Jan 2006 ALEXANDRE EDUARDO

VERY GOOD

07 Jan 2006 Robert Kaddu

Very well presented summary that makes more sense than that provide within the Matlab help function.

12 Jan 2006 Carlos Roldan  
13 Jan 2006 Xuewu Dai

Very Useful for understanding Kalman Filter

20 Jan 2006 akher falcon

its great.

09 Feb 2006 Eduardo Veras  
15 Feb 2006 Franz Dietrich

Useful Comments in Code.

22 Feb 2006 swami nathan  
29 Mar 2006 Camilo Lozoya  
06 Apr 2006 Carlos Orduno

The best source I've found to start working with Kalman Filters. Do you have anything for the nonlinear version?

13 Apr 2006 Indiana Jones  
23 Apr 2006 Teo chai

I try to run the "learning the kalman filter" in the matlab but i unable to run it.
May i know how to run the file? thks

26 Apr 2006 Bing Li

try to replace inv(A)*B by A\B to speed up, although it's not significant when the matrix size is small

14 Jun 2006 ti toe

you have to set up matlab first and run with workspace

15 Aug 2006 Colin O'Flynn

Thank You! Great introduction to the Kalman filter, even if you don't use Matlab.

27 Aug 2006 diop bara

good

17 Oct 2006 shao litang

I need Kalman Filter program.

26 Oct 2006 clayonjj Harrison

i like the explanation but I cant run the file the following error pops up.HELP!!

 
kalmanf
??? Input argument "s" is undefined.

Error in ==> kalmanf at 150
if ~isfield(s,'x'); s.x=nan*z; end

29 Oct 2006 Priyanka Gupta

I get the same error:

kalmanf
??? Input argument "s" is undefined.

Error in ==> kalmanf at 150
if ~isfield(s,'x'); s.x=nan*z; end

01 Nov 2006 MORSHED MAHMUD  
14 Nov 2006 Tansel Yucelen

Same Error !!!

30 Nov 2006 ravindar reddy  
11 Dec 2006 karim kiko

It will be better if you separate the comment from the m.file and try to add them as "help" in a pdf format.

08 Jan 2007 u v  
13 Mar 2007 Way Jch  
13 Mar 2007 v ram  
28 Mar 2007 fatemeh shoormij

discrete kalman filter

29 Mar 2007 Rajesh Krishnan  
25 Apr 2007 xu zheng

very good tools
thank you very much

26 Apr 2007 lai zuomei

a good demo for me!

29 Apr 2007 Rodrigo Badínez

Good demo.
You probably should separete the example, to another m file, named like RunMeDemoKalman.
For the really beginners.

09 May 2007 zhu Yi Yong zhuyiyong

very good

15 May 2007 Edward Taylor

Very easy to use, recommended.

23 May 2007 SOURAV DAS

 It is a very user friendly, recommended

29 May 2007 mehmet ali arabaci

Actually, I am not a beginner at Kalman filter issue. But, i think this is a very useful tool and i wish i got this m-file when i first started to work with Kalman. Because, it is very important for the beginners to have the simplest form of the problem and to see its solution with a simulation program.

07 Jun 2007 Bouchemmella abdelhalim

I want this refference

10 Jun 2007 sk imtiaj

this is very good

14 Jun 2007 X. King  
11 Jul 2007 hamid reza ghazizadeh

how con I find calculation of dry gas filter
for natural gas ? please

04 Oct 2007 Duong Minh Au  
05 Oct 2007 Utkarsh Gaur  
19 Oct 2007 djeunang brigitte

i want a kalman filter

06 Nov 2007 P. McNamara

Be careful. Also we are looking at your downloads records.

27 Nov 2007 wang yi

very good

28 Nov 2007 Randy Coleman  
27 Dec 2007 Arsalan Khan

I can understand Kalman filter from this document, I bet anyone can.

31 Dec 2007 James Hokanson  
04 Jan 2008 marc luc  
25 Jan 2008 Yi Cao

This is a very popular file in the File Exchange. The function itself is excelent. However, I just noticed that the example provide with the file is not correct. Somehow, it is misleading to beginers.

In line 131, the process is defined as:
true(end+1) = randn*2 + 12;
i.e. the state is a constant plus a noise. If so, the process in the standard state space form should be:
x(k+1) = 0 * x(k) + 12 + w(k)
i.e. s.A = 0; s.B = 1; s.u = 12;
However, in the file it is wrongly defined as:
s.A = 1; s.B = 0; s.u = 0;
The difference is that the process noise is not dynamically cumulated in the former definition but does in the later.

03 Mar 2008 mohamed pumma

ok

22 Apr 2008 bekir pasaoglu

great

27 Apr 2008 Sahil Ganguly

I keep getting this error,can someone explain what i'm doing wrong?

kalmanf ??? Input argument "s" is undefined. Error in ==> kalmanf at 150 if ~isfield(s,'x'); s.x=nan*z; end

03 Jun 2008 Behnam Molaee Ardekani

Good for those who want to see what the Kalman Filter is for the first time.
It works and there is a simple example in the m file.

19 Jun 2008 Sid Saraiya

This was extremely useful for a beginner like me. A great post!

03 Jul 2008 Imtiaz Hussain

Very Useful Indeed

13 Jul 2008 Vipin Gupta

Thanks for sharing. :)

21 Jul 2008 Tim Davis

Nice comments. Awful numerics. Why would you consider multiplying by the inverse? That's horribly inaccurate and slow. Replace

x = inv(H)*z
P = inv(H)*R*inv(H')

with, at least

x = H\z
P = (H\R)/H'

likewise

K = P * H' * inv (lots of stuff)

should be

K = P * H' / (lots of stuff)

further more, if H is not changing, it should be factorized just once and the factors kept (LU or CHOL).

26 Sep 2008 mohammad khorrami arani

Thanks

11 Nov 2008 Prasetyo Utomo

Thanks very much

26 Jan 2009 Michael Jordan  
06 Apr 2009 benouis mohamed

thanks

24 Jun 2009 Shivaram

Excellent KF implementation I have ever seen!

24 Jul 2009 naini naveen

very good

13 Jan 2010 RAJA MOHD ZAIDI RAJA IBRAHIM

hi,

is there anyone got the right solution in learning Kalman Filter.
Could you email file.m to me, rmzaidi8@gmail.com
I've run the file given but have alot error.
Please help me.
Thank.

03 Mar 2010 Gervasio

Very Helpful, although i would like to see an example with a control vector u.

18 Mar 2010 Etienne ARNAL

Great job !

29 Mar 2010 vu

good

01 Apr 2010 chen

thank your

21 May 2010 Examples Learn By

Thank you for your hard work
http://learnbyexamples.org/category/matlab

06 Jun 2010 Steven  
06 Jun 2010 ?

Thanks very much.

08 Jun 2010 John D

I get an error during the correction step.
>> s.x = s.x + K*(s.z-s.H*s.x);

Error using==>mtimes
Inner matrix dimensions must agree

The error makes sense given the size of my matrices but I don't see how they could be wrong. Is this code expected to not work for a system with two observer inputs (position and speed)?

Thanks for the help!
ps: let me know if you need more info - tried to keep it short.

09 Jun 2010 John D

[continued from poste above]

I have solved the problem by modifying the line to;

>> s.x = s.x + K' * (s.z-s.H*s.x); %added transpose of K

This was required as my observance vector (z) was a 2X1 matrix(and so is K and hence they couldn't be multiplied). Although the calculation now works I am still wondering why it doesnt work with the original code.

09 Jun 2010 John D

[continued from poste above]

As expected my matrices were wrong!

Thanks for the example!

10 Jun 2010 Venche

sehr gut!

12 Jul 2010 Erdal Bizkevelci  
21 Jul 2010 joybing

thank you for sharing!

11 Aug 2010 Big Andy  
17 Aug 2010 Marc  
19 Aug 2010 Steve G

Very clean example of KF, but not general enough to deal with state vectoc. For example, s.x = inv(s.H)*s.z; and s.P = inv(s.H)*s.R*inv(s.H') would not work if number of state and number of measurement are not the same.

Please login to add a comment or rating.
Updates
12 Oct 2005

comment change

Tag Activity for this File
Tag Applied By Date/Time
filter design Michael Kleder 22 Oct 2008 07:25:28
filter analysis Michael Kleder 22 Oct 2008 07:25:28
linear Michael Kleder 22 Oct 2008 07:25:28
discrete filter Michael Kleder 22 Oct 2008 07:25:28
basic kalman filter Michael Kleder 22 Oct 2008 07:25:28
kalman filter Michael Kleder 22 Oct 2008 07:25:28
linear juan 19 Nov 2008 10:39:38
basic kalman filter Md Rezaul Karim 22 Nov 2008 20:45:33
basic kalman filter Nick 16 Dec 2008 18:23:44
kalman filter SHYNESH S 13 Jan 2009 00:11:18
basic kalman filter mark 28 Jan 2009 15:34:09
basic kalman filter andy Ñ 12 Mar 2009 12:54:35
kalman filter andy Ñ 12 Mar 2009 12:54:46
basic kalman filter Joshua Malina 05 May 2009 12:09:31
kalman filter Nikola Kralj 01 Jul 2009 16:56:54
imtransform Jianglin MA 14 Jul 2009 09:52:28
imtransform Grigory 27 Jul 2009 11:15:57
basic kalman filter lev kadyshevitch 30 Jul 2009 16:16:54
kalman filter Matthew Russell 11 Aug 2009 11:51:13
basic kalman filter Kevin 27 Aug 2009 09:05:30
discrete filter Kevin 27 Aug 2009 09:05:33
linear Kevin 27 Aug 2009 09:05:39
filter analysis Kevin 27 Aug 2009 09:05:50
basic kalman filter yasmin begum 19 Sep 2009 03:24:02
imtransform yasmin begum 19 Sep 2009 03:24:13
kalman filter yasmin begum 19 Sep 2009 03:24:24
basic kalman filter Travis Perry 28 Sep 2009 20:53:42
basic kalman filter Shamail Saeed 16 Nov 2009 00:09:34
kalman filter Stefan 26 Nov 2009 08:26:34
basic kalman filter Ali 15 Dec 2009 09:07:36
basic kalman filter viswanath devalla 15 Jan 2010 05:51:59
discrete filter Mateusz 31 Jan 2010 11:09:11
basic kalman filter Manuel Ballesteros 01 Feb 2010 16:59:28
basic kalman filter Jonas Pesente 16 Feb 2010 14:12:34
discrete filter Jonas Pesente 16 Feb 2010 14:12:51
basic kalman filter d v 19 Feb 2010 02:30:47
basic kalman filter Bill 14 Mar 2010 03:26:24
fft QQ ? 17 Mar 2010 03:25:49
basic kalman filter Diego Fernandez 27 Mar 2010 14:56:20
basic kalman filter W Thielicke 11 May 2010 04:38:14
basic kalman filter Kathryn 21 May 2010 09:07:55
basic kalman filter swandoyo hartono 21 May 2010 16:46:10
basic kalman filter Jim 23 May 2010 18:16:13
basic kalman filter Salvador 01 Jun 2010 12:46:35
kalman filter Jose Ercolino 06 Jun 2010 18:36:58
basic kalman filter Christian 07 Jun 2010 03:56:38
basic kalman filter Mirel Vasiliu 08 Jun 2010 02:53:49
basic kalman filter Tseng Tseng 08 Jun 2010 08:06:57
basic kalman filter John D 09 Jun 2010 03:49:04
discrete filter John D 09 Jun 2010 03:49:07
discrete filter Tseng Tseng 09 Jun 2010 09:57:08
basic kalman filter Felix Niederer 22 Jun 2010 09:19:51
kalman filter kuoping 27 Jun 2010 23:49:28
basic kalman filter Paul Hampikian 23 Jul 2010 14:41:59
kalman filter Ng MY 15 Aug 2010 05:06:28

Contact us at files@mathworks.com