1
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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 . $\endgroup$
    – Oscar
    Aug 5, 2021 at 13:04
  • $\begingroup$ Why does sin2 have a different frequency than sin1 in your example? Are you assuming the two signals don't have same frequencies? $\endgroup$
    – Learner
    Aug 5, 2021 at 13:57
  • $\begingroup$ @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. $\endgroup$
    – terzan5
    Aug 5, 2021 at 14:17
  • $\begingroup$ @Learner : sorry, corrected, just a phase difference of pi/2. So yes, the two signals have same frequencies. $\endgroup$
    – terzan5
    Aug 5, 2021 at 14:18

2 Answers 2

3
$\begingroup$

Assuming both your filtered signals have the same frequency contents, you can determine the time-lag through cross-correlation, and then translate that time-lag to phase difference. Some other solutions are also suggested in this answer.

$\endgroup$
1
$\begingroup$

I have to compute whether these signals are in phase or out of phase

This an ill posed problem. Generally the phase difference is a function of frequency and it often doesn't make sense to condense this to a single number. Even if the signals are periodic, the tend to have lots of harmonics and each harmonic has it's own phase difference. You could do an energy weighted difference of the phase differences, but in many cases the result will be meaningless: what would you do with it?

From the graph it looks like over a good part of your spectrum the phase is more or less linear, so you could try to look at the group delay and check if that's reasonably constant with frequency.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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