Skip to content

Instantly share code, notes, and snippets.

@ivand58
Created January 30, 2018 13:14
Show Gist options
  • Save ivand58/e252dd4dcf4c44642951b602ca790daf to your computer and use it in GitHub Desktop.
Save ivand58/e252dd4dcf4c44642951b602ca790daf to your computer and use it in GitHub Desktop.
Autocorrelation
import random
x = [random.random() for i in range(128)]
N = 5
#x = range(22)
y = x[-N:] + x[:-N]
y2 = [i + 0.9*random.random() for i in y]
mx, my = sum(x)/len(x), sum(y)/len(y)
my2 = sum(y2)/len(y2)
mx, my, my2 = 0,0,0
cx = [i-mx for i in x]
cy = [i-my for i in y]
cy2 = [i-my2 for i in y2]
#print cx,print cy
z = [sum([(cx[-b:]+cx[:-b])[i]*cy[i] for i in range(len(cx))]) for b in range(len(cx))]
z2 = [sum([(cx[-b:]+cx[:-b])[i]*cy2[i] for i in range(len(cx))]) for b in range(len(cx))]
print z
import matplotlib.pyplot as plt
plt.plot(z)
plt.plot(z2)
plt.grid()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment