Old 06-11-2019, 09:13 AM   #1
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 17,840
Default JSFX: convert amplitude to dB?

I thought that I would use
Code:
dB_level = floor(20 * log10((abs(spl0) + abs(spl1))/2));
to convert the average audio amplitude to dB.

I looked in some other JSFX and saw:
Code:
sc=6/log(2); // 8.656 
dB_level2 = floor(log((abs(spl0) + abs(spl1))/2)*sc); // dB
What am I getting wrong? If they give the same result, which is more efficient? And why?
__________________
DarkStar ... interesting, if true. . . . Inspired by ...

Last edited by DarkStar; 06-11-2019 at 11:31 AM.
DarkStar is offline   Reply With Quote
Old 06-11-2019, 10:37 AM   #2
sai'ke
Human being with feelings
 
sai'ke's Avatar
 
Join Date: Aug 2009
Location: Germoney
Posts: 545
Default

They're not the same, no.
log10(x) = log(x)/log(10)

So if you want option 2 to match option one, you'd have to do something like:

sc=20/log(10);
dB_level_new = floor(log((abs(spl0) + abs(spl1))/2)*sc);

Not sure about the performance of either. I know that exp is significantly faster than pow($exp, blah); but I don't know if something similar holds for log.
__________________
[Tracker Plugin: Thread|Github|Reapack] | [Routing Plugin: Thread|Reapack] | [Filther: Thread|Github|Reapack] | [More JSFX: Thread|Reapack]
sai'ke is online now   Reply With Quote
Old 06-12-2019, 08:18 AM   #3
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 17,840
Default

Ah ha - I see where I was going wrong.

I was reading "log" as "log to the base 10", but it is actually "log to the base e, or natural log". With that understood, the 2 equations yield very similar results. Here they both are in MS Excel, with a range of values:



With amplitudes of 0.50, the equations evaluate to -6.021 and -6.000 respectively, so the second equations looks the better of the two.
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is offline   Reply With Quote
Old 06-12-2019, 08:31 AM   #4
sai'ke
Human being with feelings
 
sai'ke's Avatar
 
Join Date: Aug 2009
Location: Germoney
Posts: 545
Default

sc should be 20/log(10);

That brings your error < 1e-10;
__________________
[Tracker Plugin: Thread|Github|Reapack] | [Routing Plugin: Thread|Reapack] | [Filther: Thread|Github|Reapack] | [More JSFX: Thread|Reapack]
sai'ke is online now   Reply With Quote
Old 06-12-2019, 10:12 AM   #5
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 17,840
Default

Even better, thank you. And this is what I am working on:

__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is offline   Reply With Quote
Old 06-12-2019, 07:22 PM   #6
ashcat_lt
Human being with feelings
 
Join Date: Dec 2012
Posts: 4,068
Default

Quote:
Originally Posted by DarkStar View Post
With amplitudes of 0.50, the equations evaluate to -6.021 and -6.000 respectively, so the second equations looks the better of the two.
???
-6.021 is closer to correct. 6db for a doubling/halving is an approximation.
ashcat_lt 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 06:11 PM.


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