Python: Get volume decibel Level real time or from a wav file

10,694

Solution 1

Short answer: dB isn't the same as dB. Your results are probably correct.

Long answer: dB levels always define a relation to some reference value. For audio / acoustics, there are many reference values, and you need to specify which one you are using for a value in dB to be meaningful. When you say

normal sound may be in the range of 50-70 dB

that's not really an accurate statement, you probably mean

normal sound may be in the range of 50-70 dB SPL

where you are giving a value relative to the reference sound pressure level of 20 µPa.

In digital systems, sound files are typically represented by floating numbers < 1, then we speak of dB FS (dB full scale) with reference value 1. By the laws of math, dB FS values are negative.

It is also clear that you cannot directly relate dB FS values to dB SPL values: if you play the same audio file (i.e. taking some dB FS value) and play it twice, but turn up the volume knob of your speaker, it will lead to two different values dB SPL (what you hear).

Solution 2

You need to add reference of sound i.e 20 log10(P rms_value/P ref)+120 dB to get the data in the range of dB scale, Pref can be tuned from your input module

Share:
10,694

Related videos on Youtube

K K
Author by

K K

Updated on June 04, 2022

Comments

  • K K
    K K almost 2 years

    For a project work, I need to measure the volume level either from a recorded audio file or in real time recording using a mic. After my primary research, I have tried using soundfile library. The file was read using soundfile and using 20*np.log10(np.sqrt(np.mean(np.absolute(a)**2))), I have calculated the dB value. I'm getting a negative value for the sound file. But a normal sound may be in the range of 50-70 dB and I'm getting a negative value. Can anybody help me to sort out this?

    • Bollehenk
      Bollehenk over 5 years
      What is your value of a? What does it represent and what is the unit?
    • DisappointedByUnaccountableMod
      DisappointedByUnaccountableMod over 5 years
      If mean(a) is less than 1.0, then you will be taking the log10 of <1.0, which is a negative number. That’s how Log10 works. Two suggestions: 1. Research the definition of audio Sound Pressure Level “dB” because they must be relative to some physical unit like pascals used for air pressure, and 2. The position of the volume control on your amplifier can make the exact same audio file into a deafening cacophony or a barely audible whisper, so you will have to make some assumptions if you want to relate a sound file to it’s SPL.
    • gurkensaas
      gurkensaas over 2 years
      I'm sorry to revive this thread but your math sometimes returns NaN when used with this code. It is otherwise very accurate, do you have any idea why that is?
  • DisappointedByUnaccountableMod
    DisappointedByUnaccountableMod over 5 years
    Agree that sound files are often floating point numbers <=1.0 - but that makes the Log10 vale <=0.0 (log10(1.0 === 0.0) - so the db FS (dB full scale) reference value is 0.0 dB in your terminology not “1”