Author Topic: I want to get phase difference through c#, please help me, thanks  (Read 237 times)

zg45

  • Posts: 6
I want to get phase difference through c#, please help me, thanks

Hanuman

  • Posts: 89
What is phase difference, and what have you done so far?

zg45

  • Posts: 6
For example, I have an audio, and I want to check whether the audio has phase reversal. How to achieve it? Currently I can display the waveform via Un4seen.Bass.Misc.WaveForm and play it. Thank you for helping me

zg45

  • Posts: 6
Code: [Select]
_plm1 = new DSP_PeakLevelMeter(_stream, 0);
_plm1.CalcRMS = true;
_plm1.UpdateTime = 0.001f;
_plm1.Notification += new EventHandler(_plm1_Notification);

Can I get the phase (phase-reversed or not) information via _plm1

jpf

  • Posts: 146
I don't think it's possible to get the "correlation" from the peak meter.

For intensity stereo recordings, the basic idea is to get, sample by sample, the sum and the difference between L & R values, (L+R, L-R), accumulate all the sum values and the difference values of the amplitude separatelly,  and then compare the accumulated values.

If the sum is higher than the difference then probably there's no inversion.

The opposite isnt's true, for instance, if you have some instruments panned hard to the left and others panned hard to the right.

Stereo effects on intensity stereo recordings tend to raise the L-R component, too, leading to more false "inverted" results.

In those cases the hardly panned instruments will sound OK even when there's inversion, and only the center panned instruments (usually the voice) will be cancelled.

On recordings not based on intensity stereo (binaural, discrete, among others) the concept of phase inversion can't be applied.

Anyway, since most modern recordings are based on intensity stereo (for mono compatibility) and the instruments aren't usually hardly panned, the sum minus difference will probably be accurate enough to detect phase inversion.

I'm not a C# user, but I've done this in VB6 for my personal player. This is the simplified code:
Code: [Select]
Dim DiffAcc As Double, SumAcc As Double, ScanBytes As Long, hOv As Long, Buff() As Single
SumAcc = 0
DiffAcc = 0
hOv = BASS_StreamCreateFileAll(BASSFALSE, FileName, 0, 0, _
BASS_STREAM_DECODE Or BASS_SAMPLE_FLOAT)
ScanBytes = BASS_ChannelSeconds2Bytes(hOv, 0.1) '0.1 sec window
ReDim Buff(ScanBytes \ 4 - 1)
Do
    BytesRead = BASS_ChannelGetData(hOv, Buff(0), ScanBytes)
    If BytesRead > 0 Then
            For X = 0 To BytesRead \ 4 - 1 Step 2
                SumAcc = SumAcc + Abs(Buff(X) + Buff(X + 1))
                DiffAcc = DiffAcc + Abs(Buff(X) - Buff(X + 1))
            Next
     Else
            Exit Do
     Endif
Loop
BASS_StreamFree hOv
MsgBox "Correlation = " & SumAcc / (SumAcc + DiffAcc + 1E-32)
Correlation will range from 1.0 (mono) to 0 (phase inverted mono). Values > 0.5 indicate probably not phase inverted recording.

Hope you get the idea.

There could be more sophisticated algorithms based on FFT phase component (you can get that using the BASS_DATA_FFT_COMPLEX flag), but I didn't get to explore that area.

Edit reason: typo

zg45

  • Posts: 6
Thanks a lot, I'll try it