Internet PLC Forum
General => Technical support => Topic started by: cch1955 on April 05, 2010, 12:42:55 PM
-
I have an application that uses a 1200 line encoder and drive wheel measuring speed in FPM. The HSC is fast enough that it registers the minor variations in speed due to mechanical limitations of the machine. I am looking for a simple way to average the pulsefrequency over a group of samples to an average value.
I looked in the samples and did not see anything that would do this.
I am thinking some type of for/next loop that loads a series of values into memory and then averages that group of values on a regular basis.
Thanks
-
The following code is an example of how to maintain an array of 8 data values in DM. Each time a new data point is added the old values are shifted to the "right". The values in the updated array are then averaged.
I suggest that you cut and paste this code into a custom function and run it in simulation.
' Update Array of 8 32-bit data values and compute running average.
'
' DM[1..16] holds 8 32-bit values. As DM[] is only 16-bits wide,
' it takes two entries in DM[] to hold a each 32-bit value.
'
' On entry, variable V holds the 32-bit value to be added to the array.
' On Exit, A holds the avearage of the 8 array values.
' Variables V and I are modified.
' Insert newest entry on the left, DM[1], and slide all old values right, towards
' DM[16]. Oldest value is discarded.
'
FOR I = 13 to 1 STEP -2 ' Shift old data
DM[I+2] = DM
DM[I+3] = DM[I+1]
NEXT
' Insert New Data Value into beginning of the array
'
DM[1] = V ' lower 16 bits of value V
DM[2] = getHIGH16(V)
' Sum value of array elements into A
'
A = 0
for I = 1 to 15 STEP +2
V = DM
SETHIGH16 V,DM[I+1]
A = A + V
NEXT
' Compute average value
'
A = A / 8
[/color]
Good Luck,
Gary D
-
Nice work Gary, I like the method you used. Mine was a bit brute force and live with it.
Old guys have to look out for each other.
Thanks and Cheers
Chris
-
One of my favourite ways of averaging is a method that doesn't use multiple memory values and is really useful when wanting long averaging times or very fast processing.
The formula is:
NewAvg = LastAvg + (CurrentVal - LastAvg )/Constant
The constant value determines the rate of averaging. The higher the number the longer the average. I guess this is more accurately a smoothing function but can work very well depending on the application.
-
Nice simple formula, I will give it a try.
Thanks