[sdiy] ADC filtering (was Re: A/D converter with good accuracy for a reasonable price?)
Scott Gravenhorst
music.maker at gte.net
Sun May 8 02:05:04 CEST 2011
dan snazelle <subjectivity at hotmail.com> wrote:
>could anyone point me to a (maybe in C) code example of how this would work?
>or just some more info?
>
>would it be sort of like putting using a FOR statement to let it
>read 4 values, then average them? (do you use the un-averaged AND
>averaged readings? or do you throw out the first four?
>
>thanks
For a moving sample filter of size 4, you will start with an array like a[4] filled initially with
zeroes. You use that as a ring buffer and each sample time compute the new average as:
Out=(a[0]+a[1]+a[2]+a[3])/4;
Note that divide by 4 can be done with 2 right shifts. You maintain a modulo 4 update pointer so
that it simply wraps and always destroys the oldest value to insert the newest value. The update
pointer is incremented each sample time.
The value Out is what you use as a "cleaned up" or filtered sample.
>
>On May 6, 2011, at 5:53 PM, Tom Wiltshire wrote:
>
>>
>> On 6 May 2011, at 00:43, Scott Gravenhorst wrote:
>>
>>> jon schatz <jon at divisionbyzero.com> wrote:
>>>> On 5/5/2011 3:02 PM, Scott Gravenhorst wrote:
>>>>
>>>>> If you are averaging (for example) four ADC readings, you collect them,
>>>>> add them together and divide by four (or right shift by 2 bits).
>>>>
>>>> you can also do some exponential filtering:
>>>>
>>>> newResult = ( prevResult * (n-1) + curAdcReading) / n;
>>>>
>>>> which has the advantage of giving you a new result every cycle instead
>>>> of every n cycles (which the above averaging would do). it also means
>>>> that you'll never get a change greater than 1/n * resolution which can
>>>> be a good thing for reducing noise but a bad thing if you want to track
>>>> sharp changes.
>>>
>>> Yeah, I was thinking that too after I hit send. The filter is a single pole lowpass
>>> IIR filter and is analogous to a simple RC lowpass filter. The higher the value of n,
>>> the lower the "bandwidth" of the filter, that is more suppression of noise at higher
>>> frequencies. So it really depends on what you need from the sample stream. I think
>>> that for reading the value off of pots, the exponential filter method can be nicely
>>> advantageous, and as Jon points out, there is a loss of slew rate, but for pots, how
>>> fast can you turn them anyway?
>>>
>>> -- ScottG
>>
>>
>> +1 for averaging as a useful technique for microcontroller on-chip ADCs.
>>
>> I like to store the past values (say the last 16) and a running
>total. Then you can get a higher resolution averaged value (e.g.
>with reduced noise and no abrupt changes) by simply taking off
>the oldest input value and adding on the new input value. > > If
>you had a 12-bit input, you could add 16 of them together and get
>a 16-bit output. It won't be 16-bit accurate, but it'll never
>vary by more than 1/16th of the value. This is the "loss of slew
>rate" already mentioned, and it's often pretty handy. It's much
>better than upshifting the raw 12-bit ADC value to 16-bit since
>this also multiplies any jitter and errors, which is often the
>alternative. It's a very basic, dirty sort of digital filtering,
>but it works usefully well, and if you don't have one-cycle MACs
>for real filters it might be the best you get (one add, one
>subtract, maybe a shift if you don't need 16-bits). > > HTH, >
>Tom > > > > _______________________________________________ >
>Synth-diy mailing list > Synth-diy at dropmix.xs4all.nl >
>http://dropmix.xs4all.nl/mailman/listinfo/synth-diy >
>
>_______________________________________________
>Synth-diy mailing list
>Synth-diy at dropmix.xs4all.nl
>http://dropmix.xs4all.nl/mailman/listinfo/synth-diy
>
-- ScottG
________________________________________________________________________
-- Scott Gravenhorst
-- FPGA MIDI Synthesizer Information: home1.gte.net/res0658s/FPGA_synth/
-- FatMan: home1.gte.net/res0658s/fatman/
-- NonFatMan: home1.gte.net/res0658s/electronics/
-- When the going gets tough, the tough use the command line.
More information about the Synth-diy
mailing list