Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: Bolt on June 24, 2020, 11:43:08 AM

Title: AVGR of Array
Post by: Bolt on June 24, 2020, 11:43:08 AM
Is there a way to do a AVGR (or SUMR) of an array with a pointer that wraps around?

For example:
Array of R0-R511
Pointer V0, incremented via (V0+1) & 0x1FF
Sample Size of V1

AVGR(R[V0], V1) should work, until the pointer wraps around.  Any easy work arounds?
Title: Re: AVGR of Array
Post by: Bolt on June 24, 2020, 11:45:35 AM
Perhaps move the values in question to another array, and then do the AVGR on it?
Title: Re: AVGR of Array
Post by: franji1 on June 24, 2020, 01:06:18 PM
Just do 1 or optionally 2 SUMRs, then divide by the count (does not handle the case of 0 data points, gotta have at least 1)
Code: [Select]
STRLE Start End  // normal non-wrap-around case, Start <= End
MATH Count "End+1-Start"  // e.g. Start of 5 and End of 6 yields Count of 2
MATH Avg "SUMR(R[Start], Count) / Count"  // you could just do AVGR here since this is the non-wrap case
NOT // else, Start > End, hence data is wrapping
MATH TempCountTop "End+1"  // if End is 0, TempCountTop will be 1, which is what we want
MATH SumTop "SUMR(R0, TempCountTop)"  // summation of top part of wrap
MATH TempCountBottom "DataSize-Start"  // DataSize would be 512, so wrap-around Start of 511 (R511, last one) would result in TempCountBottom equaling 1, which is what we want
MATH SumBottom "SUMR(R[Start], TempCountBottom)"  // summation of bottom part of wrap
MATH Avg "(SumTop + SumBottom) / (TempCountTop + TempCountBottom)"
Title: Re: AVGR of Array
Post by: Bolt on June 24, 2020, 01:59:44 PM
Very nice, thank you sir!