These things should be done once, at the start of your program:
- Allocate memory for buffers, using code like
float *buffer = malloc(NumberOfElements * sizeof *buffer);
. - Create an FFT setup, using code like
FFTSetup setup = vDSP_create_fftsetup(log2n, FFT_RADIX2);
. - Also test the return values. If
malloc
orvDSP_create_fftsetup
returns 0, write an error message and exit the program or take other exception behavior.
These things should be done once, at the end of your program:
- Destroy the FFT setup, using code like
vDSP_destroy_fftsetup(setup);
. - Release the memory for the buffers, using code like
free(buffer);
.
In the middle of your program, while you are processing samples, the code should use the existing buffers and setup. So the variables pointing to the buffers and the setup must be visible to that code. You can either pass them in as parameters (perhaps grouped together in a struct) or make them global (which should be only a temporary solution for small programs).
Your program should be arranged so that it is never necessary to allocate memory or create an FFT setup while samples are being processed.
All memory that is allocated should be freed eventually.