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 :
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:
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.