Click here to Skip to main content
11,805,604 members (47,302 online)
Click here to Skip to main content

Kalman Filtering: Part 2

, 9 Oct 2012 CPOL 25.2K 2.2K 33
Rate this:
Please Sign up or sign in to vote.
A simple example of Kalman filtering and merging data.

Demo image


This is a sequel to the previous article on Kalman filtering, and can be thought of as a more elaborate and more useful example. Kalman filters do a particularly good job of adaptively removing noise from a signal with as little distortion as possible. This example is for 2D navigation using a GPS and an inexpensive accelerometer.

Suppose you wanted to mow the lawn, or have a robot drop off a letter, or navigate very accurately off road. GPS provides inaccurate position and velocities (2.5 m rms, 10 cm rms, respectively) and using position and velocity a 2D Kalman filter will improve things.

Using only GPS means at best the signal to noise improves by averaging (sqrt(1/n)) and at 5 to 10 Hz the accuracy degrades rapidly with course changes. GPS also will “jump” in location as different satellites are periodically chosen to be added / removed from the solution. This jump is sometimes 10-15 meters. The velocity from GPS (computed from Doppler) has less noise, and “jumps” less when the satellite constellation changes.

The solution is to add another data source. There are several accelerometers available that can measure the linear acceleration. Simply put, they provide the rate of change in velocity, and after a coordinate transform, an do so in the same coordinate space as the GPS. In addition to a linear accelerometer, an angular with a second GPS would provide the course and further refine the solution, but for this example a linear accelerometer should be sufficient for a dramatic improvement. Relying only on the accelerometer causes a drift as noise and round off integrate.

The ideal thing about the accelerometer is the noise is small (0.002 m rms), it can be measured rapidly (1Hz – 5kHz), the errors are Gaussian (average out) and the error sources are completely different than GPS. Because there is no connection in noise source, the accelerometer and GPS error sources tend to cancel out.


Assuming we can get decent HW (below are very available HW specs at reasonable prices).

GPS                2.5 m rms, 10 Hz
Accelerometer      2 mm rms, 1 kHz

Operational goal:

Goal               <25 cm rms for low dynamic movements


The source code could have used a 3D Kalman filter (position, velocity, acceleration) but there is no real correlation between the GPS and the acceleration. It is simpler to use two filters and optimize each separately. One filter computes the velocity as a 2D Kalman (velocity, acceleration) such that the GPS Doppler is smoothed / corrected by the acceleration measurements. A second filter takes the highly accurate velocity information and filters in position. To get this to work in the horizontal plane, two filters for X (position, velocity) and two for Y (position and velocity) were used.

The test scenario is accelerating from a stop, cruising, slowing down, rotating, slow to stop, then start up and cruise back.


The stock coefficients provided yield good accuracy balanced with good responsiveness: once acceleration is detected it immediately helps shift the position, the GPS slowly corrects against drift. I’m hoping to use this for a robots project in the future, and I’m hoping it helps others out there.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) KMC Systems
United States United States
Phil is a Principal Software developer focusing on weird yet practical algorithms that run the gamut of embedded and desktop (PID loops, Kalman filters, FFTs, client-server SOAP bindings, ASIC design, communication protocols, game engines, robotics).

In his personal life he is a part time mad scientist, full time dad, and studies small circle jujitsu, plays guitar and piano.

You may also be interested in...

Comments and Discussions

QuestionKalman in IMU/AHRS Pin
winx9-Sep-14 4:30
memberwinx9-Sep-14 4:30 
QuestionBest GPS based system example souce code Pin
kasun61810-Oct-13 22:29
memberkasun61810-Oct-13 22:29 
GeneralMy vote of 5 Pin
kainraz13-Aug-13 5:36
memberkainraz13-Aug-13 5:36 
GeneralAwesome Article - Thankyou Pin
David Shields11-May-13 23:50
memberDavid Shields11-May-13 23:50 
GeneralRe: Awesome Article - Thankyou Pin
HoshiKata12-May-13 4:10
memberHoshiKata12-May-13 4:10 
GeneralRe: Awesome Article - Thankyou Pin
David Shields12-May-13 15:17
memberDavid Shields12-May-13 15:17 
GeneralRe: Awesome Article - Thankyou Pin
HoshiKata12-May-13 16:39
memberHoshiKata12-May-13 16:39 
GeneralRe: Awesome Article - Thankyou Pin
David Shields13-May-13 0:47
memberDavid Shields13-May-13 0:47 
GeneralRe: Awesome Article - Thankyou Pin
David Shields14-May-13 23:58
memberDavid Shields14-May-13 23:58 
QuestionFiltering without accerelometer Pin
Member 260151319-Dec-12 6:30
memberMember 260151319-Dec-12 6:30 
AnswerRe: Filtering without accerelometer Pin
HoshiKata19-Dec-12 6:41
memberHoshiKata19-Dec-12 6:41 
AnswerRe: Filtering without accerelometer Pin
HoshiKata22-Apr-13 11:49
memberHoshiKata22-Apr-13 11:49 
Questionplease help Pin
eclipse20045-Dec-12 20:18
membereclipse20045-Dec-12 20:18 
AnswerRe: please help Pin
HoshiKata22-Apr-13 11:52
memberHoshiKata22-Apr-13 11:52 
Questionreal example Pin
Stephan Brunner20-Nov-12 9:19
memberStephan Brunner20-Nov-12 9:19 
QuestionDemo with Kalman3D ? Pin
x8935-Nov-12 0:02
memberx8935-Nov-12 0:02 
AnswerRe: Demo with Kalman3D ? Pin
HoshiKata7-Nov-12 13:48
memberHoshiKata7-Nov-12 13:48 
AnswerRe: Demo with Kalman3D ? Pin
HoshiKata22-Apr-13 11:50
memberHoshiKata22-Apr-13 11:50 
QuestionCan't download the zip file either ? Pin
BigAlBuchanan8-Oct-12 14:23
memberBigAlBuchanan8-Oct-12 14:23 
AnswerRe: Can't download the zip file either ? Pin
HoshiKata9-Oct-12 6:15
memberHoshiKata9-Oct-12 6:15 
QuestionThe zip file cannot be downloaded Pin
wvd_vegt3-Oct-12 0:41
memberwvd_vegt3-Oct-12 0:41 
AnswerRe: The zip file cannot be downloaded Pin
HoshiKata9-Oct-12 6:15
memberHoshiKata9-Oct-12 6:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 9 Oct 2012
Article Copyright 2012 by HoshiKata
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid