File Exchange

image thumbnail

Kalman Filter Package

version 1.0 (205 KB) by

Implements Kalman Filter, Extended Kalman Filter, Dual Kalman Filter, and Square Root Kalman Filters

19 Ratings



View License

This package implements the following Kalman filters:

1) Standard Kalman Filter
2) Extended Kalman Filter
3) Dual Kalman Filter
4) Square Root Kalman Filter

This package also contains instructive examples for each filter type demonstrating their practical application.

In all 4 cases, the KF functions accept as input noisy samples of a multi-dimensional system and produce the KF estimate of the true system state based on the time-varying process/noise covariances inherent in the noisy samples.

Exponentially-weighted (or unweighted) moving averages are used to estimate the time-varying system covariances from the noisy measurements.

The Standard Kalman Filter is the most basic KF implementation. It assumes a model that the noisy measurements contain the true system state plus white noise.

The Extended Kalman Filter is a generalization of the Standard Kalman Filter that allows the user to specify a nonlinear system model, which is then iteratively linearized during EKF execution.

The Dual Kalman filter simultaneously solves two Standard Kalman filter problems:

1) Fits an auto-regressive model to the data and applies a Kalman Filter to update the AR model

2) Applies the AR model at each iteration before performing the Standard KF update

Square Root Kalman Filters are a more robust and numerically stable method to perform Standard/Dual Kalman Filtering, especially when the covariance matrices of interest are ill-conditioned or nearly not positive definite. The Square Root Kalman Filtering idea is to propagate the process error covariance P in square root form P = U D U', where U and D are iteratively updated and P is not explicitly computed. Doing so will guarantee P is positive definite and thus increase the numerically stability of the KF.

Comments and Ratings (34)

omkar jagan

Thank you @Brian Moore

Can you suggest any reference publication for standardARkalmanfilter.m

Dan Gianotti

I'm having trouble with the StandardARKalmanFIlter. In the first attempt through the filtering loop I get errors because of the i and N values passed to StandardCovEstimate (indexing with 0:i). I'm using an AR(1) model (MA = 0). It looks like an off-by-one sort of problem. Any suggestions?



Nathan Zhang



Danhay (view profile)

Dear Brian,
is there any reference publication I can look into to read on the methods.

Also, am I right to say that the moving average is used for the smoothed_z while the look back value is used for the kalman filter update?

Thank you :)

Brian Moore

Brian Moore (view profile)

@ Mushfiqul: z = measurements, x = state

Dear Brian,
Thanks for the reply.
Is the variable 'z' meant to be state of the EKF or measurement of the EKF ?


Brian Moore

Brian Moore (view profile)

@Mushfiqul: smoothed_z is a moving average (either unweighted or exponentially-weighted) of the input data, and it is computed in the main function (e.g., StandardKalmanFilter.m)

Dear Brian Moore,
The MATLAB package is wonderful. Thank you so much for sharing it with us.

Can you please explain how did you calculate smoothed_z in the StandarCovEst function ?

Thanks, Mushfiqul

Brian Moore

Brian Moore (view profile)

@Xinzhou: Thanks for the feedback! smoothed_z is a moving average (e.g., exponentially weighted) of the input data, z. StandardCovEst.m then computes

Q = covariance of smoothed_z
R = covariance of z - smoothed_z

The above method is a heuristic. The idea is that the Kalman Filter (KF) basically smoothes your data, so I use smoothed_z as a surrogate for the unknown state, and z - smoothed_z as a surrogate for the noise.

If you want a more rigorous algorithm for estimating the covariances (having, e.g., statistical consistency guarantees), you'll need to consult in the KF literature. Any such algorithm will require assumptions on your data, and I didn't want to make any such assumptions in my implementation, so I went with a heuristic.

Hope this helps.


Great work, thank you so much. I have a question on estimating Q and R. "StandardCovEst.m " seems to be the function doing this work. How did you get smoothed_z in the StandardKalmanFIlter function.
It seems that you use this value to calculate the covariance. If you have any reference about this estimation method could you please also provide to me. Thanks.

Brian Moore

Brian Moore (view profile)

@kong xiangchuang: You'll have to modify the code yourself. I intentionally wrote my code with A = I, B = I, etc. for simplicity.

Modifying the code shouldn't be too hard, just refer to one of the many Kalman Filter references in the literature to see where to add the system matrices. For example, I recommend the following easy-to-read paper:

How include A,B,u,H in your code? Thanks a lot!

yuli zhang

well done

Helder pais

Brian Moore

Brian Moore (view profile)

@tverskoj Why the 1 star rating? Please provide feedback so I can improve the submission!

Brian Moore

Brian Moore (view profile)

@Aaron: The Dual Kalman filter is used to implement the auto-regressive Kalman filter (see StandardARKalmanFilter.m)


Aaron (view profile)

Hi, I cannot find the Dual Kalman filter in this package, has it been removed?

Brian Moore

Brian Moore (view profile)

@Mike Good point - I assumed the most basic "white noise" model (A = I, B = I, etc.) for simplicity so that the user can quickly run filter without doing any modeling.

Of course, if you do have a model for your data, you should be able to follow any basic Kalman filter reference to see where to insert the model matrices

Mikes Vines

Mikes Vines

Why did you not include A, B, u, and H in your equations?



Justin (view profile)


Very helpful for me, thanks so much!


any example on structural dynamics problem

Amit Bermano

Easy to use, good default values, readable, great!
Thanks a lot

afu2007 ??

could you tell me the reference for StandardCovEst.m?
thank you!

zhang blue

nice code ,thanks !

W. Chong

Brian Moore

Brian Moore (view profile)

Hi Alexander,

Make sure you add the Kalman Filters directory and *all* subdirectories to your MATLAB path before running any of the scripts.

For instance, to address your specific problem, EWMA() is a function located in "MA Methods" subdirectory. But you will probably get more errors if you don't add the other subdirectories ("Covariance Estimation Methods", "Linear Algebra Methods", and "Square Root Filter Methods") to your path as well.



Getting a problem with Demo2
??? Error using ==> eval
Undefined function or method 'EWMA' for input arguments of type

Error in ==> StandardKalmanFilter at 113
eval(['smoothed_z = ' MAType '(z,MAlen);']);

Error in ==> KF_Demo2 at 41
[x_kf KF] = StandardKalmanFilter(z,MAlen,N);

Youssef Khmou

Youssef Khmou (view profile)

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today