FNFT
Loading...
Searching...
No Matches
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#include <limits.h>
34
45 FNFT_UINT desired_length)
46{
47 // Also seems to work well with FFTW
48 return kiss_fft_next_fast_size(desired_length);
49}
50
59{
60 return NULL;
61}
62
85 fnft__fft_wrapper_plan_t * plan_ptr,
86 FNFT_UINT fft_length,
87 FNFT_COMPLEX * in,
88 FNFT_COMPLEX * out,
89 FNFT_INT is_inverse)
90{
91 if (plan_ptr == NULL)
92 return FNFT__E_INVALID_ARGUMENT(plan);
93 if (fft_length == 0)
94 return FNFT__E_INVALID_ARGUMENT(fft_length);
95 if (is_inverse != 1 && is_inverse != -1)
96 return FNFT__E_INVALID_ARGUMENT(is_inverse);
97
98 // Check if typecast to unsigned int is lossless
99#ifdef HAVE_FFTW3
100 if (fft_length>INT_MAX) // overflow
102 *plan_ptr = fftw_plan_dft_1d((int) fft_length, in, out, is_inverse, FFTW_ESTIMATE);
103#else
104 (void)in;
105 (void)out;
106 *plan_ptr = kiss_fft_alloc(fft_length,(FNFT_UINT)(is_inverse+1)/2, NULL, NULL);
107#endif
108
109 if (*plan_ptr == NULL)
110 return FNFT__E_NOMEM;
111 return FNFT_SUCCESS;
112}
113
130{
131 if (plan == NULL)
132 return FNFT__E_INVALID_ARGUMENT(plan);
133
134#ifdef HAVE_FFTW3
135 fftw_execute_dft((fftw_plan)plan, (fftw_complex *)in, (fftw_complex *)out);
136#else
137 kiss_fft((kiss_fft_cfg)plan, (kiss_fft_cpx *)in, (kiss_fft_cpx *)out);
138#endif
139
140 return FNFT_SUCCESS;
141}
142
156 fnft__fft_wrapper_plan_t * plan_ptr)
157{
158 if (plan_ptr == NULL)
159 return FNFT__E_INVALID_ARGUMENT(plan_ptr);
160#ifdef HAVE_FFTW3
161 fftw_destroy_plan(*plan_ptr);
162#else
163 KISS_FFT_FREE(*plan_ptr);
164#endif
166 return FNFT_SUCCESS;
167}
168
178static inline void * fnft__fft_wrapper_malloc(FNFT_UINT size)
179{
180#ifdef HAVE_FFTW3
181 return fftw_malloc(size);
182#else
183 return KISS_FFT_MALLOC(size);
184#endif
185}
186
196static inline void fnft__fft_wrapper_free(void * ptr)
197{
198#ifdef HAVE_FFTW3
199 fftw_free(ptr);
200#else
201 KISS_FFT_FREE(ptr);
202#endif
203}
204
205#ifdef FNFT_ENABLE_SHORT_NAMES
206#ifndef FNFT__FFT_WRAPPER_SHORT_NAMES
207#define FNFT__FFT_WRAPPER_SHORT_NAMES
208#define fft_wrapper_next_fft_length(...) fnft__fft_wrapper_next_fft_length(__VA_ARGS__)
209#define fft_wrapper_safe_plan_init(...) fnft__fft_wrapper_safe_plan_init(__VA_ARGS__)
210#define fft_wrapper_create_plan(...) fnft__fft_wrapper_create_plan(__VA_ARGS__)
211#define fft_wrapper_execute_plan(...) fnft__fft_wrapper_execute_plan(__VA_ARGS__)
212#define fft_wrapper_destroy_plan(...) fnft__fft_wrapper_destroy_plan(__VA_ARGS__)
213#define fft_wrapper_malloc(...) fnft__fft_wrapper_malloc(__VA_ARGS__)
214#define fft_wrapper_free(...) fnft__fft_wrapper_free(__VA_ARGS__)
215#endif
216#endif
217
#define FNFT_EC_ASSERTION_FAILED
Definition fnft_errwarn.h:94
#define FNFT_SUCCESS
Definition fnft_errwarn.h:44
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:128
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:84
static void fnft__fft_wrapper_free(void *ptr)
Memory deallocation for the FFT wrapper.
Definition fnft__fft_wrapper.h:196
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:44
static void * fnft__fft_wrapper_malloc(FNFT_UINT size)
Memory allocation for the FFT wrapper.
Definition fnft__fft_wrapper.h:178
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
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:155
static fnft__fft_wrapper_plan_t fnft__fft_wrapper_safe_plan_init()
Value to initialize plan variables.
Definition fnft__fft_wrapper.h:58
size_t FNFT_UINT
Definition fnft_numtypes.h:62
double complex FNFT_COMPLEX
Definition fnft_numtypes.h:47
int32_t FNFT_INT
Definition fnft_numtypes.h:56
#define FNFT__E_NOMEM
Definition fnft__errwarn.h:42
#define FNFT__E_INVALID_ARGUMENT(name)
Definition fnft__errwarn.h:48