2 Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
6 aubio is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 aubio is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with aubio. If not, see <http://www.gnu.org/licenses/>.
21 #include "aubio_priv.h"
24 #include "mathutils.h"
25 #include "spectral/fft.h"
27 #ifdef HAVE_FFTW3 // using FFTW3
28 /* note that <complex.h> is not included here but only in aubio_priv.h, so that
29 * c++ projects can still use their own complex definition. */
35 /** fft data type with complex.h and fftw3f */
36 #define FFTW_TYPE fftwf_complex
38 /** fft data type with complex.h and fftw3 */
39 #define FFTW_TYPE fftw_complex
43 /** fft data type without complex.h and with fftw3f */
44 #define FFTW_TYPE float
46 /** fft data type without complex.h and with fftw */
47 #define FFTW_TYPE double
52 typedef FFTW_TYPE fft_data_t;
55 #define fftw_malloc fftwf_malloc
56 #define fftw_free fftwf_free
57 #define fftw_execute fftwf_execute
58 #define fftw_plan_dft_r2c_1d fftwf_plan_dft_r2c_1d
59 #define fftw_plan_dft_c2r_1d fftwf_plan_dft_c2r_1d
60 #define fftw_plan_r2r_1d fftwf_plan_r2r_1d
61 #define fftw_plan fftwf_plan
62 #define fftw_destroy_plan fftwf_destroy_plan
67 #error "Using aubio in double precision with fftw3 in single precision"
68 #endif /* HAVE_AUBIO_DOUBLE */
70 #elif defined (HAVE_FFTW3) /* HAVE_FFTW3F */
71 #if !HAVE_AUBIO_DOUBLE
72 #error "Using aubio in single precision with fftw3 in double precision"
73 #endif /* HAVE_AUBIO_DOUBLE */
75 #endif /* HAVE_FFTW3F */
77 // a global mutex for FFTW thread safety
78 pthread_mutex_t aubio_fftw_mutex = PTHREAD_MUTEX_INITIALIZER;
80 #elif defined HAVE_ACCELERATE // using ACCELERATE
81 // https://developer.apple.com/library/mac/#documentation/Accelerate/Reference/vDSPRef/Reference/reference.html
82 #include <Accelerate/Accelerate.h>
84 #if !HAVE_AUBIO_DOUBLE
85 #define aubio_vDSP_ctoz vDSP_ctoz
86 #define aubio_vDSP_fft_zrip vDSP_fft_zrip
87 #define aubio_vDSP_ztoc vDSP_ztoc
88 #define aubio_vDSP_zvmags vDSP_zvmags
89 #define aubio_vDSP_zvphas vDSP_zvphas
90 #define aubio_vDSP_vsadd vDSP_vsadd
91 #define aubio_vDSP_vsmul vDSP_vsmul
92 #define aubio_vDSP_create_fftsetup vDSP_create_fftsetup
93 #define aubio_vDSP_destroy_fftsetup vDSP_destroy_fftsetup
94 #define aubio_DSPComplex DSPComplex
95 #define aubio_DSPSplitComplex DSPSplitComplex
96 #define aubio_FFTSetup FFTSetup
97 #define aubio_vvsqrt vvsqrtf
99 #define aubio_vDSP_ctoz vDSP_ctozD
100 #define aubio_vDSP_fft_zrip vDSP_fft_zripD
101 #define aubio_vDSP_ztoc vDSP_ztocD
102 #define aubio_vDSP_zvmags vDSP_zvmagsD
103 #define aubio_vDSP_zvphas vDSP_zvphasD
104 #define aubio_vDSP_vsadd vDSP_vsaddD
105 #define aubio_vDSP_vsmul vDSP_vsmulD
106 #define aubio_vDSP_create_fftsetup vDSP_create_fftsetupD
107 #define aubio_vDSP_destroy_fftsetup vDSP_destroy_fftsetupD
108 #define aubio_DSPComplex DSPDoubleComplex
109 #define aubio_DSPSplitComplex DSPDoubleSplitComplex
110 #define aubio_FFTSetup FFTSetupD
111 #define aubio_vvsqrt vvsqrt
112 #endif /* HAVE_AUBIO_DOUBLE */
114 #elif defined HAVE_INTEL_IPP // using INTEL IPP
121 // let's use ooura instead
122 extern void aubio_ooura_rdft(int, int, smpl_t *, int *, smpl_t *);
126 struct _aubio_fft_t {
130 #ifdef HAVE_FFTW3 // using FFTW3
133 fft_data_t * specdata; /* complex spectral data */
135 #elif defined HAVE_ACCELERATE // using ACCELERATE
137 aubio_FFTSetup fftSetup;
138 aubio_DSPSplitComplex spec;
141 #elif defined HAVE_INTEL_IPP // using Intel IPP
142 // mark FFT impl as Intel IPP
143 #define INTEL_IPP_FFT 1
148 #if HAVE_AUBIO_DOUBLE
149 struct FFTSpec_R_64f* fftSpec;
152 struct FFTSpec_R_32f* fftSpec;
159 #endif /* using OOURA */
164 aubio_fft_t * new_aubio_fft (uint_t winsize) {
165 aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
166 if ((sint_t)winsize < 2) {
167 AUBIO_ERR("fft: got winsize %d, but can not be < 2\n", winsize);
173 s->winsize = winsize;
174 /* allocate memory */
175 s->in = AUBIO_ARRAY(real_t,winsize);
176 s->out = AUBIO_ARRAY(real_t,winsize);
177 s->compspec = new_fvec(winsize);
179 pthread_mutex_lock(&aubio_fftw_mutex);
180 #ifdef HAVE_COMPLEX_H
181 s->fft_size = winsize/2 + 1;
182 s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
183 s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in, s->specdata, FFTW_ESTIMATE);
184 s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE);
186 s->fft_size = winsize;
187 s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
188 s->pfw = fftw_plan_r2r_1d(winsize, s->in, s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
189 s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
191 pthread_mutex_unlock(&aubio_fftw_mutex);
192 for (i = 0; i < s->winsize; i++) {
196 for (i = 0; i < s->fft_size; i++) {
200 #elif defined HAVE_ACCELERATE // using ACCELERATE
201 s->winsize = winsize;
202 s->fft_size = winsize;
203 s->compspec = new_fvec(winsize);
204 s->log2fftsize = aubio_power_of_two_order(s->fft_size);
205 s->in = AUBIO_ARRAY(smpl_t, s->fft_size);
206 s->out = AUBIO_ARRAY(smpl_t, s->fft_size);
207 s->spec.realp = AUBIO_ARRAY(smpl_t, s->fft_size/2);
208 s->spec.imagp = AUBIO_ARRAY(smpl_t, s->fft_size/2);
209 s->fftSetup = aubio_vDSP_create_fftsetup(s->log2fftsize, FFT_RADIX2);
211 #elif defined HAVE_INTEL_IPP // using Intel IPP
212 const IppHintAlgorithm qualityHint = ippAlgHintAccurate; // OR ippAlgHintFast;
213 const int flags = IPP_FFT_NODIV_BY_ANY; // we're scaling manually afterwards
214 int order = aubio_power_of_two_order(winsize);
215 int sizeSpec, sizeInit, sizeBuffer;
218 if (winsize <= 4 || aubio_is_power_of_two(winsize) != 1)
220 AUBIO_ERR("intel IPP fft: can only create with sizes > 4 and power of two, requested %d,"
221 " try recompiling aubio with --enable-fftw3\n", winsize);
225 #if HAVE_AUBIO_DOUBLE
226 status = ippsFFTGetSize_R_64f(order, flags, qualityHint,
227 &sizeSpec, &sizeInit, &sizeBuffer);
229 status = ippsFFTGetSize_R_32f(order, flags, qualityHint,
230 &sizeSpec, &sizeInit, &sizeBuffer);
232 if (status != ippStsNoErr) {
233 AUBIO_ERR("fft: failed to initialize fft. IPP error: %d\n", status);
236 s->fft_size = s->winsize = winsize;
237 s->compspec = new_fvec(winsize);
238 s->in = AUBIO_ARRAY(smpl_t, s->winsize);
239 s->out = AUBIO_ARRAY(smpl_t, s->winsize);
240 s->memSpec = ippsMalloc_8u(sizeSpec);
241 s->memBuffer = ippsMalloc_8u(sizeBuffer);
243 s->memInit = ippsMalloc_8u(sizeInit);
245 #if HAVE_AUBIO_DOUBLE
246 s->complexOut = ippsMalloc_64fc(s->fft_size / 2 + 1);
247 status = ippsFFTInit_R_64f(
248 &s->fftSpec, order, flags, qualityHint, s->memSpec, s->memInit);
250 s->complexOut = ippsMalloc_32fc(s->fft_size / 2 + 1);
251 status = ippsFFTInit_R_32f(
252 &s->fftSpec, order, flags, qualityHint, s->memSpec, s->memInit);
254 if (status != ippStsNoErr) {
255 AUBIO_ERR("fft: failed to initialize. IPP error: %d\n", status);
260 if (aubio_is_power_of_two(winsize) != 1) {
261 AUBIO_ERR("fft: can only create with sizes power of two, requested %d,"
262 " try recompiling aubio with --enable-fftw3\n", winsize);
265 s->winsize = winsize;
266 s->fft_size = winsize / 2 + 1;
267 s->compspec = new_fvec(winsize);
268 s->in = AUBIO_ARRAY(smpl_t, s->winsize);
269 s->out = AUBIO_ARRAY(smpl_t, s->winsize);
270 s->ip = AUBIO_ARRAY(int , s->fft_size);
271 s->w = AUBIO_ARRAY(smpl_t, s->fft_size);
273 #endif /* using OOURA */
282 void del_aubio_fft(aubio_fft_t * s) {
284 #ifdef HAVE_FFTW3 // using FFTW3
285 pthread_mutex_lock(&aubio_fftw_mutex);
286 fftw_destroy_plan(s->pfw);
287 fftw_destroy_plan(s->pbw);
288 fftw_free(s->specdata);
289 pthread_mutex_unlock(&aubio_fftw_mutex);
291 #elif defined HAVE_ACCELERATE // using ACCELERATE
292 AUBIO_FREE(s->spec.realp);
293 AUBIO_FREE(s->spec.imagp);
294 aubio_vDSP_destroy_fftsetup(s->fftSetup);
296 #elif defined HAVE_INTEL_IPP // using Intel IPP
299 ippFree(s->memBuffer);
300 ippFree(s->complexOut);
307 del_fvec(s->compspec);
313 void aubio_fft_do(aubio_fft_t * s, const fvec_t * input, cvec_t * spectrum) {
314 aubio_fft_do_complex(s, input, s->compspec);
315 aubio_fft_get_spectrum(s, s->compspec, spectrum);
318 void aubio_fft_rdo(aubio_fft_t * s, const cvec_t * spectrum, fvec_t * output) {
319 aubio_fft_get_realimag(s, spectrum, s->compspec);
320 aubio_fft_rdo_complex(s, s->compspec, output);
323 void aubio_fft_do_complex(aubio_fft_t * s, const fvec_t * input, fvec_t * compspec) {
325 #ifndef HAVE_MEMCPY_HACKS
326 for (i=0; i < s->winsize; i++) {
327 s->in[i] = input->data[i];
330 memcpy(s->in, input->data, s->winsize * sizeof(smpl_t));
331 #endif /* HAVE_MEMCPY_HACKS */
333 #ifdef HAVE_FFTW3 // using FFTW3
334 fftw_execute(s->pfw);
335 #ifdef HAVE_COMPLEX_H
336 compspec->data[0] = REAL(s->specdata[0]);
337 for (i = 1; i < s->fft_size -1 ; i++) {
338 compspec->data[i] = REAL(s->specdata[i]);
339 compspec->data[compspec->length - i] = IMAG(s->specdata[i]);
341 compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
342 #else /* HAVE_COMPLEX_H */
343 for (i = 0; i < s->fft_size; i++) {
344 compspec->data[i] = s->specdata[i];
346 #endif /* HAVE_COMPLEX_H */
348 #elif defined HAVE_ACCELERATE // using ACCELERATE
349 // convert real data to even/odd format used in vDSP
350 aubio_vDSP_ctoz((aubio_DSPComplex*)s->in, 2, &s->spec, 1, s->fft_size/2);
352 aubio_vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_FORWARD);
353 // convert from vDSP complex split to [ r0, r1, ..., rN, iN-1, .., i2, i1]
354 compspec->data[0] = s->spec.realp[0];
355 compspec->data[s->fft_size / 2] = s->spec.imagp[0];
356 for (i = 1; i < s->fft_size / 2; i++) {
357 compspec->data[i] = s->spec.realp[i];
358 compspec->data[s->fft_size - i] = s->spec.imagp[i];
361 smpl_t scale = 1./2.;
362 aubio_vDSP_vsmul(compspec->data, 1, &scale, compspec->data, 1, s->fft_size);
364 #elif defined HAVE_INTEL_IPP // using Intel IPP
367 #if HAVE_AUBIO_DOUBLE
368 ippsFFTFwd_RToCCS_64f(s->in, (Ipp64f*)s->complexOut, s->fftSpec, s->memBuffer);
370 ippsFFTFwd_RToCCS_32f(s->in, (Ipp32f*)s->complexOut, s->fftSpec, s->memBuffer);
372 // convert complex buffer to [ r0, r1, ..., rN, iN-1, .., i2, i1]
373 compspec->data[0] = s->complexOut[0].re;
374 compspec->data[s->fft_size / 2] = s->complexOut[s->fft_size / 2].re;
375 for (i = 1; i < s->fft_size / 2; i++) {
376 compspec->data[i] = s->complexOut[i].re;
377 compspec->data[s->fft_size - i] = s->complexOut[i].im;
380 #if HAVE_AUBIO_DOUBLE
381 ippsMulC_64f(compspec->data, 1.0 / 2.0, compspec->data, s->fft_size);
383 ippsMulC_32f(compspec->data, 1.0 / 2.0, compspec->data, s->fft_size);
387 aubio_ooura_rdft(s->winsize, 1, s->in, s->ip, s->w);
388 compspec->data[0] = s->in[0];
389 compspec->data[s->winsize / 2] = s->in[1];
390 for (i = 1; i < s->fft_size - 1; i++) {
391 compspec->data[i] = s->in[2 * i];
392 compspec->data[s->winsize - i] = - s->in[2 * i + 1];
394 #endif /* using OOURA */
397 void aubio_fft_rdo_complex(aubio_fft_t * s, const fvec_t * compspec, fvec_t * output) {
400 const smpl_t renorm = 1./(smpl_t)s->winsize;
401 #ifdef HAVE_COMPLEX_H
402 s->specdata[0] = compspec->data[0];
403 for (i=1; i < s->fft_size - 1; i++) {
404 s->specdata[i] = compspec->data[i] +
405 I * compspec->data[compspec->length - i];
407 s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1];
409 for (i=0; i < s->fft_size; i++) {
410 s->specdata[i] = compspec->data[i];
413 fftw_execute(s->pbw);
414 for (i = 0; i < output->length; i++) {
415 output->data[i] = s->out[i]*renorm;
418 #elif defined HAVE_ACCELERATE // using ACCELERATE
419 // convert from real imag [ r0, r1, ..., rN, iN-1, .., i2, i1]
420 // to vDSP packed format [ r0, rN, r1, i1, ..., rN-1, iN-1 ]
421 s->out[0] = compspec->data[0];
422 s->out[1] = compspec->data[s->winsize / 2];
423 for (i = 1; i < s->fft_size / 2; i++) {
424 s->out[2 * i] = compspec->data[i];
425 s->out[2 * i + 1] = compspec->data[s->winsize - i];
427 // convert to split complex format used in vDSP
428 aubio_vDSP_ctoz((aubio_DSPComplex*)s->out, 2, &s->spec, 1, s->fft_size/2);
430 aubio_vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_INVERSE);
431 // convert result to real output
432 aubio_vDSP_ztoc(&s->spec, 1, (aubio_DSPComplex*)output->data, 2, s->fft_size/2);
434 smpl_t scale = 1.0 / s->winsize;
435 aubio_vDSP_vsmul(output->data, 1, &scale, output->data, 1, s->fft_size);
437 #elif defined HAVE_INTEL_IPP // using Intel IPP
439 // convert from real imag [ r0, 0, ..., rN, iN-1, .., i2, i1, iN-1] to complex format
440 s->complexOut[0].re = compspec->data[0];
441 s->complexOut[0].im = 0;
442 s->complexOut[s->fft_size / 2].re = compspec->data[s->fft_size / 2];
443 s->complexOut[s->fft_size / 2].im = 0.0;
444 for (i = 1; i < s->fft_size / 2; i++) {
445 s->complexOut[i].re = compspec->data[i];
446 s->complexOut[i].im = compspec->data[s->fft_size - i];
448 #if HAVE_AUBIO_DOUBLE
450 ippsFFTInv_CCSToR_64f((const Ipp64f *)s->complexOut, output->data, s->fftSpec, s->memBuffer);
452 ippsMulC_64f(output->data, 1.0 / s->winsize, output->data, s->fft_size);
455 ippsFFTInv_CCSToR_32f((const Ipp32f *)s->complexOut, output->data, s->fftSpec, s->memBuffer);
457 ippsMulC_32f(output->data, 1.0f / s->winsize, output->data, s->fft_size);
458 #endif /* HAVE_AUBIO_DOUBLE */
461 smpl_t scale = 1.0 / s->winsize;
462 s->out[0] = compspec->data[0];
463 s->out[1] = compspec->data[s->winsize / 2];
464 for (i = 1; i < s->fft_size - 1; i++) {
465 s->out[2 * i] = compspec->data[i];
466 s->out[2 * i + 1] = - compspec->data[s->winsize - i];
468 aubio_ooura_rdft(s->winsize, -1, s->out, s->ip, s->w);
469 for (i=0; i < s->winsize; i++) {
470 output->data[i] = s->out[i] * scale;
475 void aubio_fft_get_spectrum(aubio_fft_t *s, const fvec_t * compspec, cvec_t * spectrum) {
476 aubio_fft_get_phas(s, compspec, spectrum);
477 aubio_fft_get_norm(s, compspec, spectrum);
480 void aubio_fft_get_realimag(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) {
481 aubio_fft_get_imag(s, spectrum, compspec);
482 aubio_fft_get_real(s, spectrum, compspec);
485 void aubio_fft_get_phas(aubio_fft_t *s, const fvec_t * compspec, cvec_t * spectrum) {
487 #ifdef INTEL_IPP_FFT // using Intel IPP FFT
490 // convert from real imag [ r0, 0, ..., rN, iN-1, .., i2, i1, iN-1] to complex format
491 s->complexOut[0].re = compspec->data[0];
492 s->complexOut[0].im = 0;
493 s->complexOut[s->fft_size / 2].re = compspec->data[s->fft_size / 2];
494 s->complexOut[s->fft_size / 2].im = 0.0;
495 for (i = 1; i < spectrum->length - 1; i++) {
496 s->complexOut[i].re = compspec->data[i];
497 s->complexOut[i].im = compspec->data[compspec->length - i];
500 #if HAVE_AUBIO_DOUBLE
501 IppStatus status = ippsPhase_64fc(s->complexOut, spectrum->phas, spectrum->length);
503 IppStatus status = ippsPhase_32fc(s->complexOut, spectrum->phas, spectrum->length);
505 if (status != ippStsNoErr) {
506 AUBIO_ERR("fft: failed to extract phase from fft. IPP error: %d\n", status);
509 #else // NOT using Intel IPP
511 if (compspec->data[0] < 0) {
512 spectrum->phas[0] = PI;
514 spectrum->phas[0] = 0.;
516 for (i=1; i < spectrum->length - 1; i++) {
517 spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i],
520 if (compspec->data[compspec->length/2] < 0) {
521 spectrum->phas[spectrum->length - 1] = PI;
523 spectrum->phas[spectrum->length - 1] = 0.;
528 void aubio_fft_get_norm(aubio_fft_t *s, const fvec_t * compspec, cvec_t * spectrum) {
530 spectrum->norm[0] = ABS(compspec->data[0]);
531 for (i=1; i < spectrum->length - 1; i++) {
532 spectrum->norm[i] = SQRT(SQR(compspec->data[i])
533 + SQR(compspec->data[compspec->length - i]) );
535 spectrum->norm[spectrum->length-1] =
536 ABS(compspec->data[compspec->length/2]);
539 void aubio_fft_get_imag(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) {
541 for (i = 1; i < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; i++) {
542 compspec->data[compspec->length - i] =
543 spectrum->norm[i]*SIN(spectrum->phas[i]);
547 void aubio_fft_get_real(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) {
549 for (i = 0; i < compspec->length / 2 + 1; i++) {
551 spectrum->norm[i]*COS(spectrum->phas[i]);