Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

View Poll Results: How do you convert dB to amp and back again?
6/ln(2) = 8.656170245 1 25.00%
20/ln(10) = 8.685889638 3 75.00%
Voters: 4. You may not vote on this poll

Reply
 
Thread Tools Display Modes
Old 12-15-2019, 03:08 AM   #1
GLD
Human being with feelings
 
Join Date: Jun 2019
Posts: 21
Default How do you convert decibels (dB) to amplitude and back again?

I am very new to making JSFX and I have been trying to figure out how people convert decibels to amplitude and back again.

After digging around in other JSFX files, I have notice that people tend to use approximations of the two following numbers:

1. 6/ln(2) = 8.656170245 ('AMP_dB = 8.6562' seems quite common)
2. 20/ln(10) = 8.685889638

These numbers are similar but not the same, apart from the first two significant figures.

After experimenting in JSFX, I found that when converting from -6dB to amplitude and back again that the first number got me closest to a circuit without change in the dB on the way out.

1.Amp_dB = 8.6562;

0.50000119 = exp(-6dB / Amp_dB);

-6dB = Amp_dB * log(0.50000119);


1b. Amp_dB = 8.65617025;

0.5 = exp(-6dB / 8.65617025);

-5.99997938dB = 8.65617025 * log(0.5);


2. Amp_dB = 8.6858896380650365530225783783321;

0.50118723 = exp(-6dB / 8.6858896380650365530225783783321);

-6.02057922dB = 8.6858896380650365530225783783321 * log(0.50118723);


3. Random method (I just played around with digits to get -6dB to be 0.5 and 0.5 to be -6dB on the way back:

Amp_dB (way out) = 8.6561703
Amp_dB (way back) = 8.6562

0.5 = exp(-6dB / 8.6561703);

-6dB = 8.6562 * log(-6dB);


Obviously, I changed the code to make it more legible. It was all done with variables, not numbers and I added the 'dB' bits in to make it clear.

The numbers above are the numbers shown in the panel at the right hand side of the the JSFX editor (I attached the image in case I copied them incorrectly).

So my question is this, how do you convert from decibels to amplitude (and back) and why?

After doing some research online I have seen both numbers used but I do not understand why some people tend to use one over the other.

This is very much out of my wheelhouse (I do not have a mathematical background and am just learning as I go) so I would appreciate the kind of an explanation/ opinion that a layman such as myself can interpret! Thank you!

GLD

4. EDIT: found the best way (I think) :

log2db = 8.6858896380650365530225783783321; // 20 / ln(10)
db2log = 0.11512925464970228420089957273422; // ln(10) / 20
0.50118723 = exp(-6 * db2log);
-6dB = log(0.50118723) * log2db;

This gives the 0.501 that is what wikipedia says should be the amp from -6dB

and gets back to exactly -6dB

I could not change the poll so just select option 2 I guess if you use this method!

I would still really love to know why the two different constants are used though...
Attached Images
File Type: jpg JSFX snip.JPG (15.9 KB, 222 views)

Last edited by GLD; 12-15-2019 at 03:52 AM. Reason: Found another way dB to amp and back to dB!
GLD is offline   Reply With Quote
Old 12-15-2019, 02:49 PM   #2
Heart Doctor
Human being with feelings
 
Join Date: Jan 2015
Location: Munich
Posts: 241
Default Gain (in dB) vs. multiplier

Let us assume that your signal has the amplitude (or voltage) x0
and you want to change the amplitude to x.

The correct formula for calculation of the gain Lx (in dB) is:

Lx = 20 * log10(x/x0) dB

Lx is the gain in dB and log10 is the decadic logarithm (base 10).

Examples:

If you double the amplitude (x/x0 = 2), you apply positive gain
of Lx = 6.0206 dB

If you reduce the amplitude to half (x/x0 = 0.5), you apply
negative gain of Lx = -6.0206 dB

We can invert the formula. Now, if your gain change Lx (in dB) is given,
you can calculate the corresponding amplitude ratio x/x0 as follows:

x/x0 = 10 ^(Lx / 20 dB)

Examples:

For Lx = 6 dB of gain multiply the amplitude by factor 1.995

For Lx = -6 dB of gain multiply the amplitude by factor 0.5012

---------------------------------------------------------------

You notice that the correct formula does not give us "nice" numbers.
For this reason, it is common to use a different formula:

x/x0 = 2 ^(Lx / 6 dB)

This formula has the advantage that gain values of 6 dB and -6 dB
correspond to multipliers of 2 and 0.5, respectively. Nice numbers,
easy to remember and easy to type into a parameter window.

Although these values are not fully correct, they are so close to the
correct result that the difference does not matter in general.

Last edited by Heart Doctor; 06-06-2020 at 05:37 AM.
Heart Doctor is online now   Reply With Quote
Old 12-15-2019, 04:37 PM   #3
GLD
Human being with feelings
 
Join Date: Jun 2019
Posts: 21
Default Gain (in dB) vs. multiplier

Thank you Heart Doctor! That makes sense- I do not understand how such things can be used interchangeably but I now have two ways to get the correct numbers in and out so I am very happy with that- thanks again- G
GLD is offline   Reply With Quote
Old 12-16-2019, 01:48 AM   #4
SaulT
Human being with feelings
 
Join Date: Oct 2013
Location: Seattle, WA
Posts: 876
Default

When it comes to this type of thing there are often ways that are "correct" and ways that are "fast" but not often ways that are both "correct" and "fast". 2^(x/6) is a useful approximation because it's easy to remember, and in some contexts is faster than 10^(x/20).

This type of approximation is not always necessary. The difference between 2^x and 10^x is fairly negligible. That said, there are functions that by default take up huge amounts of processor time, and so lots of time is spent finding shortcuts and approximations that will deliver satisfactory results with less CPU crunch.

One example is a saturation algorithm like the hyperbolic tangent. Tanh() is sloooow, so we spend a lot of time trying to find alternative functions and approximations that will give us the look/sound/performance of tanh() without the CPU hit.

There is always a tradeoff. TANFL - there ain't no free lunch. Need a steeper slope on your filter? It'll mean more CPU and possibly more filter ring. Need linear phase? Here's your group delay/latency. Want to preserve all of those nonlinearities in your filter structure? Be prepared to run an iterative solver like Newton Raphson. Ladder filter not very precise in the upper octave or two? Here's your oversampling, adding development time and a big hit to CPU.

Realizing that everything is a tradeoff really opened my eyes.
SaulT is offline   Reply With Quote
Old 12-16-2019, 12:28 PM   #5
ErBird
Human being with feelings
 
Join Date: Jan 2017
Location: Los Angeles
Posts: 1,161
Default

Quote:
Originally Posted by GLD View Post
I do not understand how such things can be used interchangeably
6/ln(2) and 20/ln(10) are actually change of base formulas, allowing you to use exp() and log() (actually natural log) functions.

Proof:


10^(x/20) and 20*log10(x) are the scientific standard, unfortunately. I feel like this is slightly at odds with common sense in audio. 6dB=2.0 feels more intuitive compared to 20dB=10.0, but I have run into problems before using base 2 and have stuck with base 10 since. Almost everyone, including Reaper internally, seem to use base 10.
ErBird is offline   Reply With Quote
Old 12-16-2019, 03:25 PM   #6
GLD
Human being with feelings
 
Join Date: Jun 2019
Posts: 21
Default

Saul T- thank you for the response- I kind of guessed it might have been a performance choice but I did not really see much of an obvious difference in the 'amount' of calculation required so thank you for clarifying!

ErBird - that is great thank you! Your explanation makes a lot of sense- you are essentially picking different points along the same scale it seems and that leads to greater accuracy because a 6dB increase is actually 1.995262315 times louder whearas a 20dB increase is exactly (or more exactly) 10 times louder, right?

Maths is fun!

Thank you all -G
GLD is offline   Reply With Quote
Old 02-18-2020, 06:40 PM   #7
ErBird
Human being with feelings
 
Join Date: Jan 2017
Location: Los Angeles
Posts: 1,161
Default

I forgot to respond. Sry.

The two formulas are very close:


At 6 dB input, the first one equals exactly 2. Point (6,2)


At 20 dB input, the second one equals exactly 10. Point (20,10)


a^(x/b) is equivalent to (a^(1/b))^x so whether you change a or b you're just changing the base of the exponential equation (f(x)=c^x).



For any positive c, f(0)=1, so by changing the base you can choose one other point that the function passes exactly through.

10^(x/20) is the scientific standard, so I'd follow that.

2^(x/6) is a rare case where a and b are both integers
AND
a^(1/b) ≈ 10^(1/20)
ErBird is offline   Reply With Quote
Old 02-20-2020, 05:35 AM   #8
GLD
Human being with feelings
 
Join Date: Jun 2019
Posts: 21
Default

Wow thanks ErBird, I am getting a real education here! Great stuff!

G
GLD is offline   Reply With Quote
Old 02-20-2020, 06:08 PM   #9
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 1,719
Default

These are the 2 functions I use in Lua (thanks to SPK77):

Code:
------------------------------------------------------------
-- Mod from SPK77
-- http://forum.cockos.com/showpost.php?p=1608719&postcount=6
--Trak_Vol_dB = 20*math.log(val, 10) end
--Trak Vol val = 10^(dB_val/20) end
------------------------------------------------------------


-------------------------------------------------------------
-- item Vol conversion    https://forum.cockos.com/showthread.php?p=2200278#post2200278



-----------------------------------------------------------
local LN10_OVER_TWENTY = 0.11512925464970228420089957273422
function DB2VAL(x) return math.exp(x*LN10_OVER_TWENTY) end

function VAL2DB(x)
  if x < 0.0000000298023223876953125 then
    return -150
  else
    return math.max(-150, math.log(x)* 8.6858896380650365530225783783321); 
  end
end
__________________
Cheers... Andrew K
Reaper v6.80+dev0621 - June 21 2023 • Catalina • Mac Mini 2020 6 core i7 • 64GB RAM • OS: Catalina • 4K monitor • RME RayDAT card with Sync Card and extended Light Pipe.
Thonex is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:41 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.