I am doing comparison between two wave files, How to calculate phase difference between those files? Second wave is generated by making some changes in the first one .I have tried calculating phase like this:-

y, sr = librosa.load('first.wav') 
D = librosa.stft(y) 
magnitude, phase1 = librosa.magphase(D)
y1, sr1 = librosa.load('second.wav')
D1 = librosa.stft(y1)
magnitude1, phase2 = librosa.magphase(D1)

than calculated the difference phase1-phase2.

Is this the correct way?


Is this the correct way?

This may be technically correct but won't give you any useful information. The reasons for this are actually quite simple. The phase between two signals can be computed accurately only if your signals contain pure sine tones. You basically have only 2 scenarios where computing the phase difference between 2 signals makes sense.

a) the 2 signals both contain the same frequency component but have a phase shift between them

b) the 2 signals contain each a different frequency component

For case #a, the phase difference will remain the same throughout the entire length of the signals, whereas in #b, the phase difference will change as you traverse the length of the signals (it's assumed that the frequency in signal #2 is not a multiple of the frequency in signal #1 and the signals don't start in phase)

When you have a signal that contains multiple frequency components, the phase calculated by an FFT routine (including librosa.stft, which computes dft on multiple signal frames) is pretty much random (you can think of it as being the average of all frequency components contributing to a particular frequency bin, including noise i.e. undesirable components).

This means that computing the phase difference for the purpose of finding the true phase difference between 2 random signals is not possible and is completely meaningless. Computing the phase difference (within a single signal) can still be useful in other applications where it can be used to more accurately estimate the "true" bin frequencies but here it's the relative phases that we're dealing with so it's different.

In conclusion, if your signals don't fit either case a# or case b#, you won't be able to do what you want and it doesn't even make sense to try to do that in the first place.

  • $\begingroup$ Phase isn't only defined between pure sines, nor is it 'random' for non-sines. Related claims are also inaccurate. We can find phase differences via analytic representations, which STFT is capable of producing. $\endgroup$ Jun 22 '21 at 18:55
  • $\begingroup$ @OverLordGoldDragon, I don't think one can compute the true phase of a signal, at least not purely from running the signal through an FFT. An analytical approach is ok for known signals but in the general case you won't be able to compute the true phase. $\endgroup$
    – dsp_user
    Jun 23 '21 at 13:06
  • $\begingroup$ Not from FFT, but STFT is a time-frequency representation that allows phase computation. It can be done for any signal that can be modeled as a sum of analytic intrinsic modes, which spans a wide variety. This complicates with noise or if components cross in time-frequency, but that's an empirical estimation problem as with anything else. The model I speak of and related literature can be found here. $\endgroup$ Jun 23 '21 at 13:08
  • $\begingroup$ @OverLordGoldDragon, STFT allows for computing the relative phase differences between overlapping adjacent fft frames, which in turn can be used for better frequency estimates (fft bin correction) but this alone still won't give you the true phase. The answer you linked to is rather lengthy and contains some other stuff and not just STFT so I'll skip it this time (but may return to it later when I find the time). $\endgroup$
    – dsp_user
    Jun 23 '21 at 13:17
  • $\begingroup$ With synchrosqueezed STFT phase can be retrieved exactly in best case. Admittedly I'm not so sure about non-synchrosqueezed, but pure STFT's phase still isn't "random" and contains the necessary information to determine true phase - and it's even better with CWT. My main objection is this answer's claim that phase of non-sinusoids can't be found at all (with any method). $\endgroup$ Jun 23 '21 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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