In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Tracy Allen wrote: I'd just like to add a comment about filtering in general, when using integer math. There tends to be a bias toward lower values, and in order to get the best result it is necessay to carry along a few extra bits, to get nearer what you would expect from a precision calculation. The imprecision comes from truncation. Let's say you have 15 samples at 8 bits, and 14 of them are value 101 and 1 of them is value 100. The average is 100.93, but if you are carrying out the average with 8 bit integer math, you will still be seeing a filter output of 100. As i said, the bias is toward low values, so as an input decreases and increases, there is a hysteresis effect as the average jumps between values, more easily down than up. To lessen that effect, multiply the input by some factor (say 16 or 256) and take the average of that. The extra bits can slosh around and in effect interpolate to the right of the radix point, so you can get an interpolated reading as an output, or at least get a better round-off. This doesn't matter if you simply what to reduce wild fluctuations and the least significant bit is not that significant anyway. But it does matter if you are using the average to do any kind of interpolation of noisy data. Signals that have pop noise or occasional outliers are greatly improved by throwing away the min and max before the average, as Bean suggested, or by making a first stage filter output the median value to a second stage filter that takes the average. If there is a strong frequency component in the noise, say 60 hertz, the most effective filter will take several samples carefully timed to cover one period of the interference. That is hard to do with something like a distance sensor, but look for possible effects of 60hz pickup. ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=175860#m176162 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2007 (http://www.dotNetBB.com)