fft, r2c and c2r
Date | 2015-08-20 15:46 |
From | Anders Genell |
Subject | fft, r2c and c2r |
Attachments | None None |
Dear list (and perhaps especially Dr Lazzarini as the resposible creator)!
As I am exploring avenues to plot fft:s in real time using Ed brilliant websockets opcode and HTML5/Javascript I have been trying to understand the also newly added fft opcode and accompanying c2r and r2c opcodes. I normally work with the fft implementation in GNU/Octave (or Matlab) where the transform is appied to a real time signal and results in a complex signal where the real and the imaginary parts are not used as much a their polar coordinates counterparts radius and angle, such that the radius is the amplitude and the angle is the phase of the signal. The csound implementation expects a complex signal upon which the fft would act, and one can be created using r2c, but the the imaginary part is said to be zero. Likewise to get a real signal from the complex result of the fft, using c2r will throw away the imaginary part. I am just trying to wrap my head around how to obtain an array of amplitude values that I can plot. Any hints? Regards, Anders |
Date | 2015-08-20 16:08 |
From | Victor Lazzarini |
Subject | Re: fft, r2c and c2r |
I think the best way is to use rfft() and then mags(). kSpec[] mags rfft(kInput) then you get the magnitude. Look at http://csound.github.io/docs/manual/rfft.html http://csound.github.io/docs/manual/mags.html ======================== Dr Victor Lazzarini Dean of Arts, Celtic Studies and Philosophy, Maynooth University, Maynooth, Co Kildare, Ireland Tel: 00 353 7086936 Fax: 00 353 1 7086952 > On 20 Aug 2015, at 15:46, Anders Genell |
Date | 2015-08-20 16:12 |
From | Anders Genell |
Subject | Re: fft, r2c and c2r |
Attachments | None None |
Ah! mags! I tries abs, but it didn't like arrays as argument... Thank you!! Regrads, Anders On Thu, Aug 20, 2015 at 5:08 PM, Victor Lazzarini <Victor.Lazzarini@nuim.ie> wrote: I think the best way is to use rfft() and then mags(). |
Date | 2015-08-20 16:16 |
From | "Dr. Richard Boulanger" |
Subject | Re: fft, r2c and c2r |
Attachments | None None |
NOTE: Can't run the examples from the manual in CsoundQt because of a parsing error. STARTING FILE Creating options Creating orchestra Creating score rtaudio: PortAudio module enabled ... using blocking interface error: syntax error, unexpected T_IDENT, expecting T_OPCODE or T_FUNCTION or ',' (token "irfft") from file /Library/Frameworks/CsoundLib64.framework/Resources/Manual/examples/mags.csd (1) line 59: >>> kRow[] irfft <<< Parsing failed due to invalid input! Stopping on parser failure cannot compile orchestra _____________________________________________ Dr. Richard Boulanger Professor of Electronic Production and Design Professional Writing and Music Technology Division Berklee College of Music 1140 Boylston Street, FB-75 Boston, MA 02215-3693 Office @ 161 Mass Ave - 4th Floor 617-747-2384 (office) 774-488-9166 (cell) ______________________________________________ rboulanger@berklee.edu (email) https://www.facebook.com/richard.boulanger.58 (facebook) ______________________________________________ President of Boulanger Labs - http://boulangerlabs.com Director of Csounds.com - http://csounds.com Author & Editor: The Csound Book - http://mitpress.mit.edu/books/csound-book Author & Editor: The Audio Programming Book - http://mitpress.mit.edu/books/audio-programming-book ______________________________________________ about: http://www.boulangerlabs.com/about/richardboulanger/ about: http://www.csounds.com/community/developers/dr-richard-boulanger/ ______________________________________________ music: http://www.csounds.com/community/developers/dr-richard-boulanger/dr-richard-boulanger-music/ On Thu, Aug 20, 2015 at 11:08 AM, Victor Lazzarini <Victor.Lazzarini@nuim.ie> wrote: I think the best way is to use rfft() and then mags(). |
Date | 2015-08-20 16:19 |
From | Victor Lazzarini |
Subject | Re: fft, r2c and c2r |
Just note that: 1) mags() is designed for rfft() out format (not fft()). 2) its output array is N/2 + 1 (half the input size + 1), to accommodate the Nyquist bin. Regards ======================== Dr Victor Lazzarini Dean of Arts, Celtic Studies and Philosophy, Maynooth University, Maynooth, Co Kildare, Ireland Tel: 00 353 7086936 Fax: 00 353 1 7086952 > On 20 Aug 2015, at 16:12, Anders Genell |
Date | 2015-08-20 16:20 |
From | Victor Lazzarini |
Subject | Re: fft, r2c and c2r |
Spelling error, irfft => rifft. I think it has been fixed in the manual sources already. ======================== Dr Victor Lazzarini Dean of Arts, Celtic Studies and Philosophy, Maynooth University, Maynooth, Co Kildare, Ireland Tel: 00 353 7086936 Fax: 00 353 1 7086952 > On 20 Aug 2015, at 16:16, Dr. Richard Boulanger |
Date | 2015-08-20 18:11 |
From | Ed Costello |
Subject | Re: fft, r2c and c2r |
Attachments | None None |
Here is an instrument that should do what you want, It sends audio from a vco2 opcode with the frequency altered by an lfo to an fft, gets the magnitudes and sends these on to the websocket, it also uses the new framebuffer opcode, but if you've built from sources you should have that. Ed instr 1 isize init 4096 ioverlaps init 4 klfo lfo 1, 1 kfreq = 10 asig vco2 0.5, 2000 + (1000 * klfo) kframe[] framebuffer asig, isize kwindowedFrame[] window kframe kspectrum[] rfft kwindowedFrame kmagnitudes[] mags kspectrum ;outs asig, asig iport init 8888 kinput websocket iport, kmagnitudes endin On Thu, 20 Aug 2015 at 17:21 Victor Lazzarini <Victor.Lazzarini@nuim.ie> wrote: Spelling error, irfft => rifft. I think it has been fixed in the manual sources already. -- Edward Costello
|
Date | 2015-08-20 18:27 |
From | Anders Genell |
Subject | Re: fft, r2c and c2r |
Attachments | None None |
Ha! I have almost the identical code here after having mashed together your we sockets example with the examples for rfft and mags :-) But your code is better structured and more versatile with the overlap option, so thank you very much!! I have a JavaScript plotting the raw mags on a <canvas> in HTML5 and can see the peak moving up and down, so now I just need to polish a bit to get proper decibel amplitudes and logarithmic frequency axis. Is it possible to send more than one array btw? It looks like the HTML5 end allows naming the incoming variable name used in the csd, so I assume it should be possible? I was thinking it would be good to be able to send more than just the raw magnitudes. Perhaps even create the frequency axis array in csound (expseg perhaps?) and send both? Regards, Anders
|
Date | 2015-08-20 18:42 |
From | Ed Costello |
Subject | Re: fft, r2c and c2r |
Attachments | None None |
Yeah the opcode accepts any number of inputs and outputs. There may be a limit as to how much data can be sent over websockets before it gets congested and you get buffer overruns, but I have made the buffers quite large so I wouldn't imagine there would be much of a problem. Ed On Thu, 20 Aug 2015 at 19:28 Anders Genell <anders.genell@gmail.com> wrote:
-- Edward Costello
|
Date | 2015-08-21 09:07 |
From | Anders Genell |
Subject | Re: fft, r2c and c2r |
Attachments | None None |
I'll give it a try, thanks! Oh, I can also report it is quite stable as I now have had it running for some 20 hours without a glitch... Regards,
On Thu, Aug 20, 2015 at 7:42 PM, Ed Costello <phasereset@gmail.com> wrote:
|
Date | 2015-08-21 09:56 |
From | Ed Costello |
Subject | Re: fft, r2c and c2r |
Attachments | None None |
Wow thats great, I never got to test it for very long, seems to be pretty solid then if it runs for 20hrs :) Cheers Ed On Fri, 21 Aug 2015 at 10:09 Anders Genell <anders.genell@gmail.com> wrote:
-- Edward Costello
|