0

I am trying to make the following script faster. Essentially what I am doing is a rolling window averaging. But in this way it takes far to long to run

window = 100
PVI    = []
ar =np.random.rand(100000)
for i in range(int(window/2),len(ar)-int(window/2)):
    PVI.append(np.sqrt((np.mean(ar[i-int(window/2):i+int(window/2)]**2))))

I have tried doing this by using np.cumsum. Up to this point this is what I have tried:

cumsum_vec = np.cumsum(np.insert(ar**2, 0, 0)) 
PVI_new = np.sqrt((cumsum_vec[window:] - cumsum_vec[:-window]) / window)

However, I am not sure, Are the two scrips equivalent?

jokerp
  • 157
  • 1
  • 8

0 Answers0