Averaging AtoD samples


Closed Thread
Results 1 to 40 of 40

Hybrid View

  1. #1
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    That's better!

    Cheers

    Al.
    All progress began with an idea

  2. #2
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by aratti View Post
    That's better!

    Cheers

    Al.
    & thanks for offering up some help/support earlier!
    Last edited by HankMcSpank; - 25th September 2010 at 10:49.

  3. #3
    Join Date
    Oct 2010
    Posts
    27


    Did you find this post helpful? Yes | No

    Default

    In a different application I used a "weighted Average" where no one sample could mangle the running average.
    This is the regurgitated conversion to PBP:

    I initialize the values and add new value to the stack and subtract off the old average, giving the new value 1/weight of effect

    I leave the running average in A2D counts to avoid rounding
    Must watch (Max value * number of samples) < 65000


    Weight var byte
    WeightedAve var word
    Ave var word
    WeightedCounts
    NewAve var word
    Count var word

    AveragingComplete var word

    Incoming var word ‘incoming value from a2d
    WeightedCounts=0
    Weight = 100
    AveragingComplete = 0
    Ave=0

    If Count=0 then WeightedCounts= incoming* Weight
    If Count=0 then Ave= incoming

    If Count < Weight then Count = Count + 1
    NewAve =(incoming+ WeightedCounts-Ave
    WeightedCounts= NewAve
    Ave= WeightedCounts/ Weight
    If Count = Weight then AveragingComplete = 1
    If Count = Weight then Display “Weighted Average” ‘lcd routine
    If Count < Weight then Display “building average” ‘lcd routine

  4. #4
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Plcguy View Post
    In a different application I used a "weighted Average" where no one sample could mangle the running average.
    This is the regurgitated conversion to PBP:

    I initialize the values and add new value to the stack and subtract off the old average, giving the new value 1/weight of effect
    It must be cos I suck at maths, but I've read that a few times & it's not sinking in. Can I use an example here to how that works in practise. Let's say six AtoD Samples are taken....

    100
    99
    100
    101
    499
    100

    You said that your weighted average stops a spurious reading skewing the overall average...can you illustrate using those six samples how the 499 would be ignored?

    Many thanks!

  5. #5
    Join Date
    Oct 2010
    Posts
    27


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HankMcSpank View Post
    It must be cos I suck at maths, but I've read that a few times & it's not sinking in. Can I use an example here to how that works in practise. Let's say six AtoD Samples are taken....

    100
    99
    100
    101
    499
    100

    You said that your weighted average stops a spurious reading skewing the overall average...can you illustrate using those six samples how the 499 would be ignored?

    Many thanks!
    It will affect it, but only at 1/sample rate.
    so, for a 10 sample example, assume the running average = 100
    100 *10 (sample rate) = 1000 = sum of the samples

    new value 101
    101+1000 - 100 = 1001
    ave = 1001/sample rate = 100.1 0r 100 for PBP (rounds down)

    new value 99
    99+1001 - 100 = 1000
    ave = 1000/sample rate = 100.0 0r 100 for PBP (rounds down)

    new value 499
    499+1000 - 100 = 1400
    ave = 1400/sample rate = 140

    new value 101
    101+1400 - 140 = 1361
    ave = 1361/sample rate = 136.1 0r 136 for PBP (rounds down)

    far less than a (499 +100)/2 = 300

    to filter the 1st bad sample add the lines (or similar)
    if incoming > ave*10/9 AND if incoming < ave*10/9 then bad_count = 0
    if (incoming < ave*10/9 OR if incoming > ave*10/9) AND bad_count = 0 then bad_count = 1
    if (incoming < ave*10/9 OR if incoming > ave*10/9) AND bad_count = 1 then bad_count = 0

    if bad_count = 0 then gosub average
    if bad_count = 1 then Skip average routine

    This (or similar) should skip the 1st out of whack signal

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default

    I played with Darrels 16 bit fast averaging routine and changing the values in HystLevel, AvgCount, FAspread, I got a very slow changing average if the sampling value was close to the average.

    But if suddently the input was grounded or connected to Vdd, then the Value of the average was instatly zeroed or maximized (1024 or 255 according).

    It is an amazing routine. Great job Darrel, once again.

    So, do not be afraid of changing the User Options as Darrel marked them and experiment.

    Ioannis

    Test code:
    Code:
    include "Fast_Average.pbp"
    
    lcdout com,1
    
    while 1
        lcdout com,$80,#Value,"    "
        adcin channel,var
        Value=var
        gosub average
        pause 100
    wend

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    I too played with Darrel's averaging & was very impressed, but for this specific need (throwing away a sprurious reading that arrives in every 10 or 12 samples), I think perhaps Melanie's number sorting is more appropriate.

    Wrt Melanie's number sorting - I dabbled with a 'rolling window' (which just takes the next sample into an array & then sorts along with the previously stored samples already in the array) or a 'fill an empty array' type (which starts with an empty array....and waits untilt he array is filled before sorting, then empties & repeats). It might have been my cack approach, but I didn't see a whole lot of difference (results wise) between the two methods.

    plcguy ...thanks for the extra explanation, but that 140 'spurious' count - though much slimmed down - would still cause chaos in the frequency detect circuit I'm intending this to go in.

    I also mulled a "deduce which sample value appeared the most' in a group of samples ....which would be a handy utility to have, but at this stage of my programming life (early!), it's beyond me!
    Last edited by HankMcSpank; - 15th October 2010 at 15:28.

Similar Threads

  1. Darrel's latest 16 bit averaging routine?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 17th October 2009, 01:57
  2. ADC Averaging
    By Sach_1979 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st September 2009, 06:53
  3. Atod Digital Filter
    By GeoJoe in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd April 2008, 17:04
  4. Microchip free samples in UK?
    By zoki008 in forum Off Topic
    Replies: 1
    Last Post: - 21st March 2006, 16:06
  5. Averaging & 16 maths
    By paul.mcallister in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd May 2005, 18:17

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts