Changeset 54dd945 for src/spectral


Ignore:
Timestamp:
Apr 11, 2013, 2:31:40 AM (12 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
ac67de7
Parents:
e8bc8e9
Message:

src/spectral/fft.c: add vDSP Accelerate

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/spectral/fft.c

    re8bc8e9 r54dd945  
    2525#include "spectral/fft.h"
    2626
    27 #ifdef HAVE_FFTW3
     27#ifdef HAVE_FFTW3             // using FFTW3
    2828/* note that <complex.h> is not included here but only in aubio_priv.h, so that
    2929 * c++ projects can still use their own complex definition. */
     
    7878pthread_mutex_t aubio_fftw_mutex = PTHREAD_MUTEX_INITIALIZER;
    7979
     80#else
     81#ifdef HAVE_ACCELERATE        // using ACCELERATE
     82// https://developer.apple.com/library/mac/#documentation/Accelerate/Reference/vDSPRef/Reference/reference.html
     83#include <Accelerate/Accelerate.h>
     84
     85#else                         // using OOURA
     86// let's use ooura instead
     87extern void rdft(int, int, double *, int *, double *);
     88
     89#endif /* HAVE_ACCELERATE */
    8090#endif /* HAVE_FFTW3 */
    8191
     
    8393  uint_t winsize;
    8494  uint_t fft_size;
    85 #ifdef HAVE_FFTW3
     95#ifdef HAVE_FFTW3             // using FFTW3
    8696  real_t *in, *out;
    8797  fftw_plan pfw, pbw;
    8898  fft_data_t * specdata;     /* complex spectral data */
    8999#else
     100#ifdef HAVE_ACCELERATE        // using ACCELERATE
     101  int log2fftsize;
     102  FFTSetup fftSetup;
     103  DSPSplitComplex spec;
     104  float *in, *out;
     105#else                         // using OOURA
    90106  double *in, *out;
    91107  double *w;
    92108  int *ip;
     109#endif /* HAVE_ACCELERATE */
    93110#endif /* HAVE_FFTW3 */
    94111  fvec_t * compspec;
    95112};
    96 
    97 #ifndef HAVE_FFTW3
    98 // let's use ooura instead
    99 extern void rdft(int, int, double *, int *, double *);
    100 #endif
    101113
    102114aubio_fft_t * new_aubio_fft (uint_t winsize) {
     
    131143  }
    132144#else
     145#ifdef HAVE_ACCELERATE        // using ACCELERATE
     146  s->winsize = winsize;
     147  s->fft_size = winsize;
     148  s->compspec = new_fvec(winsize);
     149  s->log2fftsize = (uint_t)log2f(s->fft_size);
     150  s->in = AUBIO_ARRAY(float, s->fft_size);
     151  s->out = AUBIO_ARRAY(float, s->fft_size);
     152  s->spec.realp = AUBIO_ARRAY(float, s->fft_size/2);
     153  s->spec.imagp = AUBIO_ARRAY(float, s->fft_size/2);
     154  s->fftSetup = vDSP_create_fftsetup(s->log2fftsize, FFT_RADIX2);
     155#else                         // using OOURA
    133156  s->winsize = winsize;
    134157  s->fft_size = winsize / 2 + 1;
     
    139162  s->w     = AUBIO_ARRAY(double, s->fft_size);
    140163  s->ip[0] = 0;
    141 #endif
     164#endif /* HAVE_ACCELERATE */
     165#endif /* HAVE_FFTW3 */
    142166  return s;
    143167}
     
    146170  /* destroy data */
    147171  del_fvec(s->compspec);
    148 #ifdef HAVE_FFTW3
     172#ifdef HAVE_FFTW3             // using FFTW3
    149173  fftw_destroy_plan(s->pfw);
    150174  fftw_destroy_plan(s->pbw);
    151175  fftw_free(s->specdata);
    152176#else /* HAVE_FFTW3 */
     177#ifdef HAVE_ACCELERATE        // using ACCELERATE
     178  AUBIO_FREE(s->spec.realp);
     179  AUBIO_FREE(s->spec.imagp);
     180#else                         // using OOURA
    153181  AUBIO_FREE(s->w);
    154182  AUBIO_FREE(s->ip);
     183#endif /* HAVE_ACCELERATE */
    155184#endif /* HAVE_FFTW3 */
    156185  AUBIO_FREE(s->out);
     
    174203    s->in[i] = input->data[i];
    175204  }
    176 #ifdef HAVE_FFTW3
     205#ifdef HAVE_FFTW3             // using FFTW3
    177206  fftw_execute(s->pfw);
    178207#ifdef HAVE_COMPLEX_H
     
    189218#endif /* HAVE_COMPLEX_H */
    190219#else /* HAVE_FFTW3 */
     220#ifdef HAVE_ACCELERATE        // using ACCELERATE
     221  // convert real data to even/odd format used in vDSP
     222  vDSP_ctoz((COMPLEX*)s->in, 2, &s->spec, 1, s->fft_size/2);
     223  // compute the FFT
     224  vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_FORWARD);
     225  // convert from vDSP complex split to [ r0, r1, ..., rN, iN-1, .., i2, i1]
     226  compspec->data[0] = s->spec.realp[0];
     227  compspec->data[s->fft_size / 2] = s->spec.imagp[0];
     228  for (i = 1; i < s->fft_size / 2; i++) {
     229    compspec->data[i] = s->spec.realp[i];
     230    compspec->data[s->fft_size - i] = s->spec.imagp[i];
     231  }
     232  // apply scaling
     233  smpl_t scale = 1./2.;
     234  vDSP_vsmul(compspec->data, 1, &scale, compspec->data, 1, s->fft_size);
     235#else                         // using OOURA
    191236  rdft(s->winsize, 1, s->in, s->ip, s->w);
    192237  compspec->data[0] = s->in[0];
     
    196241    compspec->data[s->winsize - i] = - s->in[2 * i + 1];
    197242  }
     243#endif /* HAVE_ACCELERATE */
    198244#endif /* HAVE_FFTW3 */
    199245}
     
    220266  }
    221267#else /* HAVE_FFTW3 */
     268#ifdef HAVE_ACCELERATE        // using ACCELERATE
     269  // convert from real imag  [ r0, r1, ..., rN, iN-1, .., i2, i1]
     270  // to vDSP packed format   [ r0, rN, r1, i1, ..., rN-1, iN-1 ]
     271  s->out[0] = compspec->data[0];
     272  s->out[1] = compspec->data[s->winsize / 2];
     273  for (i = 1; i < s->fft_size / 2; i++) {
     274    s->out[2 * i] = compspec->data[i];
     275    s->out[2 * i + 1] = compspec->data[s->winsize - i];
     276  }
     277  // convert to split complex format used in vDSP
     278  vDSP_ctoz((COMPLEX*)s->out, 2, &s->spec, 1, s->fft_size/2);
     279  // compute the FFT
     280  vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_INVERSE);
     281  // convert result to real output
     282  vDSP_ztoc(&s->spec, 1, (COMPLEX*)output->data, 2, s->fft_size/2);
     283  // apply scaling
     284  smpl_t scale = 1.0 / s->winsize;
     285  vDSP_vsmul(output->data, 1, &scale, output->data, 1, s->fft_size);
     286#else                         // using OOURA
    222287  smpl_t scale = 2.0 / s->winsize;
    223288  s->out[0] = compspec->data[0];
     
    231296    output->data[i] = s->out[i] * scale;
    232297  }
     298#endif /* HAVE_ACCELERATE */
    233299#endif /* HAVE_FFTW3 */
    234300}
Note: See TracChangeset for help on using the changeset viewer.