title: Autocorrelation of a multidimensional array in numpy
body: <p>I have a two dimensional array, i.e. an array of sequences which are also arrays. For each sequence I would like to calculate the autocorrelation, so that for a (5,4) array, I would get 5 results, or an array of dimension (5,7).</p>
  
  <p>I know I could just loop over the first dimension, but that's slow and my last resort. Is there another way?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT:</p>
  
  <p>Based on the chosen answer plus the comment from mtrw, I have the following function:</p>
  
  <pre><code>def xcorr(x):
    """FFT based autocorrelation function, which is faster than numpy.correlate"""
    # x is supposed to be an array of sequences, of shape (totalelements, length)
    fftx = fft(x, n=(length*2-1), axis=1)
    ret = ifft(fftx * np.conjugate(fftx), axis=1)
    ret = fftshift(ret, axes=1)
    return ret
  </code></pre>
  
  <p>Note that length is a global variable in my code, so be sure to declare it. I also didn't restrict the result to real numbers, since I need to take into account complex numbers as well.</p>