Skip to content

rfft

Fast Fourier Transform of a real-value array.

Applies a forward Fast Fourier Transform to a real-value input 1-dimensional array. The output is another array containing the transform, non-redundant, non-negative spectrum only.

In the case of the k-array version, the output array size will match the input size, with the first two points containing 0Hz and Nyquist frequency coefficients. This is the packed output format.

The Complex-array version, on the other hand, produces an array size that is one unit longer than the input (input size + 1 complex numbers), containing the Nyquist frequency coefficient in the last position of the Complex array.

Non-power-of-two transforms are limited to even sizes with not too many factors.

Syntax

out:k[] = rfft(in:k[])
out:Complex[] = rfft(in:k[])    
kout[] rfft kin[]
out:Complex[] rfft in:k[]    

Performance

kout[] -- output array containing the transform. It will be created if it does not exist.

kin[] -- input array containing the real-valued input.

Examples

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

Example of the rfft opcode.
<CsoundSynthesizer>

<CsOptions>
-d -o dac
</CsOptions>

<CsInstruments>
;ksmps needs to be an integer div of hopsize
ksmps = 64
0dbfs=1

instr 1

 ihopsize = 256   ; hopsize
 ifftsize = 1024  ; FFT size
 iolaps = ifftsize/ihopsize ; overlaps
 ibw = sr/ifftsize ; bin bandwidth
 kcnt init 0    ; counting vars
 krow init 0

 kOla[] init ifftsize ; overlap-add buffer
 kIn[] init ifftsize  ; input buffer
 kSw[] init ifftsize
 kOut[][] init iolaps, ifftsize ; output buffers

 a1 diskin2 "fox.wav",1,0,1 ; audio input
 ks  expon  100, p3, 1000
 asw vco2  0.15, ks

 /* every hopsize samples */
 if kcnt == ihopsize then
   /* window and take FFT */
   kWin[] window kIn,krow*ihopsize
   kSpec[] rfft kWin
   kWin window kSw,krow*ihopsize
   kSpec2[] rfft kWin
   kProd[] cmplxprod kSpec, kSpec2

   /* IFFT + window */
   kRow[] rifft kProd + kSpec
   kWin window kRow, krow*ihopsize
   /* place it on out buffer */
   kOut setrow kWin, krow

   /* zero the ola buffer */
   kOla = 0
   /* overlap-add */
   ki = 0
   until ki == iolaps do
     kRow getrow kOut, ki
     kOla = kOla + kRow
     ki += 1
   od

  /* update counters */
  krow = (krow+1)%iolaps
  kcnt = 0
 endif

 /* shift audio in/out of buffers */
 kIn shiftin a1
 kSw shiftin asw
 a2 shiftout kOla
    out a2/iolaps

 /* increment counter */
 kcnt += ksmps

endin

</CsInstruments>

<CsScore>
i1 0 10
</CsScore>

</CsoundSynthesizer>

Another example of the rfft opcode, now using a Complex-type array. It uses the file rfft_complex_type.csd.

Example of the rfft opcode (Complex array.
<CsoundSynthesizer>
<CsOptions>
-odac
</CsOptions>
<CsInstruments>
ksmps = 1024 // sets transform size
nchnls = 1
0dbfs = 1

instr 1
 Params:i[] = array(p4, p5)
 Audio:k[] = array(oscili(Params[0],Params[1]))
 Spec:Complex[] = rfft(Audio)
 Wave:k[] = rifft(Spec)
 out a(Wave)
endin
schedule(1,0,2,0.5,440)
event_i("e",0,2)

</CsInstruments>
<CsScore>
</CsScore>
</CsoundSynthesizer>

See Also

Vectorial opcodes

Array opcodes

Array-based spectral opcodes

Credits

Author: Victor Lazzarini
NUI Maynooth
2014, 2025

New in version 6.04
Complex type introduced in version 7.