pdhalf — Distorts a phasor for reading the two halves of a table at different rates.


The pdhalf opcode is designed to emulate the "classic" phase distortion synthesis method of the Casio CZ-series of synthesizers from the mid-1980's. This technique reads the first and second halves of a function table at different rates in order to warp the waveform. For example, pdhalf can smoothly transform a sine wave into something approximating the shape of a saw wave.


aout pdhalf ain, kShapeAmount [, ibipolar [, ifullscale]]


ibipolar -- an optional parameter specifying either unipolar (0) or bipolar (1) mode. Defaults to unipolar mode.

ifullscale -- an optional parameter specifying the range of input and output values. The maximum will be ifullscale. The minimum depends on the mode of operation: zero for unipolar or -ifullscale for bipolar. Defaults to 1.0 -- you should set this parameter to the maximum expected input value.


ain -- the input signal to be distorted.

aout -- the output signal.

kShapeAmount -- the amount of distortion applied to the input. Must be between negative one and one (-1 to 1). An amount of zero means no distortion.

Transfer function created by pdhalf and a negative kShapeAmount.

Transfer function created by pdhalf and a negative kShapeAmount.

The pdhalf opcode calculates a transfer function that is composed of two linear segments (see the graph). These segments meet at a "pivot point" which always lies on the same horizontal axis. (In unipolar mode, the axis is y = 0.5, and for bipolar mode it is the x axis). The kShapeAmount parameter specifies where on the horizontal axis this point falls. When kShapeAmount is zero, the pivot point is in the middle of the input range, forming a straight line for the transfer function and thus causing no change in the input signal. As kShapeAmount changes from zero (0) to negative one (-1), the pivot point moves towards the left side of the graph, producing a phase distortion pattern like the Casio CZ's "sawtooth waveform". As it changes from zero (0) to positive one (1), the pivot point moves toward the right, producing an inverted pattern.

If the input to pdhalf is a phasor and the output is used to index a table, values for kShapeAmount that are less than zero will cause the first half of the table to be read more quickly than the second half. The reverse is true for values of kShapeAmount greater than zero. The rates at which the halves are read are calculated so that the frequency of the phasor is unchanged. Thus, this method of phase distortion can only produce higher partials in a harmonic series. It cannot produce inharmonic sidebands in the way that frequency modulation does.

pdhalf can work in either unipolar or bipolar modes. Unipolar mode is appropriate for signals like phasors that range between zero and some maximum value (selectable with ifullscale). Bipolar mode is appropriate for signals that range above and below zero by roughly equal amounts such as most audio signals. Applying pdhalf directly to an audio signal in this way results in a crude but adjustable sort of waveshaping/distortion.

A typical example of the use of pdhalf is

    aphase    phasor    ifreq
    apd       pdhalf    aphase, kamount
    aout      tablei    apd, 1, 1

More information about Phase distortion synthesis can be found on Wikipedia at http://en.wikipedia.org/wiki/Phase_distortion_synthesis

See Also

pdhalfy, pdclip


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

Example 313. Example of the pdhalf opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

; Select audio/midi flags here according to platform
; Audio out   Audio in
-odac           -iadc    ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o abs.wav -W ;;; for file output any platform
; test instrument for pdhalf opcode
instr 4

    idur        = p3
    iamp        = p4
    ifreq       = p5
    itable      = p6
    aenv        linseg      0, .001, 1.0, idur - .051, 1.0, .05, 0
    aosc        phasor      ifreq
    kamount     linseg      0.0, 0.02, -0.99, 0.05, -0.9, idur-0.06, 0.0
    apd         pdhalf      aosc, kamount
    aout        tablei      apd, itable, 1
                out         aenv*aout*iamp      

f1 0 16385 10 1
f2 0 16385 10 1 .5 .3333 .25 .5
f3 0 16385  9 1 1 270           ; inverted cosine

; descending "just blues" scale

; pdhalf with cosine table
; (imitates the CZ-101 "sawtooth waveform")
t 0 100
i4 0 .333 10000 512     3
i. + .    .     448
i. + .    .     384
i. + .    .     358.4
i. + .    .     341.33
i. + .    .     298.67
i. + 2    .     256


Author: Anthony Kozar
January 2008

New in Csound version 5.08