source: src/spectral/fft.c @ 0ad2e17

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 0ad2e17 was 0ad2e17, checked in by Paul Brossier <piem@piem.org>, 7 years ago

src/spectral/fft.c: define ipp aliases for single/double precision

  • Property mode set to 100644
File size: 18.0 KB
RevLine 
[96fb8ad]1/*
[e6a78ea]2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
[96fb8ad]3
[e6a78ea]4  This file is part of aubio.
[96fb8ad]5
[e6a78ea]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.
[96fb8ad]10
[e6a78ea]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/>.
[96fb8ad]18
19*/
20
21#include "aubio_priv.h"
[aadd27a]22#include "fvec.h"
23#include "cvec.h"
[96fb8ad]24#include "mathutils.h"
[32d6958]25#include "spectral/fft.h"
[96fb8ad]26
[54dd945]27#ifdef HAVE_FFTW3             // using FFTW3
[f3bee79]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>
[d453a4a]31#include <pthread.h>
[f3bee79]32
33#ifdef HAVE_COMPLEX_H
[729a3c0]34#ifdef HAVE_FFTW3F
[f3bee79]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
[729a3c0]42#ifdef HAVE_FFTW3F
[f3bee79]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
[729a3c0]54#ifdef HAVE_FFTW3F
[a47cd35]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
[96fb8ad]63#endif
64
[729a3c0]65#ifdef HAVE_FFTW3F
[c204928]66#if HAVE_AUBIO_DOUBLE
[eeb7276]67#error "Using aubio in double precision with fftw3 in single precision"
[fd6b90f]68#endif /* HAVE_AUBIO_DOUBLE */
[152ed05]69#define real_t float
[eeb7276]70#elif defined (HAVE_FFTW3) /* HAVE_FFTW3F */
[c204928]71#if !HAVE_AUBIO_DOUBLE
[eeb7276]72#error "Using aubio in single precision with fftw3 in double precision"
[fd6b90f]73#endif /* HAVE_AUBIO_DOUBLE */
[152ed05]74#define real_t double
[fd6b90f]75#endif /* HAVE_FFTW3F */
[96fb8ad]76
[d453a4a]77// a global mutex for FFTW thread safety
78pthread_mutex_t aubio_fftw_mutex = PTHREAD_MUTEX_INITIALIZER;
79
[986131d]80#elif defined HAVE_ACCELERATE        // using ACCELERATE
[54dd945]81// https://developer.apple.com/library/mac/#documentation/Accelerate/Reference/vDSPRef/Reference/reference.html
82#include <Accelerate/Accelerate.h>
83
[39c4721]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
98#else
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 */
113
[986131d]114#elif defined HAVE_INTEL_IPP // using INTEL IPP
115
[0ad2e17]116#if !HAVE_AUBIO_DOUBLE
117#define aubio_IppFloat                 Ipp32f
118#define aubio_IppComplex               Ipp32fc
119#define aubio_FFTSpec                  FFTSpec_R_32f
120#define aubio_ippsPhas                 ippsPhase_32fc
121#define aubio_ippsMalloc_complex       ippsMalloc_32fc
122#define aubio_ippsFFTInit_R            ippsFFTInit_R_32f
123#define aubio_ippsFFTGetSize_R         ippsFFTGetSize_R_32f
124#define aubio_ippsFFTInv_CCSToR        ippsFFTInv_CCSToR_32f
125#define aubio_ippsFFTFwd_RToCCS        ippsFFTFwd_RToCCS_32f
126#else /* HAVE_AUBIO_DOUBLE */
127#define aubio_IppFloat                 Ipp64f
128#define aubio_IppComplex               Ipp64fc
129#define aubio_FFTSpec                  FFTSpec_R_64f
130#define aubio_ippsPhas                 ippsPhase_64fc
131#define aubio_ippsMalloc_complex       ippsMalloc_64fc
132#define aubio_ippsFFTInit_R            ippsFFTInit_R_64f
133#define aubio_ippsFFTGetSize_R         ippsFFTGetSize_R_64f
134#define aubio_ippsFFTInv_CCSToR        ippsFFTInv_CCSToR_64f
135#define aubio_ippsFFTFwd_RToCCS        ippsFFTFwd_RToCCS_64f
136#endif
137
[986131d]138
139#else // using OOURA
[54dd945]140// let's use ooura instead
[f50c9503]141extern void aubio_ooura_rdft(int, int, smpl_t *, int *, smpl_t *);
[54dd945]142
[986131d]143#endif
[729a3c0]144
[96fb8ad]145struct _aubio_fft_t {
[aadd27a]146  uint_t winsize;
147  uint_t fft_size;
[986131d]148
[54dd945]149#ifdef HAVE_FFTW3             // using FFTW3
[aadd27a]150  real_t *in, *out;
[4b6937b]151  fftw_plan pfw, pbw;
[986131d]152  fft_data_t * specdata; /* complex spectral data */
153
154#elif defined HAVE_ACCELERATE  // using ACCELERATE
[54dd945]155  int log2fftsize;
[39c4721]156  aubio_FFTSetup fftSetup;
157  aubio_DSPSplitComplex spec;
158  smpl_t *in, *out;
[986131d]159 
160#elif defined HAVE_INTEL_IPP  // using Intel IPP
161  // mark FFT impl as Intel IPP
162  #define INTEL_IPP_FFT 1
163  smpl_t *in, *out;
164  Ipp8u* memSpec;
165  Ipp8u* memInit;
166  Ipp8u* memBuffer;
[0ad2e17]167  struct aubio_FFTSpec* fftSpec;
168  aubio_IppComplex* complexOut;
[54dd945]169#else                         // using OOURA
[5c6b264]170  smpl_t *in, *out;
171  smpl_t *w;
[729a3c0]172  int *ip;
[986131d]173#endif /* using OOURA */
174
[aadd27a]175  fvec_t * compspec;
[96fb8ad]176};
177
[729a3c0]178aubio_fft_t * new_aubio_fft (uint_t winsize) {
[a47cd35]179  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
[d897aae]180  if ((sint_t)winsize < 2) {
181    AUBIO_ERR("fft: got winsize %d, but can not be < 2\n", winsize);
[a984461]182    goto beach;
183  }
[986131d]184
[729a3c0]185#ifdef HAVE_FFTW3
[8b3a7e7]186  uint_t i;
[aadd27a]187  s->winsize  = winsize;
[a47cd35]188  /* allocate memory */
[aadd27a]189  s->in       = AUBIO_ARRAY(real_t,winsize);
190  s->out      = AUBIO_ARRAY(real_t,winsize);
[d95ff38]191  s->compspec = new_fvec(winsize);
[a47cd35]192  /* create plans */
[d453a4a]193  pthread_mutex_lock(&aubio_fftw_mutex);
[237f632]194#ifdef HAVE_COMPLEX_H
[4b6937b]195  s->fft_size = winsize/2 + 1;
[a47cd35]196  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]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);
[237f632]199#else
[aadd27a]200  s->fft_size = winsize;
[a47cd35]201  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]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);
[237f632]204#endif
[d453a4a]205  pthread_mutex_unlock(&aubio_fftw_mutex);
[4f4299d]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  }
[986131d]213
214#elif defined HAVE_ACCELERATE  // using ACCELERATE
[54dd945]215  s->winsize = winsize;
216  s->fft_size = winsize;
217  s->compspec = new_fvec(winsize);
[986131d]218  s->log2fftsize = aubio_power_of_two_order(s->fft_size);
[39c4721]219  s->in = AUBIO_ARRAY(smpl_t, s->fft_size);
220  s->out = AUBIO_ARRAY(smpl_t, s->fft_size);
221  s->spec.realp = AUBIO_ARRAY(smpl_t, s->fft_size/2);
222  s->spec.imagp = AUBIO_ARRAY(smpl_t, s->fft_size/2);
223  s->fftSetup = aubio_vDSP_create_fftsetup(s->log2fftsize, FFT_RADIX2);
[986131d]224
225#elif defined HAVE_INTEL_IPP  // using Intel IPP
226  const IppHintAlgorithm qualityHint = ippAlgHintAccurate; // OR ippAlgHintFast;
227  const int flags = IPP_FFT_NODIV_BY_ANY; // we're scaling manually afterwards
228  int order = aubio_power_of_two_order(winsize);
229  int sizeSpec, sizeInit, sizeBuffer;
230  IppStatus status;
231
232  if (winsize <= 4 || aubio_is_power_of_two(winsize) != 1)
233  {
234    AUBIO_ERR("intel IPP fft: can only create with sizes > 4 and power of two, requested %d,"
235      " try recompiling aubio with --enable-fftw3\n", winsize);
236    goto beach;
237  }
238
[0ad2e17]239  status = aubio_ippsFFTGetSize_R(order, flags, qualityHint,
[986131d]240      &sizeSpec, &sizeInit, &sizeBuffer);
241  if (status != ippStsNoErr) {
242    AUBIO_ERR("fft: failed to initialize fft. IPP error: %d\n", status);
243    goto beach;
244  }
245  s->fft_size = s->winsize = winsize;
246  s->compspec = new_fvec(winsize);
247  s->in = AUBIO_ARRAY(smpl_t, s->winsize);
248  s->out = AUBIO_ARRAY(smpl_t, s->winsize);
249  s->memSpec = ippsMalloc_8u(sizeSpec);
250  s->memBuffer = ippsMalloc_8u(sizeBuffer);
251  if (sizeInit > 0 ) {
252    s->memInit = ippsMalloc_8u(sizeInit);
253  }
[0ad2e17]254  s->complexOut = aubio_ippsMalloc_complex(s->fft_size / 2 + 1);
255  status = aubio_ippsFFTInit_R(
[986131d]256    &s->fftSpec, order, flags, qualityHint, s->memSpec, s->memInit);
257  if (status != ippStsNoErr) {
258    AUBIO_ERR("fft: failed to initialize. IPP error: %d\n", status);
259    goto beach;
260  }
261
[54dd945]262#else                         // using OOURA
[3c6f584]263  if (aubio_is_power_of_two(winsize) != 1) {
[1b57274]264    AUBIO_ERR("fft: can only create with sizes power of two, requested %d,"
265        " try recompiling aubio with --enable-fftw3\n", winsize);
[a984461]266    goto beach;
[3c6f584]267  }
[729a3c0]268  s->winsize = winsize;
269  s->fft_size = winsize / 2 + 1;
270  s->compspec = new_fvec(winsize);
[5c6b264]271  s->in    = AUBIO_ARRAY(smpl_t, s->winsize);
272  s->out   = AUBIO_ARRAY(smpl_t, s->winsize);
[729a3c0]273  s->ip    = AUBIO_ARRAY(int   , s->fft_size);
[5c6b264]274  s->w     = AUBIO_ARRAY(smpl_t, s->fft_size);
[729a3c0]275  s->ip[0] = 0;
[986131d]276#endif /* using OOURA */
277
[a47cd35]278  return s;
[986131d]279
[a984461]280beach:
281  AUBIO_FREE(s);
282  return NULL;
[96fb8ad]283}
284
285void del_aubio_fft(aubio_fft_t * s) {
[a47cd35]286  /* destroy data */
[54dd945]287#ifdef HAVE_FFTW3             // using FFTW3
[4b251ae]288  pthread_mutex_lock(&aubio_fftw_mutex);
[a47cd35]289  fftw_destroy_plan(s->pfw);
290  fftw_destroy_plan(s->pbw);
291  fftw_free(s->specdata);
[4b251ae]292  pthread_mutex_unlock(&aubio_fftw_mutex);
[986131d]293
294#elif defined HAVE_ACCELERATE // using ACCELERATE
[54dd945]295  AUBIO_FREE(s->spec.realp);
296  AUBIO_FREE(s->spec.imagp);
[39c4721]297  aubio_vDSP_destroy_fftsetup(s->fftSetup);
[986131d]298
299#elif defined HAVE_INTEL_IPP  // using Intel IPP
300  ippFree(s->memSpec);
301  ippFree(s->memInit);
302  ippFree(s->memBuffer);
303  ippFree(s->complexOut);
304
[54dd945]305#else                         // using OOURA
[729a3c0]306  AUBIO_FREE(s->w);
307  AUBIO_FREE(s->ip);
[986131d]308#endif
309
310  del_fvec(s->compspec);
[729a3c0]311  AUBIO_FREE(s->in);
[986131d]312  AUBIO_FREE(s->out);
[a47cd35]313  AUBIO_FREE(s);
[96fb8ad]314}
315
[feb694b]316void aubio_fft_do(aubio_fft_t * s, const fvec_t * input, cvec_t * spectrum) {
[aadd27a]317  aubio_fft_do_complex(s, input, s->compspec);
[986131d]318  aubio_fft_get_spectrum(s, s->compspec, spectrum);
[96fb8ad]319}
320
[feb694b]321void aubio_fft_rdo(aubio_fft_t * s, const cvec_t * spectrum, fvec_t * output) {
[986131d]322  aubio_fft_get_realimag(s, spectrum, s->compspec);
[aadd27a]323  aubio_fft_rdo_complex(s, s->compspec, output);
[96fb8ad]324}
325
[feb694b]326void aubio_fft_do_complex(aubio_fft_t * s, const fvec_t * input, fvec_t * compspec) {
[729a3c0]327  uint_t i;
[b5d32cb]328#ifndef HAVE_MEMCPY_HACKS
[729a3c0]329  for (i=0; i < s->winsize; i++) {
330    s->in[i] = input->data[i];
[d95ff38]331  }
[b5d32cb]332#else
333  memcpy(s->in, input->data, s->winsize * sizeof(smpl_t));
334#endif /* HAVE_MEMCPY_HACKS */
[986131d]335
[54dd945]336#ifdef HAVE_FFTW3             // using FFTW3
[d95ff38]337  fftw_execute(s->pfw);
[4b6937b]338#ifdef HAVE_COMPLEX_H
[d95ff38]339  compspec->data[0] = REAL(s->specdata[0]);
[729a3c0]340  for (i = 1; i < s->fft_size -1 ; i++) {
341    compspec->data[i] = REAL(s->specdata[i]);
342    compspec->data[compspec->length - i] = IMAG(s->specdata[i]);
[d95ff38]343  }
344  compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
[729a3c0]345#else /* HAVE_COMPLEX_H  */
346  for (i = 0; i < s->fft_size; i++) {
347    compspec->data[i] = s->specdata[i];
[237f632]348  }
[729a3c0]349#endif /* HAVE_COMPLEX_H */
[986131d]350
351#elif defined HAVE_ACCELERATE // using ACCELERATE
[54dd945]352  // convert real data to even/odd format used in vDSP
[39c4721]353  aubio_vDSP_ctoz((aubio_DSPComplex*)s->in, 2, &s->spec, 1, s->fft_size/2);
[54dd945]354  // compute the FFT
[39c4721]355  aubio_vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_FORWARD);
[54dd945]356  // convert from vDSP complex split to [ r0, r1, ..., rN, iN-1, .., i2, i1]
357  compspec->data[0] = s->spec.realp[0];
358  compspec->data[s->fft_size / 2] = s->spec.imagp[0];
359  for (i = 1; i < s->fft_size / 2; i++) {
360    compspec->data[i] = s->spec.realp[i];
361    compspec->data[s->fft_size - i] = s->spec.imagp[i];
362  }
363  // apply scaling
364  smpl_t scale = 1./2.;
[39c4721]365  aubio_vDSP_vsmul(compspec->data, 1, &scale, compspec->data, 1, s->fft_size);
[986131d]366
367#elif defined HAVE_INTEL_IPP  // using Intel IPP
368
369  // apply fft
[0ad2e17]370  aubio_ippsFFTFwd_RToCCS(s->in, (aubio_IppFloat*)s->complexOut, s->fftSpec, s->memBuffer);
[986131d]371  // convert complex buffer to [ r0, r1, ..., rN, iN-1, .., i2, i1]
372  compspec->data[0] = s->complexOut[0].re;
373  compspec->data[s->fft_size / 2] = s->complexOut[s->fft_size / 2].re;
374  for (i = 1; i < s->fft_size / 2; i++) {
375    compspec->data[i] = s->complexOut[i].re;
376    compspec->data[s->fft_size - i] = s->complexOut[i].im;
377  }
378  // apply scaling
[0ad2e17]379  aubio_ippsMulC(compspec->data, 1.0 / 2.0, compspec->data, s->fft_size);
[986131d]380
[54dd945]381#else                         // using OOURA
[f50c9503]382  aubio_ooura_rdft(s->winsize, 1, s->in, s->ip, s->w);
[729a3c0]383  compspec->data[0] = s->in[0];
384  compspec->data[s->winsize / 2] = s->in[1];
385  for (i = 1; i < s->fft_size - 1; i++) {
386    compspec->data[i] = s->in[2 * i];
387    compspec->data[s->winsize - i] = - s->in[2 * i + 1];
388  }
[986131d]389#endif /* using OOURA */
[96fb8ad]390}
391
[feb694b]392void aubio_fft_rdo_complex(aubio_fft_t * s, const fvec_t * compspec, fvec_t * output) {
[729a3c0]393  uint_t i;
394#ifdef HAVE_FFTW3
[aadd27a]395  const smpl_t renorm = 1./(smpl_t)s->winsize;
[4b6937b]396#ifdef HAVE_COMPLEX_H
[d95ff38]397  s->specdata[0] = compspec->data[0];
[729a3c0]398  for (i=1; i < s->fft_size - 1; i++) {
399    s->specdata[i] = compspec->data[i] +
400      I * compspec->data[compspec->length - i];
[d95ff38]401  }
402  s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1];
[237f632]403#else
[729a3c0]404  for (i=0; i < s->fft_size; i++) {
405    s->specdata[i] = compspec->data[i];
[d95ff38]406  }
[aadd27a]407#endif
[d95ff38]408  fftw_execute(s->pbw);
[729a3c0]409  for (i = 0; i < output->length; i++) {
410    output->data[i] = s->out[i]*renorm;
411  }
[986131d]412
413#elif defined HAVE_ACCELERATE // using ACCELERATE
[54dd945]414  // convert from real imag  [ r0, r1, ..., rN, iN-1, .., i2, i1]
415  // to vDSP packed format   [ r0, rN, r1, i1, ..., rN-1, iN-1 ]
416  s->out[0] = compspec->data[0];
417  s->out[1] = compspec->data[s->winsize / 2];
418  for (i = 1; i < s->fft_size / 2; i++) {
419    s->out[2 * i] = compspec->data[i];
420    s->out[2 * i + 1] = compspec->data[s->winsize - i];
421  }
422  // convert to split complex format used in vDSP
[39c4721]423  aubio_vDSP_ctoz((aubio_DSPComplex*)s->out, 2, &s->spec, 1, s->fft_size/2);
[54dd945]424  // compute the FFT
[39c4721]425  aubio_vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_INVERSE);
[54dd945]426  // convert result to real output
[39c4721]427  aubio_vDSP_ztoc(&s->spec, 1, (aubio_DSPComplex*)output->data, 2, s->fft_size/2);
[54dd945]428  // apply scaling
429  smpl_t scale = 1.0 / s->winsize;
[39c4721]430  aubio_vDSP_vsmul(output->data, 1, &scale, output->data, 1, s->fft_size);
[986131d]431
432#elif defined HAVE_INTEL_IPP  // using Intel IPP
433
434  // convert from real imag  [ r0, 0, ..., rN, iN-1, .., i2, i1, iN-1] to complex format
435  s->complexOut[0].re = compspec->data[0];
436  s->complexOut[0].im = 0;
437  s->complexOut[s->fft_size / 2].re = compspec->data[s->fft_size / 2];
438  s->complexOut[s->fft_size / 2].im = 0.0;
439  for (i = 1; i < s->fft_size / 2; i++) {
440    s->complexOut[i].re = compspec->data[i];
441    s->complexOut[i].im = compspec->data[s->fft_size - i];
442  }
443  // apply fft
[0ad2e17]444  aubio_ippsFFTInv_CCSToR((const aubio_IppFloat *)s->complexOut, output->data, s->fftSpec, s->memBuffer);
[986131d]445  // apply scaling
[0ad2e17]446  aubio_ippsMulC(output->data, 1.0 / s->winsize, output->data, s->fft_size);
[986131d]447
[54dd945]448#else                         // using OOURA
[7100895]449  smpl_t scale = 2.0 / s->winsize;
[729a3c0]450  s->out[0] = compspec->data[0];
451  s->out[1] = compspec->data[s->winsize / 2];
452  for (i = 1; i < s->fft_size - 1; i++) {
453    s->out[2 * i] = compspec->data[i];
454    s->out[2 * i + 1] = - compspec->data[s->winsize - i];
[aadd27a]455  }
[f50c9503]456  aubio_ooura_rdft(s->winsize, -1, s->out, s->ip, s->w);
[729a3c0]457  for (i=0; i < s->winsize; i++) {
458    output->data[i] = s->out[i] * scale;
459  }
[986131d]460#endif
[237f632]461}
462
[986131d]463void aubio_fft_get_spectrum(aubio_fft_t *s, const fvec_t * compspec, cvec_t * spectrum) {
464  aubio_fft_get_phas(s, compspec, spectrum);
465  aubio_fft_get_norm(s, compspec, spectrum);
[237f632]466}
467
[986131d]468void aubio_fft_get_realimag(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) {
469  aubio_fft_get_imag(s, spectrum, compspec);
470  aubio_fft_get_real(s, spectrum, compspec);
[237f632]471}
472
[986131d]473void aubio_fft_get_phas(aubio_fft_t *s, const fvec_t * compspec, cvec_t * spectrum) {
474
475#ifdef INTEL_IPP_FFT // using Intel IPP FFT
476  uint_t i;
[95f5c83]477
[986131d]478  // convert from real imag  [ r0, 0, ..., rN, iN-1, .., i2, i1, iN-1] to complex format
479  s->complexOut[0].re = compspec->data[0];
480  s->complexOut[0].im = 0;
481  s->complexOut[s->fft_size / 2].re = compspec->data[s->fft_size / 2];
482  s->complexOut[s->fft_size / 2].im = 0.0;
483  for (i = 1; i < spectrum->length - 1; i++) {
484    s->complexOut[i].re = compspec->data[i];
485    s->complexOut[i].im = compspec->data[compspec->length - i];
486  }
[0ad2e17]487
488  IppStatus status = aubio_ippsPhas(s->complexOut, spectrum->phas, spectrum->length);
[986131d]489  if (status != ippStsNoErr) {
490    AUBIO_ERR("fft: failed to extract phase from fft. IPP error: %d\n", status);
491  }
492
493#else                 // NOT using Intel IPP
[729a3c0]494  uint_t i;
[d95ff38]495  if (compspec->data[0] < 0) {
496    spectrum->phas[0] = PI;
497  } else {
498    spectrum->phas[0] = 0.;
499  }
[729a3c0]500  for (i=1; i < spectrum->length - 1; i++) {
501    spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i],
502        compspec->data[i]);
[d95ff38]503  }
504  if (compspec->data[compspec->length/2] < 0) {
505    spectrum->phas[spectrum->length - 1] = PI;
506  } else {
507    spectrum->phas[spectrum->length - 1] = 0.;
[aadd27a]508  }
[986131d]509#endif
[f88a326]510}
511
[986131d]512void aubio_fft_get_norm(aubio_fft_t *s, const fvec_t * compspec, cvec_t * spectrum) {
[729a3c0]513  uint_t i = 0;
[d95ff38]514  spectrum->norm[0] = ABS(compspec->data[0]);
[729a3c0]515  for (i=1; i < spectrum->length - 1; i++) {
516    spectrum->norm[i] = SQRT(SQR(compspec->data[i])
517        + SQR(compspec->data[compspec->length - i]) );
[a47cd35]518  }
[729a3c0]519  spectrum->norm[spectrum->length-1] =
[d95ff38]520    ABS(compspec->data[compspec->length/2]);
[f88a326]521}
522
[986131d]523void aubio_fft_get_imag(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) {
[729a3c0]524  uint_t i;
525  for (i = 1; i < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; i++) {
526    compspec->data[compspec->length - i] =
527      spectrum->norm[i]*SIN(spectrum->phas[i]);
[a47cd35]528  }
[f88a326]529}
530
[986131d]531void aubio_fft_get_real(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) {
[729a3c0]532  uint_t i;
533  for (i = 0; i < compspec->length / 2 + 1; i++) {
[152ed05]534    compspec->data[i] =
[729a3c0]535      spectrum->norm[i]*COS(spectrum->phas[i]);
[aadd27a]536  }
[f88a326]537}
Note: See TracBrowser for help on using the repository browser.