0

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?

Peter K.
  • 25,714
  • 9
  • 46
  • 91
Saumya
  • 11
  • 2

1 Answers1

-1

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.

Peter K.
  • 25,714
  • 9
  • 46
  • 91
dsp_user
  • 921
  • 7
  • 11
  • 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. – OverLordGoldDragon Jun 22 '21 at 18:55
  • @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. – dsp_user Jun 23 '21 at 13:06
  • 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. – OverLordGoldDragon Jun 23 '21 at 13:08
  • @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). – dsp_user Jun 23 '21 at 13:17
  • 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). – OverLordGoldDragon Jun 23 '21 at 13:33
  • @OverLordGoldDragon, ¸but pure STFT's phase still isn't "random, an FFT procedure does produce random phase in the sense it doesn't contain the true phase shifts of the original signal components being analyzed. Extracting the true phase even for a single component signal can be tricky (as explained here https://www.gaussianwaves.com/2015/11/interpreting-fft-results-obtaining-magnitude-and-phase-information/) and you can't realistically expect to extract the true phase shifts for a complex signal – dsp_user Jun 24 '21 at 09:56
  • Otherwise, it would be trivial to reconstruct the phase in the frequency domain after the signal has been manipulated (equalizers etc - most implementations today, as far as I know, use some sort of optimization techique e.g Griffin-Lim ) – dsp_user Jun 24 '21 at 09:59
  • If it were random, a combination of STFT bins (what SSQ does) couldn't yield the exact phase. And surely you mean "unusable" rather than "random", which implies non-determinism. The most I'll agree is that phase cannot be extracted directly from STFT without an additional step, like SSQ. Let's also not conflate STFT with FFT. The windowing step isn't just any operation, it produces a filterbank that yields a jointly-manipulable representation which is highly redundant, and robust (unlike FFT which is highly sensitive to deformations). – OverLordGoldDragon Jun 24 '21 at 11:19
  • @OverLordGoldDragon, I didn't mean random in a strictly mathematical sense, you have a point there. Anyway, I'll try your python code and report back with the results (I may post a new thread though) – dsp_user Jun 24 '21 at 12:24
  • You could ask a question on it, I'd give it a crack when I get the chance. – OverLordGoldDragon Jun 25 '21 at 11:30