How to calculate phase synchrony?

http://paulbourke.net/miscellaneous/correlate/

The particular bit of magic in the second link is: “The Fourier transform of the cross correlation function is the product of the Fourier transform of the first series and the complex conjugate of the Fourier transform of the second series.”

So let’s manufacture a signal where y is shifted to the right by 25 samples (conversely, x is shifted by -25 samples).

x = Signal.sineFill(512, [1]);
y = x.rotate(25);

The magic formula requires FFTs.

~table = Signal.fftCosTable(x.size);
~xfft = x.fft(Signal.newClear(x.size), ~table);
~yfft = y.fft(Signal.newClear(y.size), ~table);

Then, if we multiply the FFT of the test signal (y) against the complex conjugate of the reference signal (x)'s FFT, we get the FFT of the crosscorrelation.

~crosscorrfft = ~yfft * ~xfft.conjugate;

// so the cross-correlation function is the ifft of that

~crosscorr = ~crosscorrfft.real.ifft(~crosscorrfft.imag, ~table);
~crosscorr.real.plot;
~crosscorr.real.maxIndex;  // 25

And the ‘x’ coordinate of the maximum value of that function is the amount of shift for the test signal. If you shift the y signal to the left by 25 samples, you should get the maximum amount of correlation against the reference.

When x and y are measured from different sources, there may be a number of local maxima, all reflecting amounts of shift where correlation is relatively high.

(I happened to have looked into autocorrelation a couple of years ago :stuck_out_tongue_winking_eye: which is a special case of this where x == y.)

hjh

3 Likes