计算分贝
我正在使用 XNA 库录制麦克风输入(我不认为这确实是特定于技术的,但它从来没有坏处)。每次我拿到样本时我都会计算分贝。我在互联网上进行了很多搜索,但没有找到可靠的例子...
这是我尝试从样本计算分贝:
double peak = 0;
for (var i = 0; i < _buffer.Length; i = i + 2)
{
var sample = BitConverter.ToInt16(_buffer, i);
if (sample > peak)
peak = sample;
else if (sample < -peak)
peak = -sample;
}
var decibel = (20 * Math.Log10(peak/32768));
如果我将分贝值输出到屏幕上,我可以看到随着我声音变大,值会变高当我说话更轻声时,声音更低。然而,当我绝对安静时,它总是徘徊在 -40 左右……我认为它会是 -90。我上面的块中一定有计算错误吗?根据我在一些网站上读到的内容,-40 相当于“轻声说话”……但是,它是完全安静的。
另外,如果我将麦克风静音,它会直接变为 -90。
我做错了吗?
I'm recording mic input using the XNA library (I don't think this is really technology specific, but it never hurts). Every time I get a sample I would like to calculate the decibels. I have done many searches on the internet and not found a rock solid example...
Here is my attempt at calculating decibels from a sample:
double peak = 0;
for (var i = 0; i < _buffer.Length; i = i + 2)
{
var sample = BitConverter.ToInt16(_buffer, i);
if (sample > peak)
peak = sample;
else if (sample < -peak)
peak = -sample;
}
var decibel = (20 * Math.Log10(peak/32768));
If I output the decibel value to the screen I can see the values get higher as I get louder and lower as I speak softer. However, it always hovers around -40 when I'm absolutely quiet...I would assume it would be -90. I must have a calculation wrong in the block above?? from what I have read on some sites -40 is equivalent to "soft talking"...however, it's totally quiet.
Also, If I mute my mic it goes straight to -90.
Am I doing it wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
测量声音信号的电平时,应根据 RMS 值计算 dB。在您的示例中,您正在查看绝对峰值水平。单个(峰值)样本值决定您的 dB 值,即使所有其他样本都恰好为 0。
尝试这样做:
对于“瞬时”dB 级别,您通常会计算 20-50 ms 段内的 RMS。
请注意,计算出的 dB 值是相对于满量程的。对于声音,dB 值应与 20 uPa 相关,并且您需要校准信号以找到从数字值到压力值的正确转换。
When measuring the level of a sound signal, you should calculate the dB from the RMS value. In your sample you are looking at the absolute peak level. A single (peak) sample value determines your dB value, even when all other samples are exactly 0.
try this:
For 'instantaneous' dB levels you would normally calculate the RMS over a segment of 20-50 ms.
Note that the calculated dB value is relative to full-scale. For sound the dB value should be related to 20 uPa, and you will need to calibrate your signal to find the proper conversion from digital values to pressure values.
我很欣赏 Han 的帖子,并使用他的示例编写了一个可以计算 8 位和 16 位音频格式(具有多个通道)的分贝的例程。
I appreciate Han's post, and wrote a routine that can calculate decibels on 8 and 16 bit audio formats, with multiple channels using his example.
我认为 Yann 的意思是分贝是一个相对尺度。如果您尝试测量实际的声压级或 SPL,则需要进行校准。您测量的是 dBFS(我认为是满量程分贝)。您正在测量信号比系统可以表示的最大可能信号(“满量程”信号,或这些 16 位样本的 32768)安静多少分贝。这就是为什么所有的值都是负的。
I think Yann means that Decibels are a relative scale. If you're trying to measure the actual Sound Pressure Level or SPL, you would need to calibrate. What you're measuring is dBFS (decibels full-scale, I think). You're measuring how many decibels quieter the signal is than the loudest possible signal the system can represent (the "full-scale" signal, or 32768 for these 16-bit samples). That's why all the values are negative.