I have a sonar sensor reading water level by measuring distance to the water's surface and sending back as a floating point number.
It takes single measurement at fixed intervals and sends them right after.
but every once in a while , due to environmental factors, there is a spike in the reading which does not match the physical quantity its measuring.
expected: 100 105 95 97 103 101 100 98 99 103 ... n (values hovering around 100)
received: 100 105 250 95 97 102 101 20 105 98 ... n (250 and 20 are spikes)
the value 205 and 20 is false because by design it is not possible for the water level to change that fast.
What method should I use to ignore these large changes ? I can decrease the sampling interval to get more samples to compare against and correct before reporting, but how do I do it mathematically?
Averaging doesn't work because the spike will drag the value towards it.