Moving Average and DIV32


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1

    Default Moving Average and DIV32

    Hello all,
    I'm trying to calculate the the moving average of a word size variable. The formula I'm using is listed below and is standard as far as I can tell. New values of watts are added to the average when a counter is incremented by 1 in my program.
    Code:
      wattsAvg = (wattsAvg*counter + watts)/(counter + 1) 'moving average of watts
    The results obtained are correct as as long as the numbers stay within word size limits. However, that is not the case as my numbers can range as follows:
    counter : 0 to 400
    watts : up to 7500 max

    I think the problem would be solved with longs, but these require an 18F part. I'm using a 16F1823 as I need very small size device. I tried DIV32 as follows;
    Code:
      dummy = wattsAvg*counter + watts
      wattsAvg = div32 (counter + 1)
    This does not work. Apparently you cannot mix multiplication and addition on the dummy line. The result is always zero.

    Grateful for any ideas.

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


    Did you find this post helpful? Yes | No

    Default Re: Moving Average and DIV32

    Since you are averaging then it does not make any difference if you do your calculation on the whole number or on the difference from the average. Flag if the difference is negative, so you will know that it will be a subtraction and work with the difference, that being smaller will never overflow your word.

    Cheers

    Al.
    All progress began with an idea

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default Re: Moving Average and DIV32

    With a little help from our Friend Darrel ...

    Code:
    ' This routine will keep a "Running Average"
    
    ' Instead of adding a bunch of values together and then dividing by the number of samples,
    ' it averages each sample into the final result immediately.
    ' This eliminates the need for 32 bit math.
    ' To allow it to "Catch up" to large changes, set the FAspread to an acceptable range.
    ' Simply place the new number in VALUE and GoSub AVERAGE
    ' The Average will be returned into the same variable VALUE
    
            
    AvgCount	CON  16		' = Number of samples to average
    FAspread	CON  1000	' = Fast Average threshold +/-
    ADavg		VAR  WORD
    Value		VAR  WORD
    
    ' -=-=-=-=-=-=  Average Analog values -=-=-=-=-=-=-=-=-=-=
    Average:
        IF Value = ADavg Then NoChange
        IF ABS (Value - ADavg) > FAspread OR Value < AvgCount Then FastAvg
        IF ABS (Value - ADavg) < AvgCount Then RealClose
        ADavg = ADavg - (ADavg/AvgCount)
        ADavg = ADavg + (Value/AvgCount)
        GoTo AVGok
      FastAvg:
        ADavg = Value
        GoTo AVGok
      RealClose:
        ADavg = ADavg - (ADavg/(AvgCount/4))
        ADavg = ADavg + (Value/(AvgCount/4))
      AVGok:
        Value = ADavg			' Put Average back into Value
      NoChange:
    Return

    found here: http://www.pbpgroup.com/modules/wfse...hp?articleid=7
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Moving Average and DIV32

    Guys,
    This seems to work per your suggestions:
    Code:
      if watts = wattsAvg then
      wattsAvg = watts
      endif
    
      if watts > wattsAvg then
      wattsAvg = wattsAvg + (watts - wattsAvg)/(counter + 1)
      endif
      
      if watts < wattsAvg then
      wattsAvg = wattsAvg + (wattsAvg - watts)/(counter + 1)
      endif

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Moving Average and DIV32

    Disregard my last post. Upon retest the code shown did not work.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Moving Average and DIV32

    Please disregard this whole inquiry. I'm going down the wrong track.

Similar Threads

  1. Replies: 6
    Last Post: - 1st November 2011, 18:53
  2. DT Average
    By ShaneMichael in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 6th October 2011, 23:47
  3. Replies: 1
    Last Post: - 11th June 2011, 08:22
  4. Average Values and digital filter ...
    By jorge in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 24th April 2010, 12:29
  5. Math help - rolling average Wind Direction
    By wjsmarine in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 23rd July 2009, 00:08

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