# Phase spectrum interpretation

I have an analog signal alternatively captured by two probes. Sometimes the signal is periodic and depending on the physical conditions the two periodic signals might be in phase or out of phase. Also, these signals are quite noisy. After the analog-digital conversion I have to compute whether these signals are in phase or out of phase (also other cases but I simplify for the moment). The signals look like that: Raw signals

The plan to achieve my goal is:

-Compute a FFT to see if there is some periodicity.

-If yes, filter the signals to isolate the main frequency I'm looking for.

-Find a way for compute the phase difference between these signals.

I'm not sure how to perform an efficient way to compute the phase difference between signals, even though it seems trivial on the paper. I tried matplotlib'phase_spectrum and I was wondering how to interpret my results. Here is the code with two sinus with a phase difference of pi/2 rad (after filtering I get something very similar to these sinus):

'''

import numpy as np
import matplotlib.pyplot as plt

fs = 100_000
f = 3800

sin1 = [np.sin((2 * np.pi * f * i)/ fs) for i in range(2000)]
sin2 = [np.sin((2 * np.pi * f )/ fs + np.pi/2) for i in range(2000)]

plt.phase_spectrum(sin1, Fs=fs)
plt.phase_spectrum(sin2, Fs=fs)
plt.show()


'''

I get this graphic :

Entire Phase spectrum

As there is only one frequency in my two sinus, I'm not able to figure out the meaning of the entire graph, anyway if I zoom in:

Phase spectrum centered on f = 3800hz

On this last picture I can see that around 3800 hz, the orange signal is approximatively equal to 0rad and the other to -1,57rad, so a difference of ~pi/2 in absolute value. I would like to ensure myself of that and numerically compute it, but I have no idea on how this function maps the frequencies on the x axis and how to get the corresponding indexes, any idea ?

If I'm right it seems that this method does the job well, but I'm not sure if my interpretation is good or if there are some cases where it might be more subtle and I could miss something. Btw, is this phase spectrum similar to the one we get from the Fourier Transform ? I know it's an important part of the Fourier Transform but there is not much information about, usually the emphasis is putted on the magnitude spectrum.

Another method would be to compute the time difference between two peaks of both sinus, the phase would be extracted by computing (time_difference_between_two_peaks)/T_0 = Phi/2pi with T_0 the period of the sine wave, and Phi the phase difference.

• Do you have any idea about the frequency of the periodic part? It is probably easier to get rid off as much noise as possible before doing anything else. Yes, phase_spectrum relies on the DFT as stated, although not clearly, in the documentation of the function. Basically, it computes the DFT, takes the angle/argument of the result, "unwraps" it, and plots . Aug 5 at 13:04
• Why does sin2 have a different frequency than sin1 in your example? Are you assuming the two signals don't have same frequencies? Aug 5 at 13:57
• @Oscar: the periodic part ranges between 2khz and 4khz, but as it comes from a gas mass in an enclosure, it depends on which gas is in. Maybe one day it will be 6khz so I prefer to compute the FFT and be sure of which frequency to isolate. Aug 5 at 14:17
• @Learner : sorry, corrected, just a phase difference of pi/2. So yes, the two signals have same frequencies. Aug 5 at 14:18