source: src/spectral/fft.c @ d3d72b7

feature/cnnfeature/crepe
Last change on this file since d3d72b7 was 01d4d19, checked in by Paul Brossier <piem@piem.org>, 6 years ago

Merge branch 'fix/oddfft' (closes #207)

  • Property mode set to 100644
File size: 19.1 KB
Line 
1/*
2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
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.
10
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.
15
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/>.
18
19*/
20
21#include "aubio_priv.h"
22#include "fvec.h"
23#include "cvec.h"
24#include "mathutils.h"
25#include "spectral/fft.h"
26
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. */
30#include <fftw3.h>
31#include <pthread.h>
32
33#ifdef HAVE_COMPLEX_H
34#ifdef HAVE_FFTW3F
35/** fft data type with complex.h and fftw3f */
36#define FFTW_TYPE fftwf_complex
37#else
38/** fft data type with complex.h and fftw3 */
39#define FFTW_TYPE fftw_complex
40#endif
41#else
42#ifdef HAVE_FFTW3F
43/** fft data type without complex.h and with fftw3f */
44#define FFTW_TYPE float
45#else
46/** fft data type without complex.h and with fftw */
47#define FFTW_TYPE double
48#endif
49#endif
50
51/** fft data type */
52typedef FFTW_TYPE fft_data_t;
53
54#ifdef HAVE_FFTW3F
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
63#endif
64
65#ifdef HAVE_FFTW3F
66#if HAVE_AUBIO_DOUBLE
67#error "Using aubio in double precision with fftw3 in single precision"
68#endif /* HAVE_AUBIO_DOUBLE */
69#define real_t float
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 */
74#define real_t double
75#endif /* HAVE_FFTW3F */
76
77// a global mutex for FFTW thread safety
78pthread_mutex_t aubio_fftw_mutex = PTHREAD_MUTEX_INITIALIZER;
79
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>
83
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_DSPComplex               DSPComplex
93#define aubio_DSPSplitComplex          DSPSplitComplex
94#define aubio_vDSP_DFT_Setup           vDSP_DFT_Setup
95#define aubio_vDSP_DFT_zrop_CreateSetup vDSP_DFT_zrop_CreateSetup
96#define aubio_vDSP_DFT_Execute         vDSP_DFT_Execute
97#define aubio_vDSP_DFT_DestroySetup    vDSP_DFT_DestroySetup
98#define aubio_vvsqrt                   vvsqrtf
99#else
100#define aubio_vDSP_ctoz                vDSP_ctozD
101#define aubio_vDSP_fft_zrip            vDSP_fft_zripD
102#define aubio_vDSP_ztoc                vDSP_ztocD
103#define aubio_vDSP_zvmags              vDSP_zvmagsD
104#define aubio_vDSP_zvphas              vDSP_zvphasD
105#define aubio_vDSP_vsadd               vDSP_vsaddD
106#define aubio_vDSP_vsmul               vDSP_vsmulD
107#define aubio_DSPComplex               DSPDoubleComplex
108#define aubio_DSPSplitComplex          DSPDoubleSplitComplex
109#define aubio_vDSP_DFT_Setup           vDSP_DFT_SetupD
110#define aubio_vDSP_DFT_zrop_CreateSetup vDSP_DFT_zrop_CreateSetupD
111#define aubio_vDSP_DFT_Execute         vDSP_DFT_ExecuteD
112#define aubio_vDSP_DFT_DestroySetup    vDSP_DFT_DestroySetupD
113#define aubio_vvsqrt                   vvsqrt
114#endif /* HAVE_AUBIO_DOUBLE */
115
116#elif defined HAVE_INTEL_IPP // using INTEL IPP
117
118#if !HAVE_AUBIO_DOUBLE
119#define aubio_IppFloat                 Ipp32f
120#define aubio_IppComplex               Ipp32fc
121#define aubio_FFTSpec                  FFTSpec_R_32f
122#define aubio_ippsMalloc_complex       ippsMalloc_32fc
123#define aubio_ippsFFTInit_R            ippsFFTInit_R_32f
124#define aubio_ippsFFTGetSize_R         ippsFFTGetSize_R_32f
125#define aubio_ippsFFTInv_CCSToR        ippsFFTInv_CCSToR_32f
126#define aubio_ippsFFTFwd_RToCCS        ippsFFTFwd_RToCCS_32f
127#define aubio_ippsAtan2                ippsAtan2_32f_A21
128#else /* HAVE_AUBIO_DOUBLE */
129#define aubio_IppFloat                 Ipp64f
130#define aubio_IppComplex               Ipp64fc
131#define aubio_FFTSpec                  FFTSpec_R_64f
132#define aubio_ippsMalloc_complex       ippsMalloc_64fc
133#define aubio_ippsFFTInit_R            ippsFFTInit_R_64f
134#define aubio_ippsFFTGetSize_R         ippsFFTGetSize_R_64f
135#define aubio_ippsFFTInv_CCSToR        ippsFFTInv_CCSToR_64f
136#define aubio_ippsFFTFwd_RToCCS        ippsFFTFwd_RToCCS_64f
137#define aubio_ippsAtan2                ippsAtan2_64f_A50
138#endif
139
140
141#else // using OOURA
142// let's use ooura instead
143extern void aubio_ooura_rdft(int, int, smpl_t *, int *, smpl_t *);
144
145#endif
146
147struct _aubio_fft_t {
148  uint_t winsize;
149  uint_t fft_size;
150
151#ifdef HAVE_FFTW3             // using FFTW3
152  real_t *in, *out;
153  fftw_plan pfw, pbw;
154  fft_data_t * specdata; /* complex spectral data */
155
156#elif defined HAVE_ACCELERATE  // using ACCELERATE
157  aubio_vDSP_DFT_Setup fftSetupFwd;
158  aubio_vDSP_DFT_Setup fftSetupBwd;
159  aubio_DSPSplitComplex spec;
160  smpl_t *in, *out;
161
162#elif defined HAVE_INTEL_IPP  // using Intel IPP
163  smpl_t *in, *out;
164  Ipp8u* memSpec;
165  Ipp8u* memInit;
166  Ipp8u* memBuffer;
167  struct aubio_FFTSpec* fftSpec;
168  aubio_IppComplex* complexOut;
169#else                         // using OOURA
170  smpl_t *in, *out;
171  smpl_t *w;
172  int *ip;
173#endif /* using OOURA */
174
175  fvec_t * compspec;
176};
177
178aubio_fft_t * new_aubio_fft (uint_t winsize) {
179  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
180  if ((sint_t)winsize < 2) {
181    AUBIO_ERR("fft: got winsize %d, but can not be < 2\n", winsize);
182    goto beach;
183  }
184
185#ifdef HAVE_FFTW3
186  uint_t i;
187  s->winsize  = winsize;
188  /* allocate memory */
189  s->in       = AUBIO_ARRAY(real_t,winsize);
190  s->out      = AUBIO_ARRAY(real_t,winsize);
191  s->compspec = new_fvec(winsize);
192  /* create plans */
193  pthread_mutex_lock(&aubio_fftw_mutex);
194#ifdef HAVE_COMPLEX_H
195  s->fft_size = winsize/2 + 1;
196  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
197  s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in,  s->specdata, FFTW_ESTIMATE);
198  s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE);
199#else
200  s->fft_size = winsize;
201  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
202  s->pfw = fftw_plan_r2r_1d(winsize, s->in,  s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
203  s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
204#endif
205  pthread_mutex_unlock(&aubio_fftw_mutex);
206  for (i = 0; i < s->winsize; i++) {
207    s->in[i] = 0.;
208    s->out[i] = 0.;
209  }
210  for (i = 0; i < s->fft_size; i++) {
211    s->specdata[i] = 0.;
212  }
213
214#elif defined HAVE_ACCELERATE  // using ACCELERATE
215  {
216    uint_t radix = winsize;
217    uint_t order = 0;
218    while ((radix / 2) * 2 == radix) {
219      radix /= 2;
220      order++;
221    }
222    if (order < 4 || (radix != 1 && radix != 3 && radix != 5 && radix != 15)) {
223      AUBIO_ERR("fft: vDSP/Accelerate supports FFT with sizes = "
224          "f * 2 ** n, where n > 4 and f in [1, 3, 5, 15], but requested %d. "
225          "Use the closest power of two, or try recompiling aubio with "
226          "--enable-fftw3.\n", winsize);
227      goto beach;
228    }
229  }
230  s->winsize = winsize;
231  s->fft_size = winsize;
232  s->compspec = new_fvec(winsize);
233  s->in = AUBIO_ARRAY(smpl_t, s->fft_size);
234  s->out = AUBIO_ARRAY(smpl_t, s->fft_size);
235  s->spec.realp = AUBIO_ARRAY(smpl_t, s->fft_size/2);
236  s->spec.imagp = AUBIO_ARRAY(smpl_t, s->fft_size/2);
237  s->fftSetupFwd = aubio_vDSP_DFT_zrop_CreateSetup(NULL,
238      s->fft_size, vDSP_DFT_FORWARD);
239  s->fftSetupBwd = aubio_vDSP_DFT_zrop_CreateSetup(s->fftSetupFwd,
240      s->fft_size, vDSP_DFT_INVERSE);
241
242#elif defined HAVE_INTEL_IPP  // using Intel IPP
243  const IppHintAlgorithm qualityHint = ippAlgHintAccurate; // OR ippAlgHintFast;
244  const int flags = IPP_FFT_NODIV_BY_ANY; // we're scaling manually afterwards
245  int order = aubio_power_of_two_order(winsize);
246  int sizeSpec, sizeInit, sizeBuffer;
247  IppStatus status;
248
249  if (winsize <= 4 || aubio_is_power_of_two(winsize) != 1)
250  {
251    AUBIO_ERR("intel IPP fft: can only create with sizes > 4 and power of two, requested %d,"
252      " try recompiling aubio with --enable-fftw3\n", winsize);
253    goto beach;
254  }
255
256  status = aubio_ippsFFTGetSize_R(order, flags, qualityHint,
257      &sizeSpec, &sizeInit, &sizeBuffer);
258  if (status != ippStsNoErr) {
259    AUBIO_ERR("fft: failed to initialize fft. IPP error: %d\n", status);
260    goto beach;
261  }
262  s->fft_size = s->winsize = winsize;
263  s->compspec = new_fvec(winsize);
264  s->in = AUBIO_ARRAY(smpl_t, s->winsize);
265  s->out = AUBIO_ARRAY(smpl_t, s->winsize);
266  s->memSpec = ippsMalloc_8u(sizeSpec);
267  s->memBuffer = ippsMalloc_8u(sizeBuffer);
268  if (sizeInit > 0 ) {
269    s->memInit = ippsMalloc_8u(sizeInit);
270  }
271  s->complexOut = aubio_ippsMalloc_complex(s->fft_size / 2 + 1);
272  status = aubio_ippsFFTInit_R(
273    &s->fftSpec, order, flags, qualityHint, s->memSpec, s->memInit);
274  if (status != ippStsNoErr) {
275    AUBIO_ERR("fft: failed to initialize. IPP error: %d\n", status);
276    goto beach;
277  }
278
279#else                         // using OOURA
280  if (aubio_is_power_of_two(winsize) != 1) {
281    AUBIO_ERR("fft: can only create with sizes power of two, requested %d,"
282        " try recompiling aubio with --enable-fftw3\n", winsize);
283    goto beach;
284  }
285  s->winsize = winsize;
286  s->fft_size = winsize / 2 + 1;
287  s->compspec = new_fvec(winsize);
288  s->in    = AUBIO_ARRAY(smpl_t, s->winsize);
289  s->out   = AUBIO_ARRAY(smpl_t, s->winsize);
290  s->ip    = AUBIO_ARRAY(int   , s->fft_size);
291  s->w     = AUBIO_ARRAY(smpl_t, s->fft_size);
292  s->ip[0] = 0;
293#endif /* using OOURA */
294
295  return s;
296
297beach:
298  AUBIO_FREE(s);
299  return NULL;
300}
301
302void del_aubio_fft(aubio_fft_t * s) {
303  /* destroy data */
304#ifdef HAVE_FFTW3             // using FFTW3
305  pthread_mutex_lock(&aubio_fftw_mutex);
306  fftw_destroy_plan(s->pfw);
307  fftw_destroy_plan(s->pbw);
308  fftw_free(s->specdata);
309  pthread_mutex_unlock(&aubio_fftw_mutex);
310
311#elif defined HAVE_ACCELERATE // using ACCELERATE
312  AUBIO_FREE(s->spec.realp);
313  AUBIO_FREE(s->spec.imagp);
314  aubio_vDSP_DFT_DestroySetup(s->fftSetupBwd);
315  aubio_vDSP_DFT_DestroySetup(s->fftSetupFwd);
316
317#elif defined HAVE_INTEL_IPP  // using Intel IPP
318  ippFree(s->memSpec);
319  ippFree(s->memInit);
320  ippFree(s->memBuffer);
321  ippFree(s->complexOut);
322
323#else                         // using OOURA
324  AUBIO_FREE(s->w);
325  AUBIO_FREE(s->ip);
326#endif
327
328  del_fvec(s->compspec);
329  AUBIO_FREE(s->in);
330  AUBIO_FREE(s->out);
331  AUBIO_FREE(s);
332}
333
334void aubio_fft_do(aubio_fft_t * s, const fvec_t * input, cvec_t * spectrum) {
335  aubio_fft_do_complex(s, input, s->compspec);
336  aubio_fft_get_spectrum(s->compspec, spectrum);
337}
338
339void aubio_fft_rdo(aubio_fft_t * s, const cvec_t * spectrum, fvec_t * output) {
340  aubio_fft_get_realimag(spectrum, s->compspec);
341  aubio_fft_rdo_complex(s, s->compspec, output);
342}
343
344void aubio_fft_do_complex(aubio_fft_t * s, const fvec_t * input, fvec_t * compspec) {
345  uint_t i;
346#ifndef HAVE_MEMCPY_HACKS
347  for (i=0; i < s->winsize; i++) {
348    s->in[i] = input->data[i];
349  }
350#else
351  memcpy(s->in, input->data, s->winsize * sizeof(smpl_t));
352#endif /* HAVE_MEMCPY_HACKS */
353
354#ifdef HAVE_FFTW3             // using FFTW3
355  fftw_execute(s->pfw);
356#ifdef HAVE_COMPLEX_H
357  compspec->data[0] = REAL(s->specdata[0]);
358  for (i = 1; i < s->fft_size -1 ; i++) {
359    compspec->data[i] = REAL(s->specdata[i]);
360    compspec->data[compspec->length - i] = IMAG(s->specdata[i]);
361  }
362  compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
363#else /* HAVE_COMPLEX_H  */
364  for (i = 0; i < s->fft_size; i++) {
365    compspec->data[i] = s->specdata[i];
366  }
367#endif /* HAVE_COMPLEX_H */
368
369#elif defined HAVE_ACCELERATE // using ACCELERATE
370  // convert real data to even/odd format used in vDSP
371  aubio_vDSP_ctoz((aubio_DSPComplex*)s->in, 2, &s->spec, 1, s->fft_size/2);
372  // compute the FFT
373  aubio_vDSP_DFT_Execute(s->fftSetupFwd, s->spec.realp, s->spec.imagp,
374      s->spec.realp, s->spec.imagp);
375  // convert from vDSP complex split to [ r0, r1, ..., rN, iN-1, .., i2, i1]
376  compspec->data[0] = s->spec.realp[0];
377  compspec->data[s->fft_size / 2] = s->spec.imagp[0];
378  for (i = 1; i < s->fft_size / 2; i++) {
379    compspec->data[i] = s->spec.realp[i];
380    compspec->data[s->fft_size - i] = s->spec.imagp[i];
381  }
382  // apply scaling
383  smpl_t scale = 1./2.;
384  aubio_vDSP_vsmul(compspec->data, 1, &scale, compspec->data, 1, s->fft_size);
385
386#elif defined HAVE_INTEL_IPP  // using Intel IPP
387
388  // apply fft
389  aubio_ippsFFTFwd_RToCCS(s->in, (aubio_IppFloat*)s->complexOut, s->fftSpec, s->memBuffer);
390  // convert complex buffer to [ r0, r1, ..., rN, iN-1, .., i2, i1]
391  compspec->data[0] = s->complexOut[0].re;
392  compspec->data[s->fft_size / 2] = s->complexOut[s->fft_size / 2].re;
393  for (i = 1; i < s->fft_size / 2; i++) {
394    compspec->data[i] = s->complexOut[i].re;
395    compspec->data[s->fft_size - i] = s->complexOut[i].im;
396  }
397
398#else                         // using OOURA
399  aubio_ooura_rdft(s->winsize, 1, s->in, s->ip, s->w);
400  compspec->data[0] = s->in[0];
401  compspec->data[s->winsize / 2] = s->in[1];
402  for (i = 1; i < s->fft_size - 1; i++) {
403    compspec->data[i] = s->in[2 * i];
404    compspec->data[s->winsize - i] = - s->in[2 * i + 1];
405  }
406#endif /* using OOURA */
407}
408
409void aubio_fft_rdo_complex(aubio_fft_t * s, const fvec_t * compspec, fvec_t * output) {
410  uint_t i;
411#ifdef HAVE_FFTW3
412  const smpl_t renorm = 1./(smpl_t)s->winsize;
413#ifdef HAVE_COMPLEX_H
414  s->specdata[0] = compspec->data[0];
415  for (i=1; i < s->fft_size - 1; i++) {
416    s->specdata[i] = compspec->data[i] +
417      I * compspec->data[compspec->length - i];
418  }
419  s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1];
420#else
421  for (i=0; i < s->fft_size; i++) {
422    s->specdata[i] = compspec->data[i];
423  }
424#endif
425  fftw_execute(s->pbw);
426  for (i = 0; i < output->length; i++) {
427    output->data[i] = s->out[i]*renorm;
428  }
429
430#elif defined HAVE_ACCELERATE // using ACCELERATE
431  // convert from real imag  [ r0, r1, ..., rN, iN-1, .., i2, i1]
432  // to vDSP packed format   [ r0, rN, r1, i1, ..., rN-1, iN-1 ]
433  s->out[0] = compspec->data[0];
434  s->out[1] = compspec->data[s->winsize / 2];
435  for (i = 1; i < s->fft_size / 2; i++) {
436    s->out[2 * i] = compspec->data[i];
437    s->out[2 * i + 1] = compspec->data[s->winsize - i];
438  }
439  // convert to split complex format used in vDSP
440  aubio_vDSP_ctoz((aubio_DSPComplex*)s->out, 2, &s->spec, 1, s->fft_size/2);
441  // compute the FFT
442  aubio_vDSP_DFT_Execute(s->fftSetupBwd, s->spec.realp, s->spec.imagp,
443      s->spec.realp, s->spec.imagp);
444  // convert result to real output
445  aubio_vDSP_ztoc(&s->spec, 1, (aubio_DSPComplex*)output->data, 2, s->fft_size/2);
446  // apply scaling
447  smpl_t scale = 1.0 / s->winsize;
448  aubio_vDSP_vsmul(output->data, 1, &scale, output->data, 1, s->fft_size);
449
450#elif defined HAVE_INTEL_IPP  // using Intel IPP
451
452  // convert from real imag  [ r0, 0, ..., rN, iN-1, .., i2, i1, iN-1] to complex format
453  s->complexOut[0].re = compspec->data[0];
454  s->complexOut[0].im = 0;
455  s->complexOut[s->fft_size / 2].re = compspec->data[s->fft_size / 2];
456  s->complexOut[s->fft_size / 2].im = 0.0;
457  for (i = 1; i < s->fft_size / 2; i++) {
458    s->complexOut[i].re = compspec->data[i];
459    s->complexOut[i].im = compspec->data[s->fft_size - i];
460  }
461  // apply fft
462  aubio_ippsFFTInv_CCSToR((const aubio_IppFloat *)s->complexOut, output->data, s->fftSpec, s->memBuffer);
463  // apply scaling
464  aubio_ippsMulC(output->data, 1.0 / s->winsize, output->data, s->fft_size);
465
466#else                         // using OOURA
467  smpl_t scale = 2.0 / s->winsize;
468  s->out[0] = compspec->data[0];
469  s->out[1] = compspec->data[s->winsize / 2];
470  for (i = 1; i < s->fft_size - 1; i++) {
471    s->out[2 * i] = compspec->data[i];
472    s->out[2 * i + 1] = - compspec->data[s->winsize - i];
473  }
474  aubio_ooura_rdft(s->winsize, -1, s->out, s->ip, s->w);
475  for (i=0; i < s->winsize; i++) {
476    output->data[i] = s->out[i] * scale;
477  }
478#endif
479}
480
481void aubio_fft_get_spectrum(const fvec_t * compspec, cvec_t * spectrum) {
482  aubio_fft_get_phas(compspec, spectrum);
483  aubio_fft_get_norm(compspec, spectrum);
484}
485
486void aubio_fft_get_realimag(const cvec_t * spectrum, fvec_t * compspec) {
487  aubio_fft_get_imag(spectrum, compspec);
488  aubio_fft_get_real(spectrum, compspec);
489}
490
491void aubio_fft_get_phas(const fvec_t * compspec, cvec_t * spectrum) {
492  uint_t i;
493  if (compspec->data[0] < 0) {
494    spectrum->phas[0] = PI;
495  } else {
496    spectrum->phas[0] = 0.;
497  }
498#if defined(HAVE_INTEL_IPP)
499  // convert from real imag  [ r0, r1, ..., rN, iN-1, ..., i2, i1, i0]
500  //                     to  [ r0, r1, ..., rN, i0, i1, i2, ..., iN-1]
501  for (i = 1; i < spectrum->length / 2; i++) {
502    ELEM_SWAP(compspec->data[compspec->length - i],
503        compspec->data[spectrum->length + i - 1]);
504  }
505  aubio_ippsAtan2(compspec->data + spectrum->length,
506      compspec->data + 1, spectrum->phas + 1, spectrum->length - 1);
507  // revert the imaginary part back again
508  for (i = 1; i < spectrum->length / 2; i++) {
509    ELEM_SWAP(compspec->data[spectrum->length + i - 1],
510        compspec->data[compspec->length - i]);
511  }
512#else
513  for (i=1; i < spectrum->length - 1; i++) {
514    spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i],
515        compspec->data[i]);
516  }
517#endif
518#ifdef HAVE_FFTW3
519  // for even length only, make sure last element is 0 or PI
520  if (2 * (compspec->length / 2) == compspec->length) {
521#endif
522    if (compspec->data[compspec->length/2] < 0) {
523      spectrum->phas[spectrum->length - 1] = PI;
524    } else {
525      spectrum->phas[spectrum->length - 1] = 0.;
526    }
527#ifdef HAVE_FFTW3
528  } else {
529    i = spectrum->length - 1;
530    spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i],
531        compspec->data[i]);
532  }
533#endif
534}
535
536void aubio_fft_get_norm(const fvec_t * compspec, cvec_t * spectrum) {
537  uint_t i = 0;
538  spectrum->norm[0] = ABS(compspec->data[0]);
539  for (i=1; i < spectrum->length - 1; i++) {
540    spectrum->norm[i] = SQRT(SQR(compspec->data[i])
541        + SQR(compspec->data[compspec->length - i]) );
542  }
543#ifdef HAVE_FFTW3
544  // for even length, make sure last element is > 0
545  if (2 * (compspec->length / 2) == compspec->length) {
546#endif
547    spectrum->norm[spectrum->length-1] =
548      ABS(compspec->data[compspec->length/2]);
549#ifdef HAVE_FFTW3
550  } else {
551    i = spectrum->length - 1;
552    spectrum->norm[i] = SQRT(SQR(compspec->data[i])
553        + SQR(compspec->data[compspec->length - i]) );
554  }
555#endif
556}
557
558void aubio_fft_get_imag(const cvec_t * spectrum, fvec_t * compspec) {
559  uint_t i;
560  for (i = 1; i < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; i++) {
561    compspec->data[compspec->length - i] =
562      spectrum->norm[i]*SIN(spectrum->phas[i]);
563  }
564}
565
566void aubio_fft_get_real(const cvec_t * spectrum, fvec_t * compspec) {
567  uint_t i;
568  for (i = 0; i < compspec->length / 2 + 1; i++) {
569    compspec->data[i] =
570      spectrum->norm[i]*COS(spectrum->phas[i]);
571  }
572}
Note: See TracBrowser for help on using the repository browser.