# Calculating the phase shift between two signals based on samples

I am comparing two signals in MATLAB Simulink for finding the phase between them. To do this I am inspired by using the code found here.

I have two vectors of the same size which are a collection of samples of the two signals (sampling is more than fast enough). They are sine-signals with mostly the same frequency. To find the phase between them the code below is used

signal_one; %A vector with values of signal 1
signal_two; %A vector with values of signal 2

dot_product = dot(signal_one,signal_two);
norm_product = (norm(signal_one)*norm(signal_two);


This code works satisfactory, and I am able to get the phaseshift from it, I just don't understand why. Can anyone show me why this gives the phase shift? A mathematical demonstration or a reference to an equation would be great.

It's an approximation as also admitted by your statement "satisfactory performance". Here is why.

Let $a[n]=\cos(w_0 n)$ and $b[n]=\cos(w_0 n + \theta)$ be two sequences of size $N$, represented by $1 \times N$ row matrices $\bar{a}$ and $\bar{b}$ in a program (such as Octave or Matlab)

Treating those two sequences $a[n]$ and $b[n]$ which are represented by two row matrices $\bar{a}$ and $\bar{b}$ as components of vectors $\vec{a}$ and $\vec{b}$, we can take advantage of the dot product stated for vectors and (approximately) compute the phase angle $\theta$ in between those two sinusoidal sequences $a[n]$ and $b[n]$

Geometric evaluation of the dot product between two vectors of same size is: $$\vec{a} \cdot \vec{b} = |\vec{a}||\vec{b}| \cos(\phi)$$

And the angle between those two vector therefore is: $$\cos(\phi) = \frac{\vec{a}\cdot \vec{b}}{|\vec{a}||\vec{b}|}$$ Where $\phi$ is the angle between those vectors, which will be equal to $\theta$ as shown:

The dot product between the vectors $\vec{a}$ and $\vec{b}$ can also be algebrically computed from their MAC sum of components which are contained in the elements of the matrices $\bar{a}$ and $\bar{b}$: $$\vec{a} \cdot \vec{b} = \sum_{i=1}^{N} \bar{a}(i)\bar{b}(i)$$

The product $\bar{a}(i)\bar{b}(i)$ can be shown to be equal to the following (using trigonometry): $$\bar{a}(i)\bar{b}(i) = \cos(w_0 i) \cos(w_0 i + \theta)= 0.5\cos(2 w_0 i + \theta) + 0.5 \cos(\theta)$$

And the dot-product summation becomes: $$\vec{a} \cdot \vec{a} = \sum_{i=1}^{N} a(i)b(i) = 0.5 N \cos(\theta) + \sum_{i=1}^{N} 0.5 \cos(2 w_0 i + \theta)$$

The absolute values $|\vec{a}|$ and $|\vec{b}|$ of the vectors $\vec{a}$ and $\vec{b}$ can also be computed from their norms computed over the matrices $\bar{a}$ and $\bar{b}$ as follows:

$$|\vec{a}| = ||\bar{a}||= ( \sum_{i=1}^{N} a(i)^2 )^{0.5}$$ $$|\vec{b}| = ||\bar{b}||= ( \sum_{i=1}^{N} b(i)^2 )^{0.5}$$

Again expanding the squared terms result in: $$|\vec{a}| = ||\bar{a}||= ( 0.5 \sum_{i=1}^{N} 1 + \cos(2 w_0 i) )^{0.5}$$ $$|\vec{b}| = ||\bar{b}||= ( 0.5 \sum_{i=1}^{N} 1 + \cos(2 w_0 i + \theta) )^{0.5}$$

Now, it can easily be shown that the following summations are small compared to large $N$: $$N+\sum_{i=1}^{N} \cos(2 w_0 i) \approx N , ~~~ N+\sum_{i=1}^{N} \cos(2 w_0 i + \theta) \approx N$$ for large $N$ and suitable $w_0$:

Simplify the above equations to get the angle: $$\cos(\phi) = \frac{\vec{a}\cdot \vec{b}}{|\vec{a}||\vec{b}|}$$ $$\cos(\phi) \approx \frac{ 0.5 N \cos(\theta)}{|\vec{a}||\vec{b}|}$$ $$\cos(\phi) \approx \frac{ 0.5 N \cos(\theta)}{0.5 N}$$ $$\cos(\phi) \approx \cos(\theta)$$ $$\phi \approx \theta$$

The approximation depends on the length $N$.

• Thanks for the thorough explanation. But is it self evident that the phase shift between two sine signals is the same as the angle of two vectors containing (infinite) samples of the signals? It is not evident to me. And do you have a reference for this? – Anderssh May 29 '17 at 13:38
• it's not self evident. As the whole derivations show it's an approximation. – Fat32 May 29 '17 at 15:12
• My problem is not with the fact that it is an approximation, but rather that phase shift = angle between the vectors. How can this be justified? It definitely seems to be so, but I would like to see some reference to support this. Because this is the basis of your whole reasoning. – Anderssh May 30 '17 at 12:00
• No no It's not the basis of my reasoning. Instead they happen to be equal for sinusoidal sequences, at the end of the computations after making the approximation. They are possibly related but I dont know a reference. The fact is that you can compute the phase shift $\theta$ between two sinusoidal sequences by computing the angle $\phi$ between two vectors obtained by assuming those signals as vectors. I don't have a more indepth explanation however. You can find it somewhere in vector space analysis books. – Fat32 May 30 '17 at 14:24

I would say what you just calculate the arctangent of the correlation at zero point divided by the norm multiplicated. The phase of the signal implies the harmonic fluctuation and its frequency. The phase shift (in the strict sense) can be obtained for the definite frequency using DFT of your signals