12-06-2015, 07:17 PM | #1 |
Human being with feelings
Join Date: Nov 2011
Posts: 3,440
|
basic FFT questions
So, trying to wrap my head around FFT.
A basic DFT takes n samples and converts to n complex result values, where the frequency represented by each slot is given by n/windowsize*srate, and with an audio input signal it only makes sense to look up to result slot n/2 as the values above would not be useful... right? So, do I understand correctly that a series of real values as input (e.g. audio signal) still generates a series of complex values on output, but that in audio we don't generally care about the upper half of the output values? If so, is it the case that the JS function that reaper provides -- fft() -- takes n real samples and produces n/2 pairs of result values in the same buffer, where the first is the real component and the second is the imaginary? Thus after calling fft_permute, the 0'th pair (indices 0 and 1) is <dc_component, 0> and thereafter the i'th pair (indices 2*i and 2*i+1) where i goes from 1 to windowsize/2 - 1 is the <real, imaginary> FFT value of the signal at frequency i/windowsize*srate ? No fencepost errors there? And magnitude would be sqrt(results[2*i]^2 + results[2*i+1]^2), and would range from 0 to 1? I have read that phase is inverse_tan(imag/real), but in the JS I see where aa is the first results value and bb is the second... i assume the negation is just there for screen drawing purposes, but I would have expected (bb,aa) -- maybe fft() returns the values as <imaginary, real> ? Actually, it's looking increasingly like a bug in the JS. Update: yep, pretty sure; afaict -atan2(bb,aa) would be correct there. And yes, the - sign is just for drawing. Last one: arctan only ranges from -pi/2 to pi/2, but you'd need a total of 2*pi range (+/- 1*pi) to express all possible phase shifting... I would guess that when the real part is < 0 I need to add pi to the result to get the actual phase? So that would properly be something like: ang = atan2(bb,aa) + (aa < 0 ? 3.1415926 : 0); Yeah? Thanks in advance for any help!
__________________
Free blind ABX testing software | Audio illusions / psychoacoustics video series Music Theory Distilled -- a rapid crash course | How to do a portable install Last edited by clepsydrae; 12-20-2015 at 07:24 PM. |
12-07-2015, 12:09 AM | #2 |
Human being with feelings
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,715
|
I don't have time to answer all your questions right now, but I this explains atan2:
https://en.wikipedia.org/wiki/Atan2 |
12-07-2015, 02:14 PM | #3 | |
Human being with feelings
Join Date: Nov 2011
Posts: 3,440
|
Quote:
atan2(x,y) -- returns the Arc Tangent of x divided by y (return value is in radians)....so I was confused. I figured it was just some kind of obscure efficiency thing. Good to know about atan2. I will wait patiently for any other time you have for my other queries. Thanks! |
|
12-07-2015, 02:46 PM | #4 | |||
Human being with feelings
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,715
|
Quote:
Code:
i = 0; loop(n, complex_buf[2*i] = real_buf[i]; complex_buf[2*i+1] = 0; i += 1; ); fft(complex_buf, n); fft_permute(complex_buf, n); Quote:
Quote:
Nope, re=buf[2*i], im=buf[2*i+1]. |
|||
12-07-2015, 10:55 PM | #5 | ||
Human being with feelings
Join Date: Nov 2011
Posts: 3,440
|
Quote:
I see this, too "Note that fft()/ifft() must NOT cross at 65,536 item boundary, so be sure to specify the offset accordingly" -- is this referring to the memory space that JSFX have access to? So I want to make sure not to have the fft workspace cross over 0[65535*k] to 0[65536*k] ? The web docs mention rfft() and irfft() as being the "real" versions, but they are not listed in the in-program docs and are undefined functions... so, mere fantasy? Quote:
Various places on the web say that where mag = sqrt(real^2 + imag^2) and n is number of samples/bins, the amplitude is then 2*mag/n, but the in-program script docs in reaper say "Your inputs or outputs will need to be scaled down by 1/size, if used", so I assume that's just mag/n. So I'm wondering if I need/want the 2 in there, since the reaper docs don't mention it? Second thing is that the gfxanalyzer JSFX doesn't use n at all when determining the screen y value for drawing, even though it draws a dBFS-accurate curve. Simplifying/reducing, it does: floor=-120;...so it would seem that it is not using n at all in determining the screen y value so... how does that work out? Thanks for any ideas! |
||
12-08-2015, 02:09 AM | #6 | ||||
Human being with feelings
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,715
|
Quote:
Quote:
Quote:
Quote:
I think gfxanalyzer prescales the window by 1/fftsize. |
||||
12-19-2015, 01:50 AM | #7 | |
Human being with feelings
Join Date: Nov 2011
Posts: 3,440
|
Quote:
absvalue = 2*sqrt(real^2 + imag^2) |
|
12-20-2015, 07:22 PM | #8 | |
Human being with feelings
Join Date: Nov 2011
Posts: 3,440
|
Quote:
|
|
12-28-2015, 11:55 PM | #9 |
Human being with feelings
Join Date: Nov 2011
Posts: 3,440
|
And an update/clarification in case anyone ever finds this thread: I missed the fact that gfxanalyzer incorporates the 1/fftsize in the windowing function, as you apparently need to do because when using a non-unity ("rectangular") window you don't scale by 1/fftsize but a different value dependent on the window (one over the sum of the window values.) You apply the scaling (or the scaling+window) on the sample values before the fft. Then when you do the fft you can calculate the magnitude (in absolute scale, 0 to 1) as shown above.
Interestingly, though, you don't need to scale back up when coming the other way. Meaning, you put your sample values in the fft workspace array (paired with 0-value complex values) at 1/fftsize scale, do the fft, do fft_permute, do whatever you want to to the data, then fft_ipermute, then ifft, and the resulting real values in the workspace do not need to be scaled up by fftsize, they can just be used as-is. This quirk led to a lot of confusion on my part. Someone correct me if I've missed something else. :-) |
12-29-2015, 07:26 PM | #10 |
Administrator
Join Date: Jan 2005
Location: NYC
Posts: 16,117
|
Fixing some of the things pointed out here for the next builds, thanks!
|
12-29-2015, 07:44 PM | #11 | |
Human being with feelings
Join Date: Nov 2011
Posts: 3,440
|
Quote:
I presume you're aware of this thread, too: http://forum.cockos.com/showthread.php?t=168666 ... just makin' sure. :-) |
|
12-29-2015, 08:24 PM | #12 | |
Administrator
Join Date: Jan 2005
Location: NYC
Posts: 16,117
|
Quote:
|
|
Thread Tools | |
Display Modes | |
|
|