I have a sound signal, and I would like to implement a simple realtime level control (for info, I am using Pure Data, but I'd like to know about the general case).

For this I thought of using the envelope amplitude (in dB) of the input Vin_db, calculated over a window of N samples ; use it to calculate the gain G to apply to the signal to get an output with amplitude Vout_db. (Disclaimer : I am very rusty with maths and decibels ...)

So I took the wikipedia formula for decibels :

V_db = 10 * log10(P / P0)


Vin_db = 10 * log10(Pin / P0)

Vout_db = 10 * log10(Pout / P0)

And this gave me :

Pout = Pin * 10^((Vout_db - Vin_db) / 10)


G = 10^((Vout_db - Vin_db) / 10)

However, this doesn't seem to work ... When I measure the output level it is not the expected Vout. Is there a fundamental flaw in my reasoning ? Or just a calculation error ?


Thanks to Paul R I found the correct formula (I need a 20 instead of a 10):

G = 10^((Vout_db - Vin_db) / 20)


You don't need to convert the voltages to dB, just calculate the required gain (as a factor) and then convert this to dB if necessary, i.e.

G = Vout / Vin                 # required gain (factor)

G_dB = 20 * log10(G)           # required gain (dB)


Vin = input voltage
Vout = output voltage

If you really want to work with dB throughout then you can do it like this (should give the same answer):

Vin_dB = 20 * log10(Vin)       # input voltage (dB)

Vout_dB = 20 * log10(Vout)     # output voltage (dB)

G_dB = Vout_dB - Vin_dB        # required gain (dB)

G = 10 ^ (G_dB / 20)           # required gain (factor)
  • $\begingroup$ But I don't know Vout! So how can I calculate G? How can I say at any point in time what Vout I am aiming at? I need to average the signal somehow ... $\endgroup$
    – sebpiq
    Jan 13 '13 at 10:39
  • $\begingroup$ Can you clarify exactly that is is that you are trying to achieve ? It's not clear form your question. Do you want to apply a given dB increase, or achieve a particular signal level output, or what ? $\endgroup$
    – Paul R
    Jan 13 '13 at 10:48
  • $\begingroup$ I want to achieve a particular signal level output. And so actually the last formula work perfectly :) (I edited my question with your answer) $\endgroup$
    – sebpiq
    Jan 13 '13 at 10:49
  • $\begingroup$ OK - and what is this required output level ? Is it a voltage, or what ? $\endgroup$
    – Paul R
    Jan 13 '13 at 10:50
  • $\begingroup$ What I want to do in the end is having a constant sound volume, no matter the input. So I guess output level is more like level in dB (ok ... maybe I am a bit confused with the units here). $\endgroup$
    – sebpiq
    Jan 13 '13 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.