Faster mtotdev() and htotdev()

The AllanTools functions mtotdev() and htotdev() are now almost 10-times faster, after an update to the code that more efficiently calculates moving averages.

The old code used numpy.mean() for each iteration of a loop:

for j in range(0, 6*m): # summation of the 6m terms.
    xmean1 = np.mean(xstar[j     :   j+m])
    xmean2 = np.mean(xstar[j+m   : j+2*m])
    xmean3 = np.mean(xstar[j+2*m : j+3*m])

However this can be computed much faster by noticing that the new mean differs from the old (already computed!) mean by just two points, one at the start is dropped, and a new one at the end is added:

for j in range(0, 6*m): # summation of the 6m terms.
    if j == 0:
        # intialize the sum
        xmean1 = np.sum( xstar[0:m] )
        xmean2 = np.sum( xstar[m:2*m] )
        xmean3 = np.sum( xstar[2*m:3*m] )
    else:
        # j>=1, subtract old point, add new point
        xmean1 = xmean1 - xstar[j-1] + xstar[j+m-1] #
        xmean2 = xmean2 - xstar[m+j-1] + xstar[j+2*m-1] #
        xmean3 = xmean3 - xstar[2*m+j-1] + xstar[j+3*m-1] #

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.