Signal Processing - How to filter out glitches?

Hi Everyone

I am measuring an analogue signal using a teensy board. The teensy board is continuously sampling. Whenever I require a sensor value, I request it over serial using C++ / Open Frameworks.

For my application I require the signal to be as 'clean' as possible.

There are two kinds of noise I have in the signal. a) variance which is close to the signal I am expecting and b) glitches which are way off from what the signal should be.

I am using a relatively aggressive low-pass filter on the teensy board which removes any problem I have with type a noise. However, the glitches are so extreme that they pass through this filter.

I have attempted some rather naive dynamic filtering methods:

  • adjusting the threshold of the lowpass filter to be more agressive the higher the difference between consecutive readings becomes
  • ignoring readings if their difference from the previous reading is too high.

I am unhappy with both these approaches (I can't get the dynamic threshold to behave the way I would like it to and simply ignoring readings if they are too far off feels too aggressive).

I expect that I would need to implement some type of Kalman Filter / Predictive model to do this properly. Can anyone point me towards some beginner friendly resources or code samples which might help me?

My plan in going forward is to try to improve the hardware side of things, but I'd really like to learn to what extent this could be addressed in software. Any pointers would be appreciated.

P.

What about a ‘discard worst’ algorithm? Keep the last 10 readings in an array (circular buffer) ignore the highest and lowest readings and return the average of the remaining 8 readings? Depending on the type of glitches, you may discard the 3 highest, or whatever.

This is probably best fixed on the analog side of things - keep the sensor wires away from the power wires, use snubbers on your motors, maybe add a capacitor-resistor filter network near the analog input.

agreed on the fixing it in hardware suggestion. that's how I usually go about this. I'm just interested in learning more about how to approach this from the software side.

I'll try what you suggested.

See also
http://playground.arduino.cc/Main/runningMedian

problem is always how many samples will fit into the largest glitch…

If you know the max delta two subsequent readings can have it is fairly easy to write an ignore filter, in pseudo code

int myAnalogRead(pin, maxDelta)
{
  x = analogRead(pin);
  if (prevx == NOTINIT) prevx = x;
  if (abs(x - prevx) <= maxDelta) prevx = x;
  return prevx;
}
int myAnalogRead(pin, maxDelta)
{
  x = analogRead(pin);
  if (prevx == NOTINIT) prevx = x;
  prevx = constrain(x, prevx - maxDelta, prevx + maxDelta);
  return prevx;
}

Both versions have their application

Hi Rob

Thanks for the code snippets :slight_smile:

p.