Signal Processing Stack Exchange is a question and answer site for practitioners of the art and science of signal, image and video processing. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Im trying to cross correlate two signals in matlab and get the phase difference between the signals.

For cross correlation (the idea is to do it without xcorr) I used:

Cxx=fftshift(ifft(fft(x,N).*conj(fft(y,N))))/(norm(x) * norm(y));

I get the result and not sure about the reference point I have to take for phase calculation Now which point should I take as zero on time scale and how do I get the phase difference from this result.


share|improve this question

There is no such thing as the phase angle between two signals unless they both consist of a single sinusoid at the same frequency, that is, $x(t) = A\cos(\omega t+\psi)$ and $y(t) = B\cos(\omega t + \phi)$. If you have $N$ samples of these signals $x(t)$ and $y(t)$, taken at times $0$, $T$, $2T, \ldots$, $(N-1)T$, so that $$x[n] = x(nT), ~~ y[n] = y(nT), 0 \leq n < N,$$ and either $N\omega T$ is an integer multiple of $2\pi$ or $N\omega T \gg 1$ then the phase angle between the two sinusoids is $$\theta = \arccos\left(\frac{\displaystyle \sum_{n=0}^{N-1} x[n]y[n]}{\sqrt{\displaystyle \sum_{n=0}^{N-1}(x[n])^2\displaystyle \sum_{n=0}^{N-1}(y[n])^2}}\right). \tag{1}$$ For complex-valued sinusoids $x(t) = Ae^{j(\omega t+\psi)}$ and $y(t) = BAe^{j(\omega t+\phi)}$, $(1)$ should be replaced by $$\theta = \arccos\left(\frac{\displaystyle \sum_{n=0}^{N-1} x[n](y[n])^*}{\sqrt{\displaystyle \sum_{n=0}^{N-1}|x[n]|^2\displaystyle \sum_{n=0}^{N-1}|y[n]|^2}}\right). \tag{2}$$

Of course, these formula can be used for arbitrary signals, not just for pure sinusoids, but then, what you get is the _angle between the two vectors $\mathbf x = (x[0], x[1], \ldots, x[N-1])$ and $\mathbf y =(y[0], y[1], \ldots, y[N-1])$ in the $N$-dimensional spaces $\mathbb R^N$ or $\mathbb C^N$, and not a phase angle between two sinusoids at the same frequency. Note that the three points $\mathbf 0 = (0,0,\ldots,0)$, $\mathbf x$ and $\mathbf y$ lie in a (two-dimensional) plane in $N$-dimensional space and the $\theta$ that you get is the angle between the line segments with endpoints $\mathbf 0$ and $\mathbf x$ and endpoints $\mathbf 0$ and $\mathbf y$ which lie in this plane. Another way to think about this is that $$\langle\mathbf x, \mathbf y\rangle = ||\mathbf x||\cdot||\mathbf y||\cdot\cos(\theta)$$ and thus $(1)$ is obtained from $$\cos(\theta) = \frac{\langle\mathbf x, \mathbf y\rangle}{||\mathbf x||\cdot||\mathbf y||} = \frac{\displaystyle \sum_{n=0}^{N-1} x[n](y[n])^*}{\sqrt{\displaystyle \sum_{n=0}^{N-1}|x[n]|^2}\sqrt{\displaystyle \sum_{n=0}^{N-1}|y[n]|^2}}.$$

If you must use FFTs because that's the way you have been told to do it, then you have $$\theta = \arccos\left(\frac{\displaystyle \sum_{n=0}^{N-1} X[n](Y[n])^*}{\sqrt{\displaystyle \sum_{n=0}^{N-1}|X[n]|^2\displaystyle \sum_{n=0}^{N-1}|Y[n]|^2}}\right)\tag{3}$$ so that you have the ineffable pleasure of not only needing to compute two FFTs first, but also of using complex multiplications in $(3)$ instead of the real multiplications in $(1)$ (for real-valued signals). This is overkill in my estimation, but as usual, YMMV, and what your boss insists on is always right, regardless of what people write on Internet forums.

share|improve this answer
I disagree, you can absolutely have a phase angle between two signals if at least one of them is complex. – Jim Clay Jun 9 '13 at 18:42
@JimClay Could you post another comment, or maybe a new answer, in which you give the definition of the phase angle between two complex signals or between a complex signal and a real signal, and how to compute this phase angle? – Dilip Sarwate Jun 10 '13 at 2:29
In the narrowest sense I think we could agree that if you start with a signal $x(t)$ and rotate it by $\phi$ radians to produce $y(t)$ ($y(t) = x(t)e^{j\phi}$), then $x(t)$ and $y(t)$ have a phase angle difference of $\phi$. I don't think it's too unreasonable to generalize that somewhat to the cross correlation, where the magnitude of the normalized cross-correlation gives the similarity between the signals, and the angle of the cross-correlation peak gives the phase difference of their commonality. – Jim Clay Jun 10 '13 at 13:39
@JimClay I agree with your assertion that a complex signal $x(t)$ and $x(t)e^{j\phi}$ can be said to differ in phase by $\phi$ radians but this is hardly a support for the broad assertion that "you can absolutely have a phase angle between two signals if at least one of them is complex" which most people would take to mean that one can define a phase angle between two arbitrary signals $x(t)$ and $y(t)$ as long as at least one of $x(t)$ and $y(t)$ is complex. – Dilip Sarwate Jun 10 '13 at 14:15
I meant "can" as in "it is possible", but I see that what I wrote was ambiguous. My apologies. – Jim Clay Jun 10 '13 at 15:29

If you can use the FFTs of x and y to get some sort of periodicity estimates from these two signals, and they are similar (or you have the periodicity a-priori), then one phase angle difference measure might be 2pi times the ratio between the cross-correlation lag and your periodicity estimate. Note that this works even if the signals are not sine waves or even have a missing fundamental component (by using the FFTs with a pitch estimation method such a cepstral or HPS).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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