[Csnd] trying to understand complex numbers in csound

I’ve never used these opcodes, the error message I get running:

instr 1
kIn line 0, p3, 1
kCmplx[] r2c kIn
endin

gives me a type error message

error: Unable to find opcode entry for ‘r2c’ with matching argument types:
Found: k[] r2c k
kCmplx r2c kIn …
Line: 11
from file string (1),Parsing failed due to syntax errors

my best guess is that it only works in combination with fft opcodes. But if that’s the case, then the error message is bit misleading?

The manual example (besides ifft not being an opcode but that’s easy to fix)
http://www.csounds.com/manual/html/c2r.html
does work.

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

You need an array as input. The reference manual is your friend

https://csound.com/manual/r2c.html

facepalm :slight_smile:

thanks!

» csound -z1 2>&1 | grep r2c
r2c         i[]         i[]
r2c         k[]         k[]

Csound mailing list Send bugs reports to Discussions of bugs and features can be posted here

Complex numbers? OH THANK YOU VICTOR.
Didn’t know there was such an opcode in there.
Love it.

-PBS

I have been attempting to reverse engineer the Nasca O. Paul’s padsynth algorithm with primitive csound operators, to get a better sense of the algorithm and the math behind it.

I’m currently stuck at understanding one thing in the c code and how I would convert it to csound.
It goes a bit into understanding trigonometry, in that cartesian x,y on a unit circle seem to be expressable in polar format with real and imaginary component.
Which format is expected in the fftinv,rifft is not properly documented, the examples all seem to use signals that have 0s in their imaginary indices (the odd numbered indices).
So I can’t tell if it’s supposed to be an imaginary number as radian from 0 → 2PI or some other unit?

So the padsynth algorithm uses an operator from libfftw

FFTFREQS fftfreqs;
fftfreqs.c[i]=freq_amp[i]*cos(freq_phase[i]);
fftfreqs.s[i]=freq_amp[i]*sin(freq_phase[i]);
planfftw_inv=rfftw_create_plan(fftsize,FFTW_COMPLEX_TO_REAL,FFTW_ESTIMATE|FFTW_IN_PLACE);

tmpfftdata1=new fftw_real[fftsize];
tmpfftdata2=new fftw_real[fftsize];
for (int i=0;i<fftsize/2;i++) {
tmpfftdata1[i]=freqs.c[i];
if (i!=0) tmpfftdata1[fftsize-i]=freqs.s[i];
};
rfftw_one(planfftw_inv,tmpfftdata1,tmpfftdata2);
fftw_execute(planfftw_inv);

So my question is, can I achieve the same operation using fftinv opcode, passing it a k-rate array of
k[n] = freq_amp[i]*cos(freq_phase[i]);
k[n + 1] = freq_amp[i]*sin(freq_phase[i]);
?

Or would I need to convert the units to some other format?

Sorry for repost, I noticed that I replied to the forum, I need to fix that :frowning:

I have been attempting to reverse engineer the Nasca O. Paul’s padsynth algorithm with primitive csound operators, to get a better sense of the algorithm and the math behind it.

I’m currently stuck at understanding one thing in the c code and how I would convert it to csound.
It goes a bit into understanding trigonometry, in that cartesian x,y on a unit circle seem to be expressable in polar format with real and imaginary component.
Which format is expected in the fftinv,rifft is not properly documented, the examples all seem to use signals that have 0s in their imaginary indices (the odd numbered indices).
So I can’t tell if it’s supposed to be an imaginary number as radian from 0 → 2PI or some other unit?

So the padsynth algorithm uses an operator from libfftw

FFTFREQS fftfreqs;
fftfreqs.c[i]=freq_amp[i]*cos(freq_phase[i]);
fftfreqs.s[i]=freq_amp[i]*sin(freq_phase[i]);
planfftw_inv=rfftw_create_plan(fftsize,FFTW_COMPLEX_TO_REAL,FFTW_ESTIMATE|FFTW_IN_PLACE);

tmpfftdata1=new fftw_real[fftsize];
tmpfftdata2=new fftw_real[fftsize];
for (int i=0;i<fftsize/2;i++) {
tmpfftdata1[i]=freqs.c[i];
if (i!=0) tmpfftdata1[fftsize-i]=freqs.s[i];
};
rfftw_one(planfftw_inv,tmpfftdata1,tmpfftdata2);
fftw_execute(planfftw_inv);

So my question is, can I achieve the same operation using fftinv opcode, passing it a k-rate array of
k[n] = freq_amp[i]*cos(freq_phase[i]);
k[n + 1] = freq_amp[i]*sin(freq_phase[i]);
?

Or would I need to convert the units to some other format?

I notice that these opcodes are explained well in the book Csound: A sound and music computin system, at chapter 14.4 where the opcodes mags and phs is used to converts cosine and sine to magnitude and phases. I can see better now how it relates. Perhaps I’ll figure this out, so nevermind that question.