# How to convert the amplitude values to decibels

**URL:** <https://discourse.processing.org/t/how-to-convert-the-amplitude-values-to-decibels/31402>\
**Category:** Libraries\
**Created:** [July 23, 2021, 11:31am UTC](https://discourse.processing.org/t/how-to-convert-the-amplitude-values-to-decibels/31402 "2021-07-23T11:31:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemangi\_2810](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@hemangi\_2810](https://discourse.processing.org/u/hemangi_2810)\
**Post date:** [July 23, 2021, 11:31am UTC](https://discourse.processing.org/t/how-to-convert-the-amplitude-values-to-decibels/31402/1 "2021-07-23T11:31:19Z")

</div>

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());

}

---

<div class="post-metadata">

**Author:** ![robertesler](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@robertesler](https://discourse.processing.org/u/robertesler)\
**Post date:** [July 23, 2021, 4:29pm UTC](https://discourse.processing.org/t/how-to-convert-the-amplitude-values-to-decibels/31402/2 "2021-07-23T16:29:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hemangi\_2810](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@hemangi\_2810](https://discourse.processing.org/u/hemangi_2810)\
**Post date:** [July 23, 2021, 6:44pm UTC](https://discourse.processing.org/t/how-to-convert-the-amplitude-values-to-decibels/31402/3 "2021-07-23T18:44:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robertesler](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@robertesler](https://discourse.processing.org/u/robertesler)\
**Post date:** [July 23, 2021, 8:12pm UTC](https://discourse.processing.org/t/how-to-convert-the-amplitude-values-to-decibels/31402/4 "2021-07-23T20:12:26Z")

</div>

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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![hemangi\_2810](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@hemangi\_2810](https://discourse.processing.org/u/hemangi_2810)\
**Post date:** [July 24, 2021, 2:08am UTC](https://discourse.processing.org/t/how-to-convert-the-amplitude-values-to-decibels/31402/5 "2021-07-24T02:08:16Z")

</div>

Yes it is working, thank you 😀

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 26, 2021, 10:30am UTC](https://discourse.processing.org/t/how-to-convert-the-amplitude-values-to-decibels/31402/6 "2021-07-26T10:30:45Z")

</div>

Hello,

Adding some references to the topic…

- [https://processing.org/reference/libraries/sound/Amplitude.html](https://processing.org/reference/libraries/sound/Amplitude.html)  
The _Description_ states that it returns RMS value.

- [log() \ Language (API) \ Processing 3+](https://processing.org/reference/log_.html)  
Description and example of converting to log base-10

- `println(log(10));` will print `2.3025851` to the console

- [Level (logarithmic quantity) - Wikipedia](https://en.wikipedia.org/wiki/Level_(logarithmic_quantity))

- [Decibel - Wikipedia](https://en.wikipedia.org/wiki/Decibel)

- What is a decibel (dB)?  
[https://www.rapidtables.com/electric/decibel.html](https://www.rapidtables.com/electric/decibel.html)

- You can use the [Math class](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html) directly

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0a657866aeed378530e51b0d3e39877d4e6775a3.png)

- Example and -3dB as expected for the [half-power point](https://en.wikipedia.org/wiki/Half-power_point) using _power levels_:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3d925c55d97e4fe1851d3fb015dedd33290ad0c3.png)

> **Example Code**
>
> ```auto
> // 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](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_.

`:)`
