Search code examples
calgorithmembeddedsensorscalibration

how to arrest sudden spike value occurs occasionally in sensor using embedded c


I'm working with sensor with amplifier. I can see sensor is suddenly producing very high value, which is affecting the functionality. I'm looking for some algorithm that will eliminate sudden spike values. I'm using embedded c for this project, please suggest me some algorithm that will eliminate sudden spike values using c program.

I tried previous and current value comparison method but it has not solved my problem


Solution

  • Note that a digital average filter works exactly like a RC filter in hardware: it doesn't eliminate anything, just round it down. If the spikes are part of the expected input, you can use an average filter.

    But if spikes are anomalies that should just get ignored, then to eliminate spikes rather than "down tuning" them, you need to use a median filter. Often called median-3 or median-5 etc depending on how many previous samples you store. The sample rate needs to be long enough that a spike only occurs during 1 sample, or otherwise you are arguably dealing with surges, not spikes - or just sampling too fast.

    How to code them depends on if you are reading digital or analog input.

    Digital input is as simple as storing a sequence of 1 or 0 in the order of appearance. Suppose 0 is the expected value, then you can do a table lookup on 001, the "median-3" value of that is 0.

    For analog input you have to sort the values as they come in. Some examples can be found here: https://embeddedgurus.com/stack-overflow/2010/10/median-filtering/ There are ways to optimize that code further, but it should work as a good introduction.