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)
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)"