in_real: pointer to real-valued spatial samples (for audio, this is where your entire audio signal goes)
in_imag: pointer to imaginary-valued ones (not useful for audio)
in_imag is allowed to be nullptr. If so, it will be treated as if it were all zeroes.
size: number of complex samples per domain. for audio, this is the number of real samples you have. must be a power of 2. Algorithm will definitely fail and possibly crash otherwise, not tested.
gap: must be 1 for outside callers. used for recursion.
out_real: pointer to space for real-valued output. does not need to be initialized. must be allocated.
out_imag: same as above, for imaginary. not optional.
out_real and out_imag work together to store a complex number (2d vector) representing the phase and amplitude of the given frequency band, even for wholly real inputs.
forwards: if true, transform is forwards (fft). if false, transform is backwards (ifft).
fft
(<same as fft_core, sans [gap] and [forwards]>)
compute forwards fft.
ifft
(<same as fft_core, sans [gap] and [forwards]>)
compute backwards fft (inverse fft, ifft)
normalize_fft
(in_real[], in_imag[], size)
divide the amplitude of each bin by the number of bins. obligatory after fft() for audio. modifies the input.
sanitize_fft
(in_real[], in_imag[], size)
moves all data to positive-frequency bins. yes, FFTs have negative frequencies for some reason. they're used to retain correlation data for complex inputs. for real inputs, the negative frequencies just mirror the positive ones and sap half their amplitude, therefore this function. for an explanation of what negative frequencies mean, see http://dsp.stackexchange.com/questions/431/what-is-the-physical-significance-of-negative-frequencies .
unsanitize_fft
(in_real[], in_imag[], size)
undo the above. note again that these two fuctions are not sensical for complex inputs.
*/
#include<stdlib.h>
#include<stdint.h>
#include<math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif // ifndef M_PI
// address of cell if base adderss not nullptr, nullptr otherwise
// For a 8-sample input, the FFT's last three bins contain "negative" frequencies. (So, the last (size/2)-1 bins.) They are only meaningful for complex inputs.
// This algorithm works by extending the concept of how two-bin DFTs (discrete fourier transform) work, in order to correlate decimated DFTs, recursively.
// No, I'm not your guy if you want a proof of why it works, but it does.