Cabbage Logo
Back to Cabbage Site

Display FFT correctly

Hi everyone,

I am stuck a bit with my code. I don’t get any failures from csound/cabbage but the FFT isn’t displayed in cabbage gui. Here is what I got so far:

<Cabbage>
form size(500, 500), caption("SignalDisplay"), guiMode("queue"), pluginId("SigD") 
gentable bounds(12, 10, 370, 250), tablenumber(1), tablebackgroundcolour("white"), 
tablecolour:0("blue"), identchannel("table1")
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -m0d  --displays
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

instr 1
iFFTsize = 8192 ; size of FFT
iOverlap = iFFTsize / 4 ; overlap of FFT
iWinSize = iFFTsize ; window size
iWinType = 1 ; window type (1 = Hamming)

aSignal diskin2 "1khz.wav" ; read the .wav file
fSig pvsanal aSignal, iFFTsize, iOverlap, iWinSize, iWinType ; perform FFT

; write the FFT to a function table
    karr[] init 1026
   kframe pvs2tab  karr, fSig
    tablew	kframe,1,1

endin
</CsInstruments>
<CsScore>
f  1 0 16384 -2 0 ; create a function table to hold the FFT data
i 1 0 100 ; start instrument 1
 </CsScore>
 </CsoundSynthesizer>

thanks
//rootnote

First, it might help to print values like kframe, array & table as you go to test values and see what the code is doing. As it stands you’re only writing the kframe number to the second index (1) of the table.

Also, see:
https://flossmanual.csound.com/csound-language/arrays

Example 03E07

I think possibly what you want is to copy the array/arrays to a table/tables.

<CsInstruments>

sr = 48000
ksmps = 32
nchnls = 1
0dbfs  = 1

instr 1
iFFTsize = 8192 ; size of FFT
iOverlap = iFFTsize / 4 ; overlap of FFT
iWinSize = iFFTsize ; window size
iWinType = 1 ; window type (1 = Hamming)

aSignal diskin "fox.wav", 1, 0, 1 ; read the .wav file
fSig pvsanal aSignal, iFFTsize, iOverlap, iWinSize, iWinType ; perform FFT

; write the FFT to a function table
    karr[] init iFFTsize/2 + 2
    karr2[] init iFFTsize/2 + 2
    kframe pvs2tab  karr, karr2, fSig
    printk2 kframe
;    printarray karr, metro(1)
    if changed(kframe) == 1 then
        copya2ftab karr, 1
        copya2ftab karr2, 2
    endif
    ares pvsynth fSig
    out ares
endin

instr 2
  ftprint 1
  ftprint 2
endin 
</CsInstruments>
<CsScore>
f 1 0 -4097 -2 0 ; create a function table to hold the FFT data
f 2 0 -4097 -2 0
i 1 0 10 ; start instrument 1
i 2 1 1
</CsScore>
</CsoundSynthesizer>

I haven’t tested this in Cabbage but may help point in the right direction. In this case looks (?) like karr/table 1 is the bin amplitudes, karr2/table 2 is the frequencies. I’ve never tried using gentable so clueless there.

1 Like

I think you need to call
cabbageSet “table1”, “tableNumber”, 1 ; update table display

… but that is i-rate, so perhaps add (off the top of my head, not tested)
cabbageSet kframe, “envelope1”, “tableNumber”, 1 ; update table display