Reputation: 689
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?
Upvotes: 0
Views: 1168
Reputation: 9159
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:
Upvotes: 1