Kunal Sonone
Kunal Sonone

Reputation: 43

Moving Average for ADC

Hi All i am working on a project where I have to calculate the moving average of ADC readings. The data coming out from ADC represent an Sinusoidal wave.

This is the code I am using to get moving average of a given signal.

longNew = (8 bit data from ADC);
longNew = longNew << 8;
//Division
longNew = longNew >> 8;    //255 Samples
longTemp = avgALong >> 8;

avgALong -= longTemp;// Old data 
avgALong += longNew;// New Data

avgA = avgALong >> 8;//256 Point Average

enter image description here

Reference Image

Reference Image

Please refer this image for upper limit and lower limit relative to reference (or avgA)

Currently I am using a constant value to obtain the upper limit and lower limit of voltage for my application which I am calculating as follows

upper_limit = avgA + Delta(x); lower_limit = avgA - Delta(x);

In my case I am taking Delta(x) = 15.

I want to calculate this constant expression or Delta(x) based on signal strength. The maximum voltage level of signal is 255 or 5Volt. The minimum voltage level of signal varies frequently because of that a constant value is not useful for my application which determines the lower and upper limit.

Please help

Thank you

Upvotes: 0

Views: 3138

Answers (1)

Russ Schultz
Russ Schultz

Reputation: 2689

Now with the description of what's going on, I think you want three running averages:

  1. The input signal. Lightly average it to help tamp down noise.
  2. upper_limit When you determine local maximums, push them into this average.
  3. lower_limit When you determine local minimums, push them into this average.

Your delta would be (upper_limit-lower_limit)/8 (or 4, or whatever). Your hysteresis points would be upper_limit - delta and lower_limit + delta.

Every time you transition to '1', push the current local minimum into the lower_limit moving average and then begin searching for a new local maximum. When you transition to '0', push the local maximum into the upper_limit moving average and begin searching for a new local minimum.

There is a problem if your signal strength is wildly varying (you could get to a point where your signal suddenly drops into the hysteresis band and you never get any more transitions). You could solve this a few ways:

  1. Count how much time you spend in the hysteresis band and reset everything if you spend too much time.

Or

  1. for each sample in the hysteresis band, bring upper_limit and lower_limit slightly closer together. Eventually they'd collapse to the point where you start detecting transitions again.

Take this with a grain of salt. If you're doing this for a school project, it almost certainly wont match whatever scholarly method your professor is looking for.

Upvotes: 1

Related Questions