I want to compute the phase shift between two 1-D signals of same frequency, but before I'm trying to compute the time shift between. The cross-correlation function seems to be ideal for that but I'm confused on how to interpret scipy cross-correlation. Let's take two sinus with a frequency f0 = 200 Hz, a sample frequency fs = 10000 Hz, playing during 0.1s and with a phase difference of pi.
import numpy as np import matplotlib.pyplot as plt from scipy import signal # We build the two sinus t = 0.1 f = 200 fs = 10000 x = np.arange(0, t, step = 1/fs) sin1 = np.sin(2 * np.pi * f * x) sin2 = np.sin(2 * np.pi * f * x + np.pi) # We compute the scipy cross-correlation scorr = signal.correlate(sin1, sin2) plt.plot(scorr) plt.title('Scipy Correlation') plt.show()
With this last graphic output I get:
Now a few questions:
We have a x-axis spanning on 2 * fs, as the function is hermitian, I guess that we have the hermitian symmetry? In this case we could just shift it around zero (like fftshift) and only consider the positive axis, right ?
If 1) is ok, does my x time vector could fit the x-axis of my cross-correlation ?
Finally, and not necessarily related to previous questions, how to read x and y axis ? There is not much practical documentation on cross-correlation product, the only thing I know is that we have to look where the function takes its maximum in order to get the time lag between the two signals. For me the y-axis is just the result of the product of the two signals as in the formula (cross-correlation) (but I don't get why the product of two sinus with amplitude 1 could ouput 500 ...), and the x-axis gives the indice corresponding to the time difference ( and in this case, the indice where the function takes its max corresponds to the time shift I am searching for, hence the utility of plotting the cross-correlation with a fitted x time vector). Is it right or do I misunderstand something ?