The modified tanh() function

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:

\[ \tanh(x) = \frac{\sinh(x)} {\cosh(x)} = \frac{e^x-e^{-x}}{e^x+e^{-x}} \]

Breakdown of tanh() function

And a simple addition of the distortion amount (G):

\[ \frac {e^{x*G} - e^{x*-G}} {e^{x*G}+e^{x*-G}}\]

Breakdown of tanh() function, with gain coefficient

Now, if we want to allow asymmetric wave shaping, we can add small offsets (a and b) to the numerator terms like this:

\[ \frac {e^{x*(a+G)} - e^{x*(b-G)}} {e^{x*G}+e^{x*-G}}\]

Modified tanh() function

Here are some examples of the resulting transfer function with some different values for a and b, using a constant distortion amount (G):

Straight tanh(): a=0.0, b=0.0, G= 4
Modified tanh() with a=0.8, b=0.0, G= 4
Modified tanh() with a=0.0, b=1.5, G= 4
Modified tanh() with a=0.8, b=1.5, G= 4

Signal flow

Audio signal graphs

Sine with tanh distortion, gain = 4
Modified tanh() with a=0.8, b=0.0, G= 4
Modified tanh() with a=0.0, b=1.5, G= 4
Modified tanh() with a=0.8, b=1.5, G= 4

Sound examples


440 Hz Sine wave, clean

Sine with tanh distortion, gain = 4

Sine with modified tanh() with a=0.8, b=0, G= 4

Sine with modified tanh() with a=0.0, b=1.5, G= 4

Sine with modified tanh() with a=0.8, b=1.5, G= 4

Clean sample

Sample with tanh distortion, gain = 4

Sample with modified tanh() with a=0.8, b=0, G= 4

Sample with modified tanh() with a=0.0, b=1.5, G= 4

Sample with modified tanh() with a=0.8, b=1.5, G= 4

Csound code

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
;***************************************************