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.
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
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
Re: Moving Average and DIV32
Disregard my last post. Upon retest the code shown did not work.
Re: Moving Average and DIV32
Please disregard this whole inquiry. I'm going down the wrong track.