# Phase difference between two wave files

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)

D1 = librosa.stft(y1)
magnitude1, phase2 = librosa.magphase(D1)


then calculated the difference as (phase1-phase2)

Is this the correct way??

• Are the two wave files somehow related? What have you tried so far? May 27 '20 at 6:10
• @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?? May 28 '20 at 3:05
• you will have to provide more information to have your question reopened. May 28 '20 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.

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.

• no, cross-correlation won't help you find the phase difference between 2 signals (files), which is what the OP wants to know. May 27 '20 at 17:35
• I agree that cross correlation would be the best approach to determine the delay between the two. We must also be careful to distinguish phase from delay as the two are not the same. If the phase is non-linear, then the Wiener-Hopf equations can be used to determine the phase at each frequency component in the signal (which uses a cross-correlation vector as well as an autocorrelation matrix to solve: see dsp.stackexchange.com/questions/63141/…) Mar 7 at 5:48

If the OP is actually interested in the simple time delay between the files, then computing the cross-correlation function and finding the delay parameter $$\tau$$ that maximizes this function would provide a reasonable estimate of the delay.

However delay and phase are not the same thing. For a fixed delay, the phase for every frequency component in the signal will be increasing in the negative direction versus frequency, with a slope that is larger for a larger delay (this is the basic property of "linear phase" filters which provide a fixed delay and therefore no group-delay distortion).

If the OP is truly interested in the phase characteristics between the waveforms; meaning the phase for every frequency component (or more specifically the frequency response, including magnitude and phase), then the Wiener-Hopf equations can be used to determine this: given signal $$A$$ that passes through a distortion channel to create signal $$B$$, we can use the Wiener-Hopf equations to determine the channel from $$A$$ and $$B$$ as a frequency response showing how the amplitude and phase is varied for every frequency component. I detail this further at this post here:

Compensating Loudspeaker frequency response in an audio signal

and

How determine the delay in my signal practically

Depending on the order in which we use $$A$$ and $$B$$ in the Wiener-Hopf equations, we can either determine the channel or the compensation to equalize for the effects of the channel (for subsequent waveforms that pass through the same channel). This is a linear equalization approach, which is less effective for channels with deep frequency nulls (multipath fading with long delay spreads) as it will lead to noise enhancement at those nulls compared to non-linear equalization approaches.