This provides a vanilla radix-2 FFT out-of-place implementation and a test example.
This code was written by Robin Scheibler during rainy days in October 2017.
#include "fft.h" ... // Create the FFT config structure fft_config_t *real_fft_plan = fft_init(NFFT, FFT_REAL, FFT_FORWARD, NULL, NULL); // Fill array with some data for (k = 0 ; k < fft_analysis->size ; k++) real_fft_plan->input[k] = (float)k; // Execute transformation fft_execute(real_fft_plan); // Now do something with the output printf("DC component : %f\n", real_fft_plan->output); // DC is at  for (k = 1 ; k < real_fft_plan->size / 2 ; k++) printf("%d-th freq : %f+j%f\n", k, real_fft_plan->output[2*k], real_fft_plan->output[2*k+1]); printf("Middle component : %f\n", real_fft_plan->output); // N/2 is real and stored at  // Don't forget to clean up at the end to free all the memory that was allocated fft_destroy(real_fft_plan)
Create the FFT configuration by running
fft_config_t *fft_init(int size, fft_type_t type, fft_direction_t direction, float *input, float *output) Parameters ---------- size : int The FFT size (should be a power of two), if not, returns NULL. type : fft_type_t The type of FFT, FFT_REAL or FFT_COMPLEX direction : fft_direction_t The direction, FFT_FORWARD or FFT_BACKWARD (inverse transformation) input : float * A pointer to a buffer of the correct size. If NULL, a buffer is allocated dynamically output : float * A pointer to a buffer of the correct size. If NULL, a buffer is allocated dynamically. Returns ------- A pointer to an `fft_config_t` structure that holds pointers to the buffers and all the necessary configuration options.
Fill data in the
fft_executeto run the FFT
Use the transformed data located in the
Possibly free up memory by calling
fft_destroyon the configuration structure
Note about Inverse Real FFT
When doing an inverse real FFT, the data in the input buffer is destroyed.
Buffer sizes and data organization
FFT_REALFFTs (forward as well as backward) of size
NFFT, the buffer is of size
NFFT. Then, the input data is organized as
Input : [ x, x, x, ..., x[NFFT-1] ] Output : [ X, X[NFFT/2], Re(X), Im(X), ..., Re(X[NFFT/2-1]), Im(X[NFFT/2-1]) ]
NFFT, the buffer is of size
2 * NFFTas both real and imaginary parts should be saved.
Input : [ Re(x), Im(x), ..., Re(x[NFFT-1]), Im(x[NFFT-1]) ] Output : [ Re(X), Im(X), ..., Re(X[NFFT-1]), Im(X[NFFT-1]) ]
This software is released under the MIT license.
Copyright (c) 2017 Robin Scheibler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.