4

I have an application continuously receiving speed values (m/s). These values generates some noise and variations. This speed varies all the time, but there is some real changes that is worth noticing. For example when the speed value drops significantly there may be a turn.

Right now I'm using the average of the last X values, where X typically is a number between 5-15. I plot these values in excel to see the difference from the raw data. This works quite well, but the lower the history value is, the less smooth my curve gets. The higher history value, the smoother my curve gets, but then it also reacts late changes and neglects some of them.

I have also tried to to weight the last value in the average calculation. The result is a curve with still plenty of noise, but just a little less than the raw data.

What I'm looking for is a more sophisticated method to filter out the noise which can give me values close to the raw data, but also neglects small variations and noise.

What method could that be or is there anything I could do with my existing setup?

Ole-M
  • 821
  • 2
  • 13
  • 27

3 Answers3

5

For that purpose, Kalman filter is exactly what you need. Here is a free library that implements it: http://kalman.sourceforge.net/

On the other hand, Kalman is quite hard to understand and requires a bit of of research to use it correctly (and implementation is really hard). According to this answer you may find Complementary Filter easier to implement.

Community
  • 1
  • 1
Archie
  • 6,391
  • 4
  • 36
  • 44
  • Been reading a bit about it now. Seems like a complex concept to implement. But can't really figure out what the preconditions are to apply a Kalman filter? Do I need something more than just my raw data? – Ole-M Aug 01 '12 at 12:05
  • 1
    No, you do not require anything more than your data. You just must prepare prediction matrix, and it can be guessed and adjusted just by observing filter behaviour. Also notice, that the filter is designed for _n_ dimensions, and you need filtering only in one dimension (time), and this is much easier. – Archie Aug 01 '12 at 12:36
1

Contrary to what many people say, a simple Kalman filter is not difficult to implement at all in this scenario. In fact, the simplest special case of a Kalman filter is the Exponential filter, which in your C++ code would be:

filtered_speed+=0.02 * (speed_measurement - filtered_speed);

The 0.02 is a tuning constant, and its best value depends on how noisy your measurements are and how fast you expect the actual speed to change. This is also equivalent to a simple IIR filter.

But this is not going to help you much. Kalman filter is not magic and it will not produce much better results than a moving average, unless you give it more information to work with (which will make the above formula more complicated). You can use information like:

  • multiple sensors: can you infer the speed from some other source too? Then you can combine the two measurements using Kalman filter
  • varying sensor noise: is the speed measurement noise usually the same, or is it changing (f.ex. are the measurements as noisy at high speeds as at low speeds). If it is changing, and you can estimate the noise of each measurement, then you can use that information in Kalman filter
  • varying process noise: do you know from some other source whether the speed should be changing fast or slowly at this time? That information can also be used
  • control inputs: e.g. if you are measuring the speed of a car, and you also have data from accelerator pedal or brakes, then Kalman filter can use that too

It's these things that would make Kalman filter better than a simple moving average.

Ahti
  • 136
  • 4
0

The mean is a low-pass frequency filter very simple. May be you can try some others like Chebysheb or Butterworth:

But previously to filter the noise you must know the cut-off frequency of your signal. Then depending on that cuttoff frequency and your filter delay you can have a better filter for your purpose.

You can also usse a Wiener filer if you have a clean signal to compare to: http://en.wikipedia.org/wiki/Wiener_filter

melopsitaco
  • 176
  • 4
  • The problem is that the cutoff frequency may vary in time, so some adaptative filter which can work ad hoc is way better. – Archie Aug 01 '12 at 11:59