FNFT
fnft__fft_wrapper.h
Go to the documentation of this file.
1 /*
2  * This file is part of FNFT.
3  *
4  * FNFT is free software; you can redistribute it and/or
5  * modify it under the terms of the version 2 of the GNU General
6  * Public License as published by the Free Software Foundation.
7  *
8  * FNFT is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Contributors:
17  * Sander Wahls (TU Delft) 2018.
18  */
19 
32 #include "fnft__errwarn.h"
33 
44  FNFT_UINT desired_length)
45 {
46  // Also seems to work well with FFTW
47  return kiss_fft_next_fast_size(desired_length);
48 }
49 
58 {
59  return NULL;
60 }
61 
84  fnft__fft_wrapper_plan_t * plan_ptr,
85  FNFT_UINT fft_length,
86  FNFT_COMPLEX * in,
87  FNFT_COMPLEX * out,
88  FNFT_INT is_inverse)
89 {
90  if (plan_ptr == NULL)
91  return FNFT__E_INVALID_ARGUMENT(plan);
92  if (fft_length == 0)
93  return FNFT__E_INVALID_ARGUMENT(fft_length);
94  if (is_inverse != 1 && is_inverse != -1)
95  return FNFT__E_INVALID_ARGUMENT(is_inverse);
96 
97 #ifdef HAVE_FFTW3
98  *plan_ptr = fftw_plan_dft_1d(fft_length, in, out, is_inverse, FFTW_ESTIMATE);
99 #else
100  (void)in;
101  (void)out;
102  *plan_ptr = kiss_fft_alloc(fft_length, (is_inverse+1)/2, NULL, NULL);
103 #endif
104 
105  if (*plan_ptr == NULL)
106  return FNFT__E_NOMEM;
107  return FNFT_SUCCESS;
108 }
109 
126 {
127  if (plan == NULL)
128  return FNFT__E_INVALID_ARGUMENT(plan);
129 
130 #ifdef HAVE_FFTW3
131  fftw_execute_dft((fftw_plan)plan, (fftw_complex *)in, (fftw_complex *)out);
132 #else
133  kiss_fft((kiss_fft_cfg)plan, (kiss_fft_cpx *)in, (kiss_fft_cpx *)out);
134 #endif
135 
136  return FNFT_SUCCESS;
137 }
138 
152  fnft__fft_wrapper_plan_t * plan_ptr)
153 {
154  if (plan_ptr == NULL)
155  return FNFT__E_INVALID_ARGUMENT(plan_ptr);
156 #ifdef HAVE_FFTW3
157  fftw_destroy_plan(*plan_ptr);
158 #else
159  KISS_FFT_FREE(*plan_ptr);
160 #endif
161  *plan_ptr = fnft__fft_wrapper_safe_plan_init();
162  return FNFT_SUCCESS;
163 }
164 
174 static inline void * fnft__fft_wrapper_malloc(FNFT_UINT size)
175 {
176 #ifdef HAVE_FFTW3
177  return fftw_malloc(size);
178 #else
179  return KISS_FFT_MALLOC(size);
180 #endif
181 }
182 
192 static inline void fnft__fft_wrapper_free(void * ptr)
193 {
194 #ifdef HAVE_FFTW3
195  fftw_free(ptr);
196 #else
197  KISS_FFT_FREE(ptr);
198 #endif
199 }
200 
201 #ifdef FNFT_ENABLE_SHORT_NAMES
202 #ifndef FNFT__FFT_WRAPPER_SHORT_NAMES
203 #define FNFT__FFT_WRAPPER_SHORT_NAMES
204 #define fft_wrapper_next_fft_length(...) fnft__fft_wrapper_next_fft_length(__VA_ARGS__)
205 #define fft_wrapper_safe_plan_init(...) fnft__fft_wrapper_safe_plan_init(__VA_ARGS__)
206 #define fft_wrapper_create_plan(...) fnft__fft_wrapper_create_plan(__VA_ARGS__)
207 #define fft_wrapper_execute_plan(...) fnft__fft_wrapper_execute_plan(__VA_ARGS__)
208 #define fft_wrapper_destroy_plan(...) fnft__fft_wrapper_destroy_plan(__VA_ARGS__)
209 #define fft_wrapper_malloc(...) fnft__fft_wrapper_malloc(__VA_ARGS__)
210 #define fft_wrapper_free(...) fnft__fft_wrapper_free(__VA_ARGS__)
211 #endif
212 #endif
213 
fnft__fft_wrapper_malloc
static void * fnft__fft_wrapper_malloc(FNFT_UINT size)
Memory allocation for the FFT wrapper.
Definition: fnft__fft_wrapper.h:174
FNFT_INT
int32_t FNFT_INT
Definition: fnft_numtypes.h:56
fnft__fft_wrapper_free
static void fnft__fft_wrapper_free(void *ptr)
Memory deallocation for the FFT wrapper.
Definition: fnft__fft_wrapper.h:192
FNFT_UINT
size_t FNFT_UINT
Definition: fnft_numtypes.h:62
fnft__fft_wrapper_plan_t
kiss_fft_cfg fnft__fft_wrapper_plan_t
Stores information needed by fnft__fft_wrapper_execute_plan to perform a (inverse) FFT.
Definition: fnft__fft_wrapper_plan_t.h:41
fnft__fft_wrapper_execute_plan
static FNFT_INT fnft__fft_wrapper_execute_plan(fnft__fft_wrapper_plan_t plan, FNFT_COMPLEX *in, FNFT_COMPLEX *out)
Computes a fast Fourier transform (FFT).
Definition: fnft__fft_wrapper.h:124
FNFT__E_INVALID_ARGUMENT
#define FNFT__E_INVALID_ARGUMENT(name)
Definition: fnft__errwarn.h:48
fnft__errwarn.h
fnft__fft_wrapper_create_plan
static FNFT_INT fnft__fft_wrapper_create_plan(fnft__fft_wrapper_plan_t *plan_ptr, FNFT_UINT fft_length, FNFT_COMPLEX *in, FNFT_COMPLEX *out, FNFT_INT is_inverse)
Prepares a new (inverse) fast Fourier transform (FFT).
Definition: fnft__fft_wrapper.h:83
FNFT__E_NOMEM
#define FNFT__E_NOMEM
Definition: fnft__errwarn.h:42
FNFT_SUCCESS
#define FNFT_SUCCESS
Definition: fnft_errwarn.h:44
fnft__fft_wrapper_next_fft_length
static FNFT_UINT fnft__fft_wrapper_next_fft_length(FNFT_UINT desired_length)
Next valid number of samples for the FFT routines.
Definition: fnft__fft_wrapper.h:43
FNFT_COMPLEX
double complex FNFT_COMPLEX
Definition: fnft_numtypes.h:47
fnft__fft_wrapper_safe_plan_init
static fnft__fft_wrapper_plan_t fnft__fft_wrapper_safe_plan_init()
Value to initialize plan variables.
Definition: fnft__fft_wrapper.h:57
fnft__fft_wrapper_destroy_plan
static FNFT_INT fnft__fft_wrapper_destroy_plan(fnft__fft_wrapper_plan_t *plan_ptr)
Destroys a FFT plan when it is no longer needed.
Definition: fnft__fft_wrapper.h:151
fnft__fft_wrapper_plan_t.h