Filtering Sensor Data With A Kalman Filter - Interactive Matter
Filtering Sensor Data With A Kalman Filter - Interactive Matter
with AVR-Eclipse, AVR-GCC & AVRDude Blinken Button Kit How To Blinken Button for Beginners Kit How To Arduino HTTP Client Library Arduino JSON Library Contact About Imprint / Impressum Projects Subscribe Interactive Matter Tinkering with electronics & ambient interaction
After some unsuccessful results with low pass filters I choose the Kalman Filter. The Kalman Filter involves an awful lot of complicated mathematics. But fortunately I just needed a simple one dimensional Kalman Filter
without any driving information, which makes the filter much easier.
The first two formulas represent the prediction of the Kalman Filter. And since there is no information about the driving forces it is very simple. The second three formulas calculate the measurement update. The variables are for the filtered value, for the process noise, for the sensor noise, for the estimated error and for the Kalman Gain. The state of the filter is defined by the values of these variables. The filter is applied with each measurement and initialized with the process noise , the sensor noise , the initial estimated error and the initial value . The initial values for is not very important since it is adjusted during the process. It must be just high enough to narrow down. The initial value for the readout is also not very important, since it is updated during the process. But tweaking the values for the process noise and sensor noise is essential to get clear readouts.
You see there is nearly no difference between the filtered data (white) and the original data (grey) since you just see the white line. If you turn down the process noise, e.g. to a value of 4 things start to change:
If you look closely you see that the sensor data often overshoots the filtered value. The filtered value is pretty close to the real value, but is missing a lot of noise. For my application I needed a more static output, smoothing out nearly all of the noise, giving a clean an steady signal. So lets turn down the process noise value a bit more, e.g. something like 0.125:
Now we got a quite a clean signal from quite a noisy source. It lags a bit behind the real data, but that is not critical for my application. Compared to the amount of noise reduction it is still quite fast. After this is set lets play a bit around with the sensor noise : We start back at =1:
As expected if we turn down the assumed noise of the sensor the Kalman filter relies more on the sensor data and gives more noisy results. If we increase the noise factor of the sensor to 4 we get a more stable result again:
If we go up extreme and assume a noise level of 32 we get the expected steady result:
As expected the filtered value lags significantly behind the real measurement. But it is a much cleaner signal. But it is questionable if this is still useful.
Implementing Kalman in C
Implementing this filter in C code (e.g. for an Arduino) is quite simple. First of all we define a state for a kalman filter:
typedef struct { double q; //process noise covariance double r; //measurement noise covariance double x; //value double p; //estimation error covariance double k; //kalman gain } kalman_state;
kalman_state kalman_init(double q, double r, double p, double intial_value) { kalman_state result; result.q = q; result.r = r; result.p = p; result.x = intial_value; return result; }
And a function to update the kalman state, by calculating a prediction and verifying that against the real measurement:
void kalman_update(kalman_state* state, double measurement) { //prediction update //omit x = x state->p = state->p + state->q; //measurement update state->k = state->p / (state->p + state->r); state->x = state->x + state->k * (measurement - state->x); state->p = (1 - state->k) * state->p; }
Tod E. Kurt December 18, 2009 at 23:32 Great article. What kind of lag (in samples I guess) are you seeing? Reply
Marcus December 19, 2009 at 09:05 I employed a very damped filter in one of my designs and you can see that the readout takes a bit longer to accept reality. You can really see how the value is updated. But this is with a very agressive filtering. and it is far less extreme as it was with a simple low pass filter. Reply
John Honniball December 18, 2009 at 23:18 This is a really useful and informative article! Ive never used a Kalman filter, although Ive heard of it many times. In a sonar filtering program that I worked on some years ago, we used an Infinite Impulse Response filter, which is a bit like a moving average. Its very simple, both mathematically and as an implementation. I wrote one recently to make a VU-meter style display using an Arduino I used the IIR filter to make a smoothly decaying peak reading dot on a line of LEDs. Reply
SiliconFarmer December 19, 2009 at 00:30 In addition to IIR, there is the Finite Impulse Response filter, FIR, to consider. You should be able to design an FIR to clean up that signal, using formulas instead of trial and error for the number of taps and coefficient. But perhaps thats what you used to create the low-pass filter that didnt do the job for you. I havent done an FIR on an AVR processor. It might not have the horsepower, depending on the number of taps you would need. Reply
A very pragmatic post on a broad, deep topic. It is easy to find numerous articles, research papers and textbooks dealing with Kalman filters and its variants, but its a rarity to find a practical, application oriented article. I guess most people believe they understand the underlying statistics and know how to readily program such a filter if they can name textbooks or make smart comments about taming R. E. Kalmans work. Well done! Michael. Reply
Marcus December 19, 2009 at 09:09 Thanks, to be honest. I understand not very much of the filter. I just wanted to apply it. Multidimensional problems would be much harder and woul take much longer time. I just tried to filter my data and it worked. Reply
Bala January 30, 2010 at 01:54 Hi Marcus, I am also trying to filter sensor data but no luck with the low pass filter. Can you please post your code for the Kalman filter?? Thank you.. Reply
Marcus January 31, 2010 at 09:41 Ok, for further references I have included the necessary pieces of code for you. Reply
Rohit March 9, 2010 at 04:51 Hey man, great article and I am actually trying to implement kalman filtering of accelerometer data with about 1.5 gs of sensitivity on an arduino. H/e, I was just wondering what you did to get the accelerometer data onto a
Marcus March 9, 2010 at 08:06 The graph is done with a processing script. Basically with the Arduino I print out the numbers to the serial port and read them with my processing script and plot them as a graph in processing. You can download the scipt here. Reply
Rene July 16, 2010 at 03:20 Hi Marcus! Great article! you write that you used processing for the graph plots? The link you gave seems to be code for a BMP85? where and how do you plot the serial data? Sorry, i am new to processing. thanks so much, -r
Sebastian Stark April 26, 2010 at 18:44 Your Kalman filter is in fact a variation of an even more simple filter: if you skip all the q,r,p stuff and just leave k and set it to a value 0<ax = state->x + 0.06 * (measurement state->x); would do it. You could also calculate it this way: (b = 1-a; a = 0.06; b = 0.94) state->x = b*state->x + a*measurement; Reply
Marcus April 26, 2010 at 19:11 OK, never seen it that way. Is there an official name for that filter variant? Thanks for the hint. Nevertheless reducing the Kalman to the max gave me quite a good insight into the kalman filter. Perhaps this oversimplification helps somebody, too.
Reply
Sebastian Stark April 26, 2010 at 20:06 http://en.wikipedia.org/wiki/Low-pass_filter#Algorithmic_implementation I think its a simple Low-Pass Filter now :) Reply
Marcus April 26, 2010 at 21:35 Ouch ! I have overseen it. Whatever another lesson learned. Next time I will do a real kalman filter! With several dimensions! And proper calculations ;)
jobby July 12, 2010 at 10:04 hai, generally noise is a high freuency signal. even a low pass filter can perform the function of removing noise, then what makes the kalman filter differ from low pass filter. could u pls make it clear? Reply
Marcus July 12, 2010 at 11:11 Yes, you are right. In this special case the Kalman Filter makes not so much sense. I will post a follow up soon, with a real kalman application: Filtering data from multiple sensors. This is where the real power of the Kalman Filter lies. But even though this was a useless implementation I at learned at least how to apply a Kalman filter, perhaps someone else learns also something by this fruitless approach. Reply
kalmannoob November 24, 2010 at 22:07 sir, im trying to implement my own kalman filter but ive had a trouble understanding these parts: 1. First I looked into the importance of the process noise q, starting by a very high value of 128 (which is the maximum output of the accelerometer) > how did you know that 128 is the maximum?
2. If we go up extreme and assume a noise level of 32 > same as my first question, how did you know that this is the maximum Reply
Marcus November 25, 2010 at 09:09 128 was the maximum output of the accelrometer at 1g so assuming the same level of noise as the output value is a extreme measuer. The same is true for 32 since it is a quarter of the maximum output level. A realistic value would be lower than 32 since we can assume that the sensor is not that noisy. If I would know it a bit better I would talk about signal to noise ration in dB and so on. Reply
kalmannoob November 29, 2010 at 22:10 ive implemented my own 1-d kalman. during testing though, ive noticed that even when stationary i get a huge error. for the first few 20 or so seconds i do okay but past that i get HUGE values such as this 18604.000000. any idea as to why? Reply
Marcus December 1, 2010 at 11:14 Sorry, no. It seems that you got a problem with either the implementation itself or the data format. Or something completely unrelated. Reply
Samantha April 10, 2011 at 10:36 Hi, just wanted to thank you for your detailed post; Im a university student studying how to use the Kalman Filter and your post has helped a lot thanks :) Reply
Reply
bolke May 31, 2011 at 10:21 Thanks for the article. I couldnt care less what its called, kalman or low pass, it works. Filtering like whale in the sea. :) Reply
Marcus July 16, 2010 at 08:11 Yes, you are right, I did not post the link to the processing sketch. But you can find it on github. Reply Leave a Comment Name * E-mail * Website
Submit
{ 1 trackback } Messwerte filtern thewknd Previous post: Decoupling LIS302DL There I fixed it! Next post: Seasonal Greetings
To search, type and hit enter
Your Cart
Popular Projects
TVBG - World Smallest TV-B-Gone clone Space Invaders Button Gravitron SkatePOV How was your day, darling?
Popular Posts
Filtering Sensor Data with a Kalman Filter Developing Software for the Atmel AVR with AVR-Eclipse, AVR-GCC & AVRDude Arduino & AD7746 Driving Circuits from a CR2032 Lithium Coin Cell SMT Soldering Tutorial
Recent Posts
Blinken Button for Beginners Version 2 Twitballoon 2 Blinken Button as a SPI Display Blinken Button revisited Twitballoon a Twitter Trend Visualizer
Recent Comments
TVBG World Smallest TV-B-Gone clone moderateschaltung on TVBG World Smallest TV-B-Gone clone Marcus on Arduino HTTP Client Library Marcus on Arduino HTTP Client Library Mikkel on Arduino HTTP Client Library Mikkel on Arduino HTTP Client Library
License
This work by Interactive Matter is licensed under a Creative Commons Attribution-ShareAlike 3.0 Germany License. Permissions beyond the scope of this license may be available at http://interactive-matter.org/contact. Get smart with the Thesis WordPress Theme from DIYthemes.