Csound Csound-dev Csound-tekno Search About

[Csnd] compression, logarithmic or linear

Date2013-09-20 09:11
FromOeyvind Brandtsegg
Subject[Csnd] compression, logarithmic or linear
Hi,
I'm building a compressor for pedagogical purposes,
and stumbled upon a question.
When calculating the gain reduction, should we always work in dB or in
a linear amplitude scale?
For example, with a threshold of -20 and a ratio of 2:1, using an
input signal at 0dB:
- should I get a stable output at -10 dB,
- or at amplitude 0.55 (-20 dB is amp=0.1, 0 dB is amp=1.0)

I'm guessing it should work in dB,
but figured it sensible to ask.
It's not just the relation between parameter values and output level,
the compressor will react differently to a dynamic input as well, so
transients will be shaped differently.
They sound differently, so of course there's no reason not to have
both methods available, but it would be interesting to get insight
into what is most common, what other implementations use, and what
good old hardware boxes do.

Below is a csd doing linear compression on the right channel and dB
compression in the left.

best
Oeyvind




-ocompressor1compare.wav    ; output to audio file



    sr = 48000
    ksmps = 1
    nchnls = 2
    0dbfs = 1

;***************************************************
;ftables
;***************************************************

    giSine        ftgen    0, 0, 65536, 10, 1                ; sine

;***************************************************
; compare linear and dB based compression
;***************************************************
    instr    2

    iamp        = ampdbfs(p4)            ; Amp in -dB
    kthresh        = p5                ; compression threshold
    kratio        = p6                ; compression ratio

; audio generator
    a1        oscili    iamp, 440, giSine                ; sine tone

; compressor (dB)
    krms1        rms a1                            ; analyze amplitude
of input signal
    krms1        = krms1 * 1.42                        ; normalize the
rms measurement
    krms_dB1    = dbfsamp(krms1)                    ; convert to dB scale
    kovershoot1    = kthresh - krms_dB1                    ; how much
over the threshold are we?
    ktarget1    = kthresh - (kovershoot1*(1/kratio))            ;
target output level (with current input level, threshold and ratio)
    kampMod_dB1    =  ktarget1 - krms_dB1                    ;
difference from target = adjust amount
    kampMod_dB1    limit kampMod_dB1, -150, 0                ; do not
adjust unless negative
    kampMod1    = ampdbfs(kampMod_dB1)                    ; convert
back to normalized scale
    acomp1        = a1*kampMod1                        ; apply
amplitude modification

; compressor (linear)
    krms        rms a1                            ; analyze amplitude
of input signal
    krms        = krms * 1.42                        ; normalize the
rms measurement
    krms_dB        = dbfsamp(krms)                        ; convert to dB scale
    kovershoot    = krms - ampdbfs(kthresh)                ; how much
over the threshold are we?
    ktarget        = ampdbfs(kthresh) + (kovershoot*(1/kratio))
; target output level (with current input level, threshold and ratio)
    kampMod        =  ktarget/krms                        ; get
adjustment factor as ratio between target and input amp
    kampMod        limit kampMod, 0, 1                    ; do not
adjust if over 1
    acomp        = a1*kampMod                        ; apply amplitude
modification

; audio out
    outs        acomp1, acomp
    endin
;***************************************************





;compression
;     start    dur    amp    thresh    ratio
i2    0    3    -0    -20    2
i2    ^+4    3    -0    -20    3
i2    ^+4    3    -0    -20    6




-- 

Oeyvind Brandtsegg
Professor of Music Technology
NTNU
7491 Trondheim
Norway
Cell: +47 92 203 205

http://flyndresang.no/
http://www.partikkelaudio.com/
http://soundcloud.com/brandtsegg
http://soundcloud.com/t-emp

Date2013-09-20 09:20
Frompeiman khosravi
SubjectRe: [Csnd] compression, logarithmic or linear
Hi Oeyvind,

Based on the visual display of the common compressor plug-ins - like the cheap one that comes with Pro Tools -  I'd say it's typically logarithmic. 

Thanks for the CSD, this is going to be really useful for me!
 
Best,
Peiman



On 20 September 2013 09:11, Oeyvind Brandtsegg <oyvind.brandtsegg@ntnu.no> wrote:
Hi,
I'm building a compressor for pedagogical purposes,
and stumbled upon a question.
When calculating the gain reduction, should we always work in dB or in
a linear amplitude scale?
For example, with a threshold of -20 and a ratio of 2:1, using an
input signal at 0dB:
- should I get a stable output at -10 dB,
- or at amplitude 0.55 (-20 dB is amp=0.1, 0 dB is amp=1.0)

I'm guessing it should work in dB,
but figured it sensible to ask.
It's not just the relation between parameter values and output level,
the compressor will react differently to a dynamic input as well, so
transients will be shaped differently.
They sound differently, so of course there's no reason not to have
both methods available, but it would be interesting to get insight
into what is most common, what other implementations use, and what
good old hardware boxes do.

Below is a csd doing linear compression on the right channel and dB
compression in the left.

best
Oeyvind


<CsoundSynthesizer>
<CsOptions>
-ocompressor1compare.wav    ; output to audio file
</CsOptions>
<CsInstruments>

    sr = 48000
    ksmps = 1
    nchnls = 2
    0dbfs = 1

;***************************************************
;ftables
;***************************************************

    giSine        ftgen    0, 0, 65536, 10, 1                ; sine

;***************************************************
; compare linear and dB based compression
;***************************************************
    instr    2

    iamp        = ampdbfs(p4)            ; Amp in -dB
    kthresh        = p5                ; compression threshold
    kratio        = p6                ; compression ratio

; audio generator
    a1        oscili    iamp, 440, giSine                ; sine tone

; compressor (dB)
    krms1        rms a1                            ; analyze amplitude
of input signal
    krms1        = krms1 * 1.42                        ; normalize the
rms measurement
    krms_dB1    = dbfsamp(krms1)                    ; convert to dB scale
    kovershoot1    = kthresh - krms_dB1                    ; how much
over the threshold are we?
    ktarget1    = kthresh - (kovershoot1*(1/kratio))            ;
target output level (with current input level, threshold and ratio)
    kampMod_dB1    =  ktarget1 - krms_dB1                    ;
difference from target = adjust amount
    kampMod_dB1    limit kampMod_dB1, -150, 0                ; do not
adjust unless negative
    kampMod1    = ampdbfs(kampMod_dB1)                    ; convert
back to normalized scale
    acomp1        = a1*kampMod1                        ; apply
amplitude modification

; compressor (linear)
    krms        rms a1                            ; analyze amplitude
of input signal
    krms        = krms * 1.42                        ; normalize the
rms measurement
    krms_dB        = dbfsamp(krms)                        ; convert to dB scale
    kovershoot    = krms - ampdbfs(kthresh)                ; how much
over the threshold are we?
    ktarget        = ampdbfs(kthresh) + (kovershoot*(1/kratio))
; target output level (with current input level, threshold and ratio)
    kampMod        =  ktarget/krms                        ; get
adjustment factor as ratio between target and input amp
    kampMod        limit kampMod, 0, 1                    ; do not
adjust if over 1
    acomp        = a1*kampMod                        ; apply amplitude
modification

; audio out
    outs        acomp1, acomp
    endin
;***************************************************


</CsInstruments>
<CsScore>

;compression
;     start    dur    amp    thresh    ratio
i2    0    3    -0    -20    2
i2    ^+4    3    -0    -20    3
i2    ^+4    3    -0    -20    6
</CsScore>
</CsoundSynthesizer>


--

Oeyvind Brandtsegg
Professor of Music Technology
NTNU
7491 Trondheim
Norway
Cell: +47 92 203 205

http://flyndresang.no/
http://www.partikkelaudio.com/
http://soundcloud.com/brandtsegg
http://soundcloud.com/t-emp


Send bugs reports to the Sourceforge bug trackers
csound6:
            https://sourceforge.net/p/csound/tickets/
csound5:
            https://sourceforge.net/p/csound/bugs/
Discussions of bugs and features can be posted here
To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"