I have been trying to do the cross correlation of two signals, and I am expecting the output to have the same dimensions as the two signals have. However what I am seeing is that the cross correlation has twice the amount of points enter image description here

The red here is the cross correlation of the green and blue. The two starting signals cover $1$ hour of time, sampled at $100Hz$, where as the cross correlation seems to represent $2$ hours, what I want to see is how correlated they are over the 1 hour. Can anyone explain this? (also apologies the graphs look like the joker)


1 Answer 1


When you compute cross correlations, you shift one signal over the other and compute the (normalized) inner product of the overlapping sections of the two signals. The point is that you can shift in both directions, i.e., your time shift (in samples) can be positive or negative.

Assume both signals have $N$ samples. Your central data point in the cross correlation is when you haven't shifted your second signal at all, i.e., all $N$ points are overlapping. Now, you can shift "to the right" by 1 sample: your overlap is $N-1$. Shift another one: your overlap is $N-2$. Keep going, until you shifted $N-1$ samples: there is only an overlap of 1 sample. This gives one half of your correlation function. You get the other half by shifting in the other direction, "to the left", you get another $N-1$ samples like that. In total, this gives $2N-1$ samples, which explains the "doubling" you observe.

Example: Correlate the sequences [1,2,3] and [4,-5,6]. Normalization left aside, we get as inner products:

[1,2,3]'*[0,0,4] = 12
[1,2,3]'*[0,4,-5] = -7
[1,2,3]'*[4,-5,6] = 12
[1,2,3]'*[-5,6,0] = 7
[1,2,3]'*[6,0,0] = 6

Therefore, from N=3 original lags, we get 2N-1=5 lags in the correlation function.

If you have a good reason to believe that your signals are periodic, you can use a cyclic convolution, which will result in N samples exactly. But his only makes sense for periodic signals.

  • 1
    $\begingroup$ perfect, thank you $\endgroup$ Jun 21, 2019 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.