How to convert the amplitude values to decibels

I have mentioned my code in Java mode , can anyone suggest me how to convert this amplitude values to decibels.

import processing.sound.*;
Amplitude amp;
AudioIn in;
void setup() {
size(640, 360);
background(255);

// Create the Input stream
amp = new Amplitude(this);
in = new AudioIn(this, 0);
in.play();
amp.input(in);
}

void draw() {
println(amp.analyze());

}

The conversion formula for decibels is:
100 + 20./2.302585092994 * log(x), where x is the linear volume or RMS. It looks like analyze() gives you the RMS value so just replace that with x.
I hope that is what you’re looking for.

Yaa but not exactly, I am having a problem like when I use float it shows that it is unexpected.

Hi @hemangi_2810, Can you provide the code that is not working for you? I’m not able to reproduce that problem.
I used your code but changed the draw() method to:

void draw() {
println( 100 + 20./2.302585092994 * log(amp.analyze()) );
}

And it prints the dB values as expected. Was there another functionality you were looking for?

Yes it is working, thank you :grinning:

Hello,

Adding some references to the topic…

Example Code
// Using the Power quantities (See Wikipedia reference):
float P = 0.5;  // Power measured
float Po = 1;   // Using 1 as reference

// Using Math class directly
double dB1 = 10 * Math.log10(P/Po);
println(dB1);

// Using Processing helper function
float dB2 = 10 * log(P/Po)/log(10);
println(dB2);

The example code is for power levels and may need to be modified for field levels.

See the Wikipedia reference for above example:
https://en.wikipedia.org/wiki/Decibel

For the RMS values it will be factor of 20 instead of 10.
See the formulas in the Wikipedia reference (above) for Root-power (field) quantities.

:)