Previously, we looked at the use of the tanh() function for distortion. This will give us nice and symmetric soft clipping, where the positive and negative parts of the signal is processed in the same way. If we want to model the characteristics of analog distortion, and especially tube distortion, we will want to model the positive and negative slopes differently. One way to do this is to use a modified version of the tanh() function. Let's first look at a breakdown of the tanh() function:
And a simple addition of the distortion amount (G):
Now, if we want to allow asymmetric wave shaping, we can add small offsets (a and b) to the numerator terms like this:
Here are some examples of the resulting transfer function with some different values for a and b, using a constant distortion amount (G):
The following Csound code generates a sine wave and processes it with modified tanh() distortion. The distortion amount (drive) is controlled by the input amplitude. The output amplitude of tanh will always be in the -1.0 to 1.0 range, so the use of makeup gain is normally not necessary.
;***************************************************
; modified tanh distortion
;***************************************************
instr 1
iamp = ampdbfs(p4) ; Amp in -dB
icps = p5 ; Frequency for the tone generator
kG = p6 ; Drive
kA = p7 ; a-coefficient
kB = p8 ; b-coefficient
; audio generator
a1 oscili 1, icps, giSine
; distortion
a2 = (exp(a1 * (kA + kG)) - exp(a1 * (kB - kG))) / (exp(a1 * kG) + exp(a1 * -kG))
; audio out
outs a2*iamp
endin
;***************************************************