0

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);
}
Community
  • 1
  • 1
easysaesch
  • 159
  • 1
  • 14

1 Answers1

1

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.

Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51
  • Your function (inverse of float to dB function) was my first approach. The problem I went into was, that you can't display a dB scale in a range e.g. from -72 dB to 0 dB properly. In that case values like -40 and smaller are too close to each other. – easysaesch Apr 20 '17 at 11:44
  • @bob, That's why the sound processing widely uses logarithmic scale: large range of sound participation – Vladimir Bershov Apr 20 '17 at 11:48
  • any advice on how to achieve such a uniform scale? When applying only a factor to each value I lose the small values near 0 dB – easysaesch Aug 11 '17 at 13:06
  • @bob, what is the source of your values? Can you give an example or write a piece of data? What value range do you have? – Vladimir Bershov Aug 14 '17 at 15:38