FFT Overview:
Filter:
Guides | UGens > FFT

FFT Overview

Overview of the Fast Fourier Transform (FFT) UGens

FFT and IFFT

SuperCollider implements a number of UGens supporting Fast Fourier Transform (FFT) based processing. The most basic of these are FFT and IFFT (inverse-FFT) which convert data between the time and frequency domains:

FFT requires a Buffer or LocalBuf. The buffer's size must correspond to a power of 2, and must also be a multiple of the server's current block size. The window size is equivalent to the buffer size, and the window overlap defaults to 2 (hop = 0.5). Both FFT and IFFT use a sine window by default. Their combination efficiently becomes a Hanning window (i.e. raised sine, that is, sine squared).

How FFT UGens communicate

FFT stores spectral data in the buffer, in the following format:

DCnyquistreal 1fimag 1freal 2fimag 2f...real (N-1)fimag (N-1)f

where f is the frequency corresponding to the window size, and N is the window size / 2.

The FFT UGen returns a signal (usually called chain) is constant at -1, only when a new FFT window starts, the signal equals the buffer number. This is how subsequent FFT UGens can write to that buffer and know when to do this. The FFT information is not in the chain signal, but in the buffer.

Phase Vocoder UGens and Spectral Processing

In between an FFT and an IFFT one can chain together a number of Phase Vocoder UGens (i.e. PV_...) to manipulate blocks of spectral data before reconversion. The process of buffering the appropriate amount of audio, windowing, conversion, overlap-add, etc. is handled automatically.

See PV and FFT UGens in the Standard Library for a list of UGens.

In order to expand PV UGens for a multichannel input signal, an appropriate array of buffers must be provided (not a multichannel buffer):

For more examples, see Multichannel Expansion with FFT UGens

Parallel FFT chains

PV UGens write their output data in place, i.e. back into the same buffer from which they read. PV UGens which require two buffers write their data into the first buffer, usually called 'bufferA'.

A similar example using a soundfile:

Copying FFT chains

Because each PV UGen overwrites the output of the previous one, it is necessary to copy the data to an additional buffer at the desired point in the chain in order to do parallel processing of input without using multiple FFT UGens. PV_Copy allows for this.

NOTE: As of SC 3.7 instances of PV_Copy are added automatically where necessary for parallel processing. Existing code explicitly using PV_Copy should continue to work.

PV processes can also share a single FFT UGen to process a signal in parallel. In the following example, 'chain0' and 'chain1' share the same FFT UGen. SuperCollider automatically copies the FFT data from 'chain' into hidden LocalBufs inside the Synth. In the following example, if the PV_PhaseShift UGen were operating directly on chainA, then the two IFFT units would produce the same signal, which, when added together, would reinforce each other. Instead, the sound is nearly silent -- proving that chainB is in a different buffer, even though the function does not explicitly create it.

Plotting magnitudes

Note that PV UGens convert as needed between cartesian (complex) and polar representations, therefore when using multiple PV UGens it may be impossible to know in which form the values will be at any given time. FFT produces complex output (see above). The following, however, returns a reliable magnitude plot:

UGen access to FFT data

It is possible to manipulate the FFT data directly within a synth graph (if there doesn't already exist a PV UGen which will do what you want), using the methods pvcalc, pvcalc2, pvcollect. Here's an example which uses the methods SequenceableCollection: -clump and SequenceableCollection: -flop to rearrange the order of the spectral bins:

Multichannel Expansion with FFT UGens

Care must be taken when using multichannel expansion with FFT UGens, as they require separate buffers. Code such as this can be deceptive:

The above may seem to work, but does not. It does result in two FFT UGens, but as they both write to the same buffer, the second simply overwrites the data from the first, thus wasting CPU and accomplishing nothing.

When using multichannel expansion with FFT UGens it is necessary to ensure that each one writes to a different buffer. Here's an example of one way to do this:

Note that dup on a UGen just makes a reference to that UGen, because UGen defines -copy to simply return the receiver. (See UGen for more detail.)

Code like IFFT(chain).dup is found throughout the PV help files , and is just a convenient way to copy a mono signal to stereo, without further computation.

See also Multichannel Expansion.

PV and FFT UGens in the Standard Library

The following PV UGens are included in the standard SC distribution:

FFT
Fast Fourier Transform
IFFT
Inverse Fast Fourier Transform
PV_Add
complex addition
PV_BinScramble
scramble bins
PV_BinShift
shift and stretch bin position
PV_BinWipe
combine low and high bins from two inputs
PV_BrickWall
zero bins
PV_ConformalMap
complex plane attack
PV_CopyPhase
copy magnitudes and phases
PV_Diffuser
random phase shifting
PV_HainsworthFoote
onset detection
PV_JensenAndersen
onset detection
PV_LocalMax
pass bins which are a local maximum
PV_MagAbove
pass bins above a threshold
PV_MagBelow
pass bins below a threshold
PV_MagClip
clip bins to a threshold
PV_MagFreeze
freeze magnitudes
PV_MagMul
multiply magnitudes
PV_MagDiv
division of magnitudes
PV_MagNoise
multiply magnitudes by noise
PV_MagShift
shift and stretch magnitude bin position
PV_MagSmear
average magnitudes across bins
PV_MagSquared
square magnitudes
PV_Max
maximum magnitude
PV_Min
minimum magnitude
PV_Mul
complex multiply
PV_PhaseShift
shift phase of all bins
PV_PhaseShift270
shift phase by 270 degrees
PV_PhaseShift90
shift phase by 90 degrees
PV_RandComb
pass random bins
PV_RandWipe
crossfade in random bin order
PV_RectComb
make gaps in spectrum
PV_RectComb2
make gaps in spectrum
UnpackFFT, PackFFT, Unpack1FFT
"unpacking" components used in pvcalc, pvcalc2, pvcollect (can also be used on their own)

For a full list of FFT UGens, see UGens>FFT in the Browse: UGens>FFT page.