This code aims to compute the rolling average of a signal for a range of window widths (i.e. how many points are averaged), and then calculate the sum of all pairwise differences between averages at each window width.
import numpy as nptestData = np.random.normal(0,1,10000)windowWidths = np.arange(1,1000)sliding_averages = []diffs = []for windowWidth in windowWidths: # Rolling average using convolution sliding_average = np.convolve(testData, np.ones(windowWidth) / windowWidth, mode='valid') # All pairwise differences pairwiseDiffs = (sliding_average[:,None] - sliding_average[None,:]) # Define mask to extract only one corner of difference matrix mask = np.triu(np.ones_like(pairwiseDiffs, dtype=bool), k=1) pairwiseDiffs = pairwiseDiffs[mask] pairwiseDiffsSqrd = pairwiseDiffs**2 diffs.append(np.sum(pairwiseDiffsSqrd))This is aiming to be a component in reproducing the computation described in this section of a paper:
Calculation:

My question is whether there is a more efficient way to run this calculation.
I have tried vectorizing further and replacing convolution steps with other approaches, but am not confident in the result.