Search Unity

Audio How to calculate db correct?

Discussion in 'Audio & Video' started by KILEYI, Aug 20, 2018.

  1. KILEYI

    KILEYI

    Joined:
    Mar 7, 2015
    Posts:
    52
    Not sure if I should use
    20 * Log10(n) or
    10 * Log10(n)
    to compute correct db value.

    Code (CSharp):
    1. float[] spectrum = new float[1024];
    2. AudioListener.GetSpectrumData(spectrum, 0, FFTWindow.Hanning);
    This is what I get specturm Data range between [0, 1] I think in peak?

    I want to display them in db,which formular is correct?20 or 10?
    I found some application use 10 such as audacity(open source audio editor) and Gogot Game Engine I think.

    On the other hand I read some DSP books they just use 20 * Log10(n)

    And I googled some seems it is the difference between power and voltage.

    https://electronics.stackexchange.c...gp1-p2-and-20logv1-v2-why-not-just-10logx1-x2

    So I still don't know which one to choose,what does Unity GetSpectrumData return really mean.
    Does it stand for Power or Voltage?
     
    Last edited: Aug 20, 2018
  2. Docaroo

    Docaroo

    Joined:
    Nov 7, 2017
    Posts:
    82
    From here:

    Linear to decibel
    Code (CSharp):
    1.  
    2. private float LinearToDecibel(float linear)
    3. {
    4. float dB;
    5. if (linear != 0)
    6. dB = 20.0f * Mathf.Log10(linear);
    7. else
    8. dB = -144.0f;
    9. return dB;
    10. }
    11.  
    Decibel to linear
    Code (CSharp):
    1.  
    2. private float DecibelToLinear(float dB)
    3. {
    4. float linear = Mathf.Pow(10.0f, dB/20.0f);
    5. return linear;
    6. }
    7.  
     
    EJSainz likes this.
  3. KILEYI

    KILEYI

    Joined:
    Mar 7, 2015
    Posts:
    52
    OK,Thank you.Seems most people will use 20 as I do.
    Now I'm trying to figure out why the rest use 10 in their engine.
     
  4. ShaneP1

    ShaneP1

    Joined:
    Aug 23, 2018
    Posts:
    5
    Thanks a lot for creating this thread KILEYI and thanks to Docaroo for the solution i needed a solution for this one.
     
    Last edited by a moderator: Dec 5, 2019
  5. Docaroo

    Docaroo

    Joined:
    Nov 7, 2017
    Posts:
    82
    The question of using x10 or x20 as a factor is to do with converting from the signal's amplitude or it's power. In most cases you want to deal with amplitude in which case x20 is used. Where you see places use x10 as the factor they are converting the power of the signal, not it's actual amplitude.

    This is partially because dB is a logarithmic but essentially unitless scale. dB is meaningless unless compared to some reference or standard. Back in the old analogue days it was done using voltage (those old analogue VU meters that you've seen). The voltage conversion was a power conversion so the factor there is x10.

    Nowadays in the digital realm we talk about 0dbfs (db full scale) which is the absolute clipping point of the signal as determined by the capacity for digital data to store audio waves as data points. This is only with regards to the amplitude of the signal itself (i.e. if a waveforms amplitude goes above 0dbfs in digital it will get digitally clipped).

    In most cases you should use x20 factor unless you have a reason or know to do otherwise!

    That's my understanding of it anyways!
     
  6. KILEYI

    KILEYI

    Joined:
    Mar 7, 2015
    Posts:
    52
    Thnaks for the reply.


    I don't see too many open source db examples.

    But from some Audio Editor or DAW software,They're just different.


    Same wav audio file.

    FL Studio with 100% volume and db Meter shows about -6db.
    And audacity 2.2.2 shows about -11db.

    pretty much x2 different.

    This causes problem which makes it hard to tell gain xxx db or lower xxx db really mean.

    And the noise from mic may be -40 or -80 db,so when talking about noise above or below xxx,
    which should I use as stardard?

    And there may be -60 or -96 or -144db,what are they really mean under what condition,do they mean the lowest or what.

    This is really confusing.
     
    Last edited: Aug 28, 2018
  7. Docaroo

    Docaroo

    Joined:
    Nov 7, 2017
    Posts:
    82
    This is why I said dB is a unitless scale and must be quoted in comparison to something... and what meter you are using.

    dbfs is db Full Scale and is standard in all softwares - this scale means that 0dbfs is clipping the audio. However, there are many other metering standards out there ... for mixing music I use the Bob Katz scale (K-12, k-14 and k-20). This means that on a meter showing k-12 a reading of 0db is actually -12dbfs (full scale).

    So at 0db in K-12 I am 12db away from digitally clipping the audio sample...
     
  8. florentRaffray

    florentRaffray

    Joined:
    Feb 10, 2021
    Posts:
    7
    Shameless spot for an asset that is relevant to the discussion here and implements a lot of the good comments.

    It's called getspectrumdatahelper and it lets you select the sample count for getspectrumdata (like say 2048) but selects only 128 values from the returned frequency bins that match musical note frequencies (instead of arbitrary linear division of the frequency range).

    Then it converts the return from voltage to decibels (20 * Log10 (V/reference value)).
    It has a slider for the reference value which helps you get your return data from the negative to 0 db range to a 0 to positive range.
    In some senses the 20 vs 10 doesn't matter too much since you'll probably have a multiplier of your own to adapt your range to your visuals and so it will mathematically result in neither 10 or 20. The reference value is the more important part because it's what gets you out of the, correctly, negative to 0 range in the digital world. With a reference value of 1 you will be in a completely negative to 0 range where 0 is clipping. As you bring the reference value closer and closer to zero your values will be moved up into a positive range which is likely what you want for visualizing.

    Then it also has an equal loudness contour calibration curve so the volume changes across different frequencies are calibrated better to the way the human ear hears.

    Doing all this helps your visualizations connect to music a bit better than getspectrumdata will right out of the box.

    If you're interested it's called GetSpectrumDataHelper: https://assetstore.unity.com/packages/3d/getspectrumdatahelper-188353
     
  9. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    224
    I also ended up writing my own conversion methods. I wish Unity just had built-in conversion methods like Godot, linear2db and db2linear (https://docs.godotengine.org/en/stable/classes/class_@gdscript.html), it would really help beginners.

    By the way, looking at the source code (https://github.com/godotengine/godo...9bf925ce8b8f9c37b/core/math/math_funcs.h#L311), I found out that Godot uses the same convention (the magic numbers come from 20/ln(10) = 8.68588963806503655302 and 1/20*ln(10) = 0.1151292546497022842)