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)

then calculated the difference as (phase1-phase2)

Is this the correct way??

  • 1
    $\begingroup$ Are the two wave files somehow related? What have you tried so far? $\endgroup$ – dsp_user May 27 at 6:10
  • $\begingroup$ @dsp_user yes second wave is generated by making some changes in the first one .I have tried calculating phase like this:-y, sr = librosa.load('sample.wav') D = librosa.stft(y) magnitude, phase = librosa.magphase(D) calculate phase and phase1 for both the files and than find out the difference.Is this the correct way?? $\endgroup$ – Saumya May 28 at 3:05
  • $\begingroup$ you will have to provide more information to have your question reopened. $\endgroup$ – dsp_user May 28 at 6:57

You need to cross-correlate those two wavfiles : from wikipedia "cross-correlation is a measure of similarity of two series as a function of the displacement of one relative to the other"

The result obtained will give you a measure of the similarity between those two audio signals depending on the time.

Therefore you will need to find the time for which the cross-correlation is at its maximum (ie : maximum similarity), it will indicates you the time delay, phase between those two signals.

A couple of advices :

1/ If your signals are periodic, you only need to compute cross-correlation over one period (or more if you want to average the noise).

2/ Don't forget to take the absolute value of the cross-correlation while searching for the maximum.

| improve this answer | |
  • $\begingroup$ no, cross-correlation won't help you find the phase difference between 2 signals (files), which is what the OP wants to know. $\endgroup$ – dsp_user May 27 at 17:35

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.