0

I'm trying to calculate the loudest peak in dB of an 16bit wav file. In my current situation i don't need an RMS value. Just the dB value of the loudest peak in the file because one requirement is to detect wav files, which have errors in it. for example the loudest peak is at +2dB.

I tried it like in this thread: get peak out of wave

Here is my Code:

var streamBuffer = File.ReadAllBytes(@"C:\peakTest.wav");
double peak = 0;

for (var i = 44; i < streamBuffer.Length; i = i + 2)
{
    var sample = BitConverter.ToInt16(streamBuffer, i);

    if (sample > peak)
        peak = sample;
    else if (sample < -peak)
        peak = -sample;
}

var db = 20 * Math.Log10(peak / short.MaxValue);

I manually altered this file so there is an peak in it at +2dB. The value of the peak var is now 32768. So the formula for the dB value will get me 0.0dB.
I can't get an positive value out of it because 32768 is just the max short can represent.

So my question is now how can i get the "correct" peak value of +2dB?

Community
  • 1
  • 1
metabolic
  • 669
  • 1
  • 7
  • 24
  • 32768 is just outside the ranger of a `short` which can take values from -32768 to 32767. That being said just set a breakpoint at the line where you calculate the decibels, and look at the value of the `peak` variable. Is it the value you expect it to be? – Dirk Feb 28 '14 at 04:55
  • 1st : it's not possible to have more than 0dB unless you are using floats, 2nd : your algorithm is wrong, initial 'peak' value should be first sample, 3rd your if block is silly, 4th you do get RMS of a block of sample not only one. – aybe Feb 28 '14 at 06:04
  • well i tried the code from link above... but could you give me a hint then on how to do it right please? i want to detect if there is an overdrive in the file or not and for that i would need the absolut peak value which can be larger then 0dB. – metabolic Feb 28 '14 at 07:31

1 Answers1

1

Your requirement is fatally flawed: The definition of clipping in both analogue and digital systems is a signal that exceeds the maximum amplitude that the channel is capable of conveying. In either case, once the signal is clipped, it's too late to recover all of the information that was previously in it.

In the case of a digital system there are two possible outcome: either that the signal saturates (in which case you might see a number of consecutive samples at peak amplitude) or that the signed int wraps (in which case very large positive values become very large negative ones or vice-versa).

To the subject of detecting clipping events after the event. A number of approaches are possible:

  • Look for consecutive runs of samples at the max and min sample values
  • Look for large discontinuities in signal amplitude between samples. Real signals don't tend to have large amplitude differences sample-sample
  • Perform frequency domain analysis on the signal. Clipping introduces high frequency components that will be apparent in a spectrogram
marko
  • 9,029
  • 4
  • 30
  • 46