0
import numpy as np
import matplotlib.pyplot as plt
def xcorr(x,y):
      result = np.correlate(x, y, mode='full')
      return result[result.size/2:]
a = np.random.rand(1000)+ 40
plt.plot(xcorr(a,a))
plt.show()

Why this code give a straight line with downward slope?

I thought autocorrelation of a shouldn't be a straight line...

Is this the wrong way to get autocorrelation with numpy?

I was using this: How can I use numpy.correlate to do autocorrelation?

But.. numpy.correlate([4,4,4],[4,4,4],"full") gives array([16, 32, 48, 32, 16])

numpy version is 1.8.1

Thanks a lot for help.

Community
  • 1
  • 1

1 Answers1

0

The straight line is a consequence of the specific test case you chose. If you perform the operation manually, you get:

R(-2) = 4*4             = 16;
R(-1) = 4*4 + 4*4       = 32;
R( 0) = 4*4 + 4*4 + 4*4 = 48;
R( 1) =       4*4 + 4*4 = 32;
R( 2) =             4*4 = 16;

For a different test case such as numpy.correlate([1,2,3],[1,2,3],"full") you would get:

R(-2) = 1*3             =  3;
R(-1) = 1*2 + 2*3       =  8;
R( 0) = 1*1 + 2*2 + 3*3 = 14;
R( 1) =       2*1 + 3*2 =  8;
R( 2) =             3*1 =  3;

Which is no longer a straight line.

SleuthEye
  • 14,379
  • 2
  • 32
  • 61