Skip to content

clip

Clips an a-rate signal to a predefined limit, in a “soft” manner, using one of three methods.

Syntax

ares = clip(asig, imeth, ilimit [, iarg])
ares clip asig, imeth, ilimit [, iarg]

Initialization

imeth -- selects the clipping method. This argument is required. The methods are:

  • 0 = Bram de Jong method
  • 1 = sine clipping
  • 2 = tanh clipping

Other method codes use method 0.

ilimit -- a finite, non-negative clipping limit. A limit of 0 produces silence.

iarg (optional, default=0.5) -- for method 0, the fraction of ilimit at which soft clipping starts. Use a value from 0 to 1. A value of 1 gives hard clipping at ilimit. Methods 1 and 2 ignore this argument.

Performance

asig -- a-rate input signal

For a positive ilimit, the curves below use L for ilimit and x for the input sample.

The Bram de Jong method (imeth = 0) uses a for iarg and u for the input magnitude, \(u = |x|\):

\[ u \le La: \quad f(x) = x \]
\[ La < u \le L: \quad f(x) = \operatorname{sign}(x) \left(La + \frac{u-La}{1 + \left(\frac{u-La}{L(1-a)}\right)^2}\right) \]
\[ u > L: \quad f(x) = \operatorname{sign}(x)\frac{L(1+a)}{2} \]

The curved segment is empty when a = 1. For smaller values of a, the output stops at \(\pm L(1+a)/2\), below the clipping limit.

The second method (imeth = 1) is the sine clip:

\[ |x| < L: \quad f(x) = L\sin\left(\frac{\pi x}{2L}\right), \qquad |x| \ge L: \quad f(x) = L\operatorname{sign}(x) \]

The third method (imeth = 2) is the tanh clip:

\[ |x| < L: \quad f(x) = L\frac{\tanh(x/L)}{\tanh(1)}, \qquad |x| \ge L: \quad f(x) = L\operatorname{sign}(x) \]

Examples

Here is an example of the clip opcode. It uses the file clip-modern.csd.

Example of the clip opcode.
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
-odac     ;;;RT audio out
;-iadc    ;;;uncomment -iadc if RT audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o clip.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 32
nchnls = 2
0dbfs  = 1

instr 1 ; white noise
  Rnd:a = rand(1)     ; full amlitude
  ; Clip the noisy waveform's amplitude to 0.5
  a1 = clip(Rnd, p4, 0.5)
  outs(a1, a1)
endin

instr 2 ; white noise
  Rnd:a = rand(1)     ; full amlitude
  ; Clip the noisy waveform's amplitude to 0.1
  a1 = clip(Rnd, p4, 0.1)
  outs(a1, a1)
endin

</CsInstruments>
<CsScore>

; Play Instrument #1 for one second.
i 1 0 1 2
; Play Instrument #2 for one second.
i 2 1 1 2
s
; Play Instrument #1 for one second.
i 1 0 1 0
; Play Instrument #2 for one second.
i 2 1 1 0
s
; Play Instrument #1 for one second.
i 1 0 1 1
; Play Instrument #2 for one second.
i 2 1 1 1
e

</CsScore>
</CsoundSynthesizer>

Here is an example of the clip opcode. It uses the file clip.csd.

Example of the clip opcode.
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
-odac     ;;;RT audio out
;-iadc    ;;;uncomment -iadc if RT audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o clip.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

instr 1 ; white noise

arnd rand 1     ; full amlitude
; Clip the noisy waveform's amplitude to 0.5
a1 clip arnd, p4, 0.5
   outs a1, a1

endin

instr 2 ; white noise

arnd rand 1     ; full amlitude
; Clip the noisy waveform's amplitude to 0.1
a1 clip arnd, p4, 0.1
   outs a1, a1

endin

</CsInstruments>
<CsScore>

; Play Instrument #1 for one second.
i 1 0 1 2
; Play Instrument #2 for one second.
i 2 1 1 2
s
; Play Instrument #1 for one second.
i 1 0 1 0
; Play Instrument #2 for one second.
i 2 1 1 0
s
; Play Instrument #1 for one second.
i 1 0 1 1
; Play Instrument #2 for one second.
i 2 1 1 1
e

</CsScore>
</CsoundSynthesizer>

See also

Amplitude Modifiers and Dynamic processing

Waveshaping

Credits

Author: John ffitch
University of Bath, Codemist Ltd.
Bath, UK
August, 2000

New in Csound version 4.07

September 2009: Thanks to a note from Paolo Dell'Osso, corrected the formula.