audiosignal-processingdrawdecibel

Rescale logarithmic dB value scale


I'm currently working on implementing a peak meter and need to rescale the logarithmic dB-values. Searching for existing functions I found this: How do I calculate logrithmic labels for a VU meter scale?

It does almost what I need. I don't get the mathematical background of those functions.

I hope somebody can explain.

function sigLog(n) {
    return Math.log(Math.abs(n) + 1) / Math.log(10) * sig(n);
}

function sig(n) {
    return n == 0 ? 0 : Math.abs(n) / n;
}


function sigExp(n) {
    return (Math.pow(10, Math.abs(n)) - 1) * sig(n);
}

Solution

  • First, logarithmic scale is just one of the possible numeric scales, linear is just another. Like, for example, celsius or fahrenheit temperature scales. You may know nothing about "Celsius" or "Fahrenheit", but you can simply use the formulas:

    [°C] = ([°F] - 32) × 5/9  
    [°F] = [°C] × 9/5 + 32
    

    ... And do not think about anything (because these scales are strictly defined).

    As for logarithmic & linear scales, you should think a little, because you should know which kind of scale you're currently using: for example, you have some "logarithmic" value, but what are the limits of your target linear scale? from 0 to 32768, or may be from 0 to 1 ? Also, The standard used logarithm base is 10, but possible to be another.

    What I use to convert logarithmic sound volume parameter to a liner ratio coefficient (from "log" to [0, 1] linear scale), in c++:

    float db_to_linear_ratio(float db_val)
    {
        return pow(10.0, db_val/10.0);    
    }
    

    See: Why is a logarithmic scale used to measure sound?

    Edit. As for a peak meter, I have never seen it in a real linear scale. In sound editors peak meters just display logarithmic values (-72, -30, -1, 0, ...) on an uniform scale:

    enter image description here

    Therefore, I see a solution for you not to convert dB-values to a linear scale, but just display them on a uniform scale.