# Calculate Phase angle between two signals i.e. digital Phase meter

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.

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

• 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. Mar 9, 2012 at 12:14
• @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. Mar 10, 2012 at 17:04

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.

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).

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: 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). • 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.
– Peter K.
Sep 30, 2013 at 15:23
• 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. Sep 30, 2013 at 18:17
• Cool! Thanks heaps for the great update. Will re-read with interest.
– Peter K.
Sep 30, 2013 at 18:22

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.

• That seems like more of a time difference than a phase difference. Apr 5, 2012 at 13:28
• 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. Apr 5, 2012 at 14:45