Android Question [Solved] How to calculate DECIBEL from Audio-Samples

Midimaster

Active Member
Licensed User
[SOLVED] Look at my next post to see four function for calculating decibels!


hi, I'm working with audio and build a function to find out the Dezibel's of an audio signal:

B4X:
factor=level/SamplingRate
Decibel = Logarithm(factor*factor,10)*10

this works as expected and return the decibels of an audio signal as in AUDACITY

But now I need the reverse case. I have a given Decibel value and need to find out the resulting Level

How Can I do this?

This is a working example to demonstrate the function:
B4X:
Sub LogaTest
    Dim Level As Int=30
    Dim Dezibel, factor As Double
    Dim Percent As Int
    Dim SamplingRate=32768
    For i=0 To 12
        Level=Level*2
        factor= Level/SamplingRate
        Dezibel =Logarithm(factor*factor,10)*10
        Percent=factor*100
        Log("Level= " & Level & "    (=" & Percent & "%)       =" & $" $1.1{Dezibel}"$ & "dB  ")
    Next
End Sub
 
Last edited:

udg

Expert
Licensed User
Longtime User
I don't know if this can help. Anyway, AFAIK, the inverse function of a logarithm is a raise to power.
So that
x = log base number --> base^x = number
In your case it seems that base = 10 and number = factor * factor (then you multiply by 10).
This should produce something like:
B4X:
10^ (decibel/10) = factor ^2
 
Upvote 0

Midimaster

Active Member
Licensed User
thanks UDG. Your answer brought the correct result. I did not know that it is that easy. Thanks a lot for the quick answer.

These are the four functions to calculate with Decibels from audio-signals:

B4X:
LevelToDecibel(volume As Float) As Float
    '  purpose: returns dBA equivalent  for given normalized volume


SampleValueToDecibel(Value As Int, BitDepth As Int) As Float
    '  purpose: returns dBA equuivalant  for given SampleValue based volume


DecibelToLevel(Decibel As Float) As Float
    '  purpose: returns a normalized volume for given dBA value


DecibelToSampleValue(Decibel As Float, BitDepth As Int)
    '  purpose: returns a SampleValue based volume for given dBA value


Here is the code for the four functions:
B4X:
Sub LevelToDecibel(volume As Float) As Float
    '  purpose: returns dBA equivalent  for given normalized volume
    '    input: normalized volume between  0(=silent) and 1(=max volume)
    '           ( inputs >1 will result in positive dBA values )
    '   output: -99dBA to 0dbA
    Dim Decibel As Float=Logarithm(volume*volume,10)*10
    Return Decibel
End Sub


Sub SampleValueToDecibel(Value As Int, BitDepth As Int) As Float
    '  purpose: returns dBA equuivalant  for given SampleValue based volume
    '    input: Value between 0=silent and BitDepth/2
    '   output: -99dBA to 0dbA
    Dim SamplingRate As Int=Power(2,BitDepth-1)
    Dim Level As Float=Value/SamplingRate
    Return LevelToDecibel(Level)
End Sub


Sub DecibelToLevel(Decibel As Float) As Float
    '  purpose: returns a normalized volume for given dBA value
    '    input: -99dBA to 0dbA
    '           (input>0 will result in volume>1) 
    '   output: normalized volume between  0(=silent) and 1(=max volume)
    Dim level As Double=Power(10,(Decibel/10))
    Return Sqrt(level)
End Sub


Sub DecibelToSampleValue(Decibel As Float, BitDepth As Int) As Float
    '  purpose: returns a SampleValue based volume for given dBA value
    '    input: -99dBA to 0dbA
    '   output: SampleValue based volume between  between 0=silent and BitDepth/2
    Dim level As Double=Power(10,(Decibel/10))
    level=Sqrt(level)
    Dim Value As Float=level*Power(2,BitDepth-1)
    Return Value
End Sub

Here is a example how to use them:
B4X:
Sub DecibelTest
    Log(".")
    Log("First Test: Samples To dBA")
    Log("-----------------------------------------")
    Dim SampleValue As Int=-30
    For i=0 To 10
        SampleValue=SampleValue*2
        Log("SampleValue=" & SampleValue & " equals " & $" $1.1{SampleValueToDecibel(SampleValue,16)}"$  & "dBA")
    Next
    Log(".")

    Log("Second Test: dBA To Volume")
    Log("-----------------------------------------")

    Dim Decibel As Int=-60
    For i= 0 To 12
        Decibel=Decibel +5
     
        Log(Decibel & "dBA equals "  & $" $1.2{(DecibelToLevel(Decibel)*100)}"$ &"% volume")
    Next
End Sub
 
Last edited:
Upvote 0
Top