title: Estimate Autocorrelation using Python
body: <p>I would like to perform Autocorrelation on the signal shown below. The time between two consecutive points is 2.5ms (or a repetition rate of 400Hz).</p>
  
  <p><img src="https://i.stack.imgur.com/UknOq.png" alt="enter image description here"></p>
  
  <p>This is the equation for estimating autoacrrelation that I would like to use (Taken from <a href="http://en.wikipedia.org/wiki/Autocorrelation" rel="noreferrer">http://en.wikipedia.org/wiki/Autocorrelation</a>, section Estimation):</p>
  
  <p><img src="https://i.stack.imgur.com/CujxE.png" alt="enter image description here"></p>
  
  <p>What is the simplest method of finding the estimated autocorrelation of my data in python? Is there something similar to <code>numpy.correlate</code> that I can use? </p>
  
  <p>Or should I just calculate the mean and variance?</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>With help from <a href="https://stackoverflow.com/users/190597/unutbu">unutbu</a>, I have written:</p>
  
  <pre><code>from numpy import *
  import numpy as N
  import pylab as P
  
  fn = 'data.txt'
  x = loadtxt(fn,unpack=True,usecols=[1])
  time = loadtxt(fn,unpack=True,usecols=[0]) 
  
  def estimated_autocorrelation(x):
      n = len(x)
      variance = x.var()
      x = x-x.mean()
      r = N.correlate(x, x, mode = 'full')[-n:]
      #assert N.allclose(r, N.array([(x[:n-k]*x[-(n-k):]).sum() for k in range(n)]))
      result = r/(variance*(N.arange(n, 0, -1)))
      return result
  
  P.plot(time,estimated_autocorrelation(x))
  P.xlabel('time (s)')
  P.ylabel('autocorrelation')
  P.show()
  </code></pre>