The tanh() function

This distortion technique involves using a mathematical function to directly modify the values of the audio signal. The tanh() function can give a rounded "soft clipping" kind of distortion, and the distortion amount is proportional to the input gain. Take a look at the transfer function for tanh() with some different input amplitudes.

Signal flow

A linear transfer function (signal unmodified)
tanh(x*2) transfer function
tanh(x*4) transfer function

Audio signal graphs

A clean Sine wave
Sine with tanh distortion, gain = 1
Sine with tanh distortion, gain = 2
Sine with tanh distortion, gain = 4
Sine with tanh distortion, gain = 7

Sound examples


440 Hz Sine wave, clean

Sine with tanh distortion, gain = 1

Sine with tanh distortion, gain = 2

Sine with tanh distortion, gain = 4

Sine with tanh distortion, gain = 7

Csound code

The following Csound code generates a sine wave and processes it with 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.

;***************************************************
; tanh distortion
;***************************************************
	instr	1

	iamp		= ampdbfs(p4)		; Amp in -dB
	icps		= p5			; Frequency for the tone generator
	kDrive		= p6

; audio generator
	a1		oscili	1, icps, giSine

; distortion
	a1		= tanh(a1*kDrive)
	
; audio out
	out		a1*iamp
	endin
;***************************************************