Math behind dynamic compression - help me to understand

DSP, Plug-in and Host development discussion.
2 posts since 9 Jan, 2014

Post Mon Apr 02, 2018 5:03 am

Hi all.

I'm trying to build my own digital dynamic compressor using my pc and usb audio card. I read a lot of code and documentation but there's something that i still don't get.

My target is very simple: a compressor with thresold and compression ratio as parameters. No attack and release. Just for the sake of my understanding of how things work - I'd like to have an easy-but-working compressor rather than a good compressor (fine details will come later).

What I need to know is how to calculate the multiplication factor (excuse my bad jargon) I have to apply to my samples when the compression kicks in.

I'm receiving samples from my audio input, and I apply the RMS formula to the last n samples, where n is my RMS window. I convert the rms value to db:
rms_db = 20 * log10(rms)

if rms_db is higher than my defined thresold (also expressed in db, between -Inf and 0) the compression starts and it will stop when the input rms_db goes below the thresold.
Let's say in a given moment rms_db = -3, thresold_db = -3.5, and the compression ratio is 3:1. What's the math to find the gain to multiply the input sample to get the corresponding output?

Also... am I reasoning right? Or this completely wrong?

Thanks for your help and your patience.

2115 posts since 29 May, 2012

Re: Math behind dynamic compression - help me to understand

Post Mon Apr 02, 2018 8:12 am

I think this answers your questions if you read it carefully enough
The amount of gain reduction is determined by ratio: a ratio of 4:1 means that if input level is 4 dB over the threshold, the output signal level is reduced to 1 dB over the threshold. The gain and output level has been reduced by 3 dB:

The highest ratio of ∞:1 is often known as limiting. It is commonly achieved using a ratio of 60:1, and effectively denotes that any signal above the threshold is brought down to the threshold level once the attack time has expired.

1366 posts since 26 Apr, 2004 from UK

Re: Math behind dynamic compression - help me to understand

Post Mon Apr 02, 2018 2:20 pm

The gain for a simple compressor with a variable knee can be found here: ... er.cpp#L49

Return to “DSP and Plug-in Development”