[Math] Phase shift of two sine curves

fourier analysisgraphing-functionsMATLAB

How do I determine phase shift of two sine curves (discrete time sampled sine curves) in Matlab. Currently, I have the FFT of these two sine curves, the phase shift is just the delay in time, which coresponds to the change of the phase angle. My question is, is it better to compare the phase shift in time domain, or do the comparison in the frequency domain?

Best Answer

A general method is to calculate the instantaneous phase for each of your discrete samples. The Hilbert transform, which can be represented in terms of the Fourier transform and it's inverse, can be used for this.

In Matlab, using the hilbert, angle, and unwrap functions:

dt = 1e-2;
t = 0:dt:20;
y1 = sin(t); h1 = hilbert(y1);
y2 = sin(t+1); h2 = hilbert(y2);
p1 = angle(h1); p2 = angle(h2); % Instantaneous phase (wrapped)
p = unwrap(p2)-unwrap(p1);      % Instantaneous phase difference

figure;
subplot(211);
plot(t,p1,'r',t,p2,'b');
subplot(212);
plot(t,p,'k');

The result is a figure something like this:

Matlab plot

Note, that, like the Fourier transform, this won't be accurate at the edges of your signals. Trimming your input signals to a whole number of periods will reduce aliasing effects. Type edit hilbert in your command window too see the code.