5
$\begingroup$

I want to calculate the phase difference between the two channels(signals) of a stereo audio file.(uncompressed data i.e. PCM samples).

I want to determine whether whether two signals are in phase or not i.e. kind of digital phase meter ? Not the exact phase difference.

Can you please suggest a algorithm to calculate the phase difference.

Added--

What I'm expected to identify is Phase difference between signals (due to noises , disturbances) recorded separately then feed to left and right channel.

$\endgroup$
  • 7
    $\begingroup$ The concept of a single number to represent the phase difference between two arbitrary signals is meaningless since different frequency components will have different phase differences, and some frequency components might be present in one signal but not the other, etc. Even for two-channel audio, the signals might be different if the tabla is thumping on the right channel and the harmonium squeaking on the left channel. For a stereo recording of a point source, the two channels are quite similar, and cross-correlation (as mbaitoff recommends) gives the relative delay between the signals. $\endgroup$ – Dilip Sarwate Mar 9 '12 at 12:14
  • 3
    $\begingroup$ @DilipSarwate: Also, if we consider those signals to be even more arbitrary, they may have not only frequency-dependent phase difference, but this difference might be also time-variant. So, the relative delay can be found not only withing a specific frequency band, but also within a specific time window. $\endgroup$ – mbaitoff Mar 10 '12 at 17:04
9
$\begingroup$

A quick and dirty method is to compare the energy of the sum (L+R) to the energy of the difference (L-R). This gives a a crude single-number phase difference and is good at finding things like polarity flips etc.

As Dilip pointed out, the concept of phase difference between uncorrelated signals is meaningless. Even if they are correlated, the phase difference tends to be a function of frequency, so a single number isn't particularly helpful either. Most real world stereo audio signals are partially correlated, which complicates the matter further. In order to really answer the question, you need to properly define what you mean by phase difference and what you are planning to do with it.

$\endgroup$
4
$\begingroup$

Compute the cross-correlation function of the two channels within a time window of interest. Phase spectrum intercept will give the phase difference (which is generally frequency-dependent). To compute the intercept, select a frequency band of interest, and compute a line that fits the phase spectrum within this band. If the line fits well, then the signal excerpts may indeed have the constant phase difference within the taken time window. Crossing of the line with the frequency zero line will give the intercept (which is the phase difference).

$\endgroup$
2
$\begingroup$

The way this is done on phase (correlation) meters in audio equipment is rather simple:

Phase = arctan(L/R)

With phase of 45 or 225 = 1, and phase of 135 and 315 (-45) is -1.

Essentially, the Y Axis is the L, and the X axis is the R. The phase is simply the polar angle of the vector between the two.

This type of meters will show 1 if the signal is mono, and -1 if the left and right are perfectly phase inverted.

Notice however, that phase meters of this type also account for the magnitude in the polar coordinates. So:

Magnitude = (L^2 + R^2)^1/2

Thus the actual meter display is a normalised version of:

Correlation = Phase * Magnitude

I'm not sure that satisfies your requirements, but this answers the question in the subject.

The Meter

The calculation described above is for a common meter found in many audio production products. It's prime purpose is to ensure that the programme material doesn't suffer for phase inversion between the left and right channels, which can cause issues when the material is cut to vinyl or transmitted over the FM radio medium (essentially, any system that uses MS by mechanical or transmission means.

Such is the vertical meter at the bottom of this screenshot:

A photo showing the correlation meter in Logic

From the manual:

  • A correlation of +1 (the far right position) means that the left and right channels correlate 100%—they are completely in phase.

  • A correlation of 0 (the center position) indicates the widest permissible left/right divergence, often audible as an extremely wide stereo effect.

  • Correlation values lower than 0 indicate that out-of-phase material is present, which can lead to phase cancellations if the stereo signal is combined into a monaural signal.

How it Works

It the content of the left and right channel is identical (so both sample values are X) the phase meter shows 1. If the content is perfectly out of phase (so L = X, R = -X), the phase meter shows -1.

Looking at the graph below, the green arrow represents perfect in-phase relationship between the left and right channels, and the red one perfect out-of-phase relationship between the two. In this type of meter, if one of the channels carries silence (but not the other) the meter goes to 0 (so 0, 90, 180, 270 degrees).

A graph with L as Y and R as X

$\endgroup$
  • $\begingroup$ Can you provide a reference for that? It doesn't make sense to me that simply taking the arctangent of the ratio of left and right channel signals will yield anything sensible in terms of a phase. $\endgroup$ – Peter K. Sep 30 '13 at 15:23
  • 1
    $\begingroup$ I've been looking for the equation for this for nearly two years. I gave a student of mine the task to build such phase meter in Octave, with the calculations based on some MS arithmetics. But it didn't produce the correct results. I had a moment of enlightenment (thinking of the polar conversions in DFT) and we tried it out and it worked. While I don't have a reference, I have an tested Octave code that produce the same readings as the phase meter discussed. Anyhow, I've added quite a few details to my answer. $\endgroup$ – Izhaki Sep 30 '13 at 18:17
  • $\begingroup$ Cool! Thanks heaps for the great update. Will re-read with interest. $\endgroup$ – Peter K. Sep 30 '13 at 18:22
1
$\begingroup$

It sounds like the OP is looking to align two signals. One simple way of caluclating this is to do something similar to the cross Corr:

1) Mark the left or right channel as the Reference channel

2) Take the Left channel (lets say you called the right channel the reference) and compute the mean squared error

3) shift the left channel a small time distance foward

4) go to step 2

5) when you've computed enough steps (say you took 1 second and split it into 1000 increments) the smallest Mean Squared Error will be the "phase" difference between them.

Alternate method

Measure the phase difference between two signals.

1) use the FFT algorithm of both channels

2) calculate the Phase for each frequency bin

3) for each frequency compute the Phase

4) take the mean absolute, or squared difference between the L and R channel phase components (i.e., for each frequency bin, take the difference between the phase between the L and R channels)

There are many more algorithms that will complete this for you look into Onset Detection Algorithms for beat synchronisation or look into Music Information Retrieval, this is one of many topics the area covers.

$\endgroup$
  • $\begingroup$ That seems like more of a time difference than a phase difference. $\endgroup$ – Jim Clay Apr 5 '12 at 13:28
  • $\begingroup$ Op is looking to sync the two channels, i.e., making sure the two channels are aligned in time and if I am understanding it, misused the term Phase. $\endgroup$ – CyberMen Apr 5 '12 at 14:45

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.