I was never good in math, so I need help how to convert linear value to dB.
First I show you how I calculated the linear value from -24dB.
var dB, linearLevel: integer;
begin
dB := -24;
linearLevel := Round( 1/exp( 2.30258509299 * (abs(dB)/20) ) *32767);
end;
Where 1/exp( 2.30258509299 * (abs(-24)/20) ) ;
is normalized value.
For conversion to sample value I use 1/exp(...)*32767
My problem is here: conversion back
I was told to use formula 20*log10(linearLevel)
. So I tried to create it but my result is 18 instead 24 (or -24) dB.
linearValue := 2067; // the result of the formula above
db := round( exp( 20*2.30258509299*(linearLevel / 32767) ) );
How to calculate the dB?
If
linearLevel := Round( 1/exp( 2.30258509299 * (abs(dB)/20) ) * 32767);
we lose information due to the rounding (in the language of mathematics, the Round
function is not injective). So given a linearLevel
value, it is impossible to get back the original dB
value. Therefore, let us consider
linearLevel := 1/exp( 2.30258509299 * (abs(dB)/20) ) * 32767;
instead. This implies
linearLevel / 32767 := 1/exp( 2.30258509299 * (abs(dB)/20) )
and
32767 / linearLevel := exp( 2.30258509299 * (abs(dB)/20) )
and
ln(32767 / linearLevel) := 2.30258509299 * (abs(dB)/20)
and
ln(32767 / linearLevel) / 2.30258509299 := abs(dB)/20
and
20 * ln(32767 / linearLevel) / 2.30258509299 := abs(dB).
Here we again have an issue, since the absolute value function is not injective. If abs(dB)
is 7
, we cannot possibly tell if dB
is 7
or -7
.
But if we assume that dB
is non-positive, we finally have
dB = -20 * ln(32767 / linearLevel) / 2.30258509299.
Simplifications
Since 2.30258509299
is ln(10)
, this is
dB = -20 * ln(32767 / linearLevel) / ln(10).
But log10(x) = ln(x) / ln(10)
, so we can write
dB = -20 * Log10(32767 / linearLevel)
where the Log10
function is found in the Math
unit.
Also, using the law a log(b) = log(b^a)
in the case a = -1
, we can even write
dB = 20 * Log10(linearLevel / 32767).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With