source: src/spectral/fft.c @ f69e3bd

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since f69e3bd was 54dd945, checked in by Paul Brossier <piem@piem.org>, 12 years ago

src/spectral/fft.c: add vDSP Accelerate

  • Property mode set to 100644
File size: 11.2 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
[4369cb9]67#warning "Using aubio in double precision with fftw3 in single precision"
[fd6b90f]68#endif /* HAVE_AUBIO_DOUBLE */
[4369cb9]69#define real_t float
[fd6b90f]70#else /* HAVE_FFTW3F */
[c204928]71#if !HAVE_AUBIO_DOUBLE
[fd6b90f]72#warning "Using aubio in single precision with fftw3 in double precision"
73#endif /* HAVE_AUBIO_DOUBLE */
[4369cb9]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
[54dd945]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 */
[729a3c0]90#endif /* HAVE_FFTW3 */
91
[96fb8ad]92struct _aubio_fft_t {
[aadd27a]93  uint_t winsize;
94  uint_t fft_size;
[54dd945]95#ifdef HAVE_FFTW3             // using FFTW3
[aadd27a]96  real_t *in, *out;
[4b6937b]97  fftw_plan pfw, pbw;
[aadd27a]98  fft_data_t * specdata;     /* complex spectral data */
[729a3c0]99#else
[54dd945]100#ifdef HAVE_ACCELERATE        // using ACCELERATE
101  int log2fftsize;
102  FFTSetup fftSetup;
103  DSPSplitComplex spec;
104  float *in, *out;
105#else                         // using OOURA
[729a3c0]106  double *in, *out;
107  double *w;
108  int *ip;
[54dd945]109#endif /* HAVE_ACCELERATE */
[729a3c0]110#endif /* HAVE_FFTW3 */
[aadd27a]111  fvec_t * compspec;
[96fb8ad]112};
113
[729a3c0]114aubio_fft_t * new_aubio_fft (uint_t winsize) {
[a47cd35]115  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
[729a3c0]116#ifdef HAVE_FFTW3
[8b3a7e7]117  uint_t i;
[aadd27a]118  s->winsize  = winsize;
[a47cd35]119  /* allocate memory */
[aadd27a]120  s->in       = AUBIO_ARRAY(real_t,winsize);
121  s->out      = AUBIO_ARRAY(real_t,winsize);
[d95ff38]122  s->compspec = new_fvec(winsize);
[a47cd35]123  /* create plans */
[d453a4a]124  pthread_mutex_lock(&aubio_fftw_mutex);
[237f632]125#ifdef HAVE_COMPLEX_H
[4b6937b]126  s->fft_size = winsize/2 + 1;
[a47cd35]127  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]128  s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in,  s->specdata, FFTW_ESTIMATE);
129  s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE);
[237f632]130#else
[aadd27a]131  s->fft_size = winsize;
[a47cd35]132  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]133  s->pfw = fftw_plan_r2r_1d(winsize, s->in,  s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
134  s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
[237f632]135#endif
[d453a4a]136  pthread_mutex_unlock(&aubio_fftw_mutex);
[4f4299d]137  for (i = 0; i < s->winsize; i++) {
138    s->in[i] = 0.;
139    s->out[i] = 0.;
140  }
141  for (i = 0; i < s->fft_size; i++) {
142    s->specdata[i] = 0.;
143  }
[729a3c0]144#else
[54dd945]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
[729a3c0]156  s->winsize = winsize;
157  s->fft_size = winsize / 2 + 1;
158  s->compspec = new_fvec(winsize);
[f61c88a]159  s->in    = AUBIO_ARRAY(double, s->winsize);
160  s->out   = AUBIO_ARRAY(double, s->winsize);
[729a3c0]161  s->ip    = AUBIO_ARRAY(int   , s->fft_size);
162  s->w     = AUBIO_ARRAY(double, s->fft_size);
163  s->ip[0] = 0;
[54dd945]164#endif /* HAVE_ACCELERATE */
165#endif /* HAVE_FFTW3 */
[a47cd35]166  return s;
[96fb8ad]167}
168
169void del_aubio_fft(aubio_fft_t * s) {
[a47cd35]170  /* destroy data */
[aadd27a]171  del_fvec(s->compspec);
[54dd945]172#ifdef HAVE_FFTW3             // using FFTW3
[a47cd35]173  fftw_destroy_plan(s->pfw);
174  fftw_destroy_plan(s->pbw);
175  fftw_free(s->specdata);
[729a3c0]176#else /* HAVE_FFTW3 */
[54dd945]177#ifdef HAVE_ACCELERATE        // using ACCELERATE
178  AUBIO_FREE(s->spec.realp);
179  AUBIO_FREE(s->spec.imagp);
180#else                         // using OOURA
[729a3c0]181  AUBIO_FREE(s->w);
182  AUBIO_FREE(s->ip);
[54dd945]183#endif /* HAVE_ACCELERATE */
[729a3c0]184#endif /* HAVE_FFTW3 */
[a47cd35]185  AUBIO_FREE(s->out);
[729a3c0]186  AUBIO_FREE(s->in);
[a47cd35]187  AUBIO_FREE(s);
[96fb8ad]188}
189
[aadd27a]190void aubio_fft_do(aubio_fft_t * s, fvec_t * input, cvec_t * spectrum) {
191  aubio_fft_do_complex(s, input, s->compspec);
192  aubio_fft_get_spectrum(s->compspec, spectrum);
[96fb8ad]193}
194
[aadd27a]195void aubio_fft_rdo(aubio_fft_t * s, cvec_t * spectrum, fvec_t * output) {
196  aubio_fft_get_realimag(spectrum, s->compspec);
197  aubio_fft_rdo_complex(s, s->compspec, output);
[96fb8ad]198}
199
[aadd27a]200void aubio_fft_do_complex(aubio_fft_t * s, fvec_t * input, fvec_t * compspec) {
[729a3c0]201  uint_t i;
202  for (i=0; i < s->winsize; i++) {
203    s->in[i] = input->data[i];
[d95ff38]204  }
[54dd945]205#ifdef HAVE_FFTW3             // using FFTW3
[d95ff38]206  fftw_execute(s->pfw);
[4b6937b]207#ifdef HAVE_COMPLEX_H
[d95ff38]208  compspec->data[0] = REAL(s->specdata[0]);
[729a3c0]209  for (i = 1; i < s->fft_size -1 ; i++) {
210    compspec->data[i] = REAL(s->specdata[i]);
211    compspec->data[compspec->length - i] = IMAG(s->specdata[i]);
[d95ff38]212  }
213  compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
[729a3c0]214#else /* HAVE_COMPLEX_H  */
215  for (i = 0; i < s->fft_size; i++) {
216    compspec->data[i] = s->specdata[i];
[237f632]217  }
[729a3c0]218#endif /* HAVE_COMPLEX_H */
219#else /* HAVE_FFTW3 */
[54dd945]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
[729a3c0]236  rdft(s->winsize, 1, s->in, s->ip, s->w);
237  compspec->data[0] = s->in[0];
238  compspec->data[s->winsize / 2] = s->in[1];
239  for (i = 1; i < s->fft_size - 1; i++) {
240    compspec->data[i] = s->in[2 * i];
241    compspec->data[s->winsize - i] = - s->in[2 * i + 1];
242  }
[54dd945]243#endif /* HAVE_ACCELERATE */
[729a3c0]244#endif /* HAVE_FFTW3 */
[96fb8ad]245}
246
[aadd27a]247void aubio_fft_rdo_complex(aubio_fft_t * s, fvec_t * compspec, fvec_t * output) {
[729a3c0]248  uint_t i;
249#ifdef HAVE_FFTW3
[aadd27a]250  const smpl_t renorm = 1./(smpl_t)s->winsize;
[4b6937b]251#ifdef HAVE_COMPLEX_H
[d95ff38]252  s->specdata[0] = compspec->data[0];
[729a3c0]253  for (i=1; i < s->fft_size - 1; i++) {
254    s->specdata[i] = compspec->data[i] +
255      I * compspec->data[compspec->length - i];
[d95ff38]256  }
257  s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1];
[237f632]258#else
[729a3c0]259  for (i=0; i < s->fft_size; i++) {
260    s->specdata[i] = compspec->data[i];
[d95ff38]261  }
[aadd27a]262#endif
[d95ff38]263  fftw_execute(s->pbw);
[729a3c0]264  for (i = 0; i < output->length; i++) {
265    output->data[i] = s->out[i]*renorm;
266  }
267#else /* HAVE_FFTW3 */
[54dd945]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
[729a3c0]287  smpl_t scale = 2.0 / s->winsize;
288  s->out[0] = compspec->data[0];
289  s->out[1] = compspec->data[s->winsize / 2];
290  for (i = 1; i < s->fft_size - 1; i++) {
291    s->out[2 * i] = compspec->data[i];
292    s->out[2 * i + 1] = - compspec->data[s->winsize - i];
[aadd27a]293  }
[729a3c0]294  rdft(s->winsize, -1, s->out, s->ip, s->w);
295  for (i=0; i < s->winsize; i++) {
296    output->data[i] = s->out[i] * scale;
297  }
[54dd945]298#endif /* HAVE_ACCELERATE */
[729a3c0]299#endif /* HAVE_FFTW3 */
[237f632]300}
301
[aadd27a]302void aubio_fft_get_spectrum(fvec_t * compspec, cvec_t * spectrum) {
303  aubio_fft_get_phas(compspec, spectrum);
304  aubio_fft_get_norm(compspec, spectrum);
[237f632]305}
306
[aadd27a]307void aubio_fft_get_realimag(cvec_t * spectrum, fvec_t * compspec) {
308  aubio_fft_get_imag(spectrum, compspec);
309  aubio_fft_get_real(spectrum, compspec);
[237f632]310}
311
[aadd27a]312void aubio_fft_get_phas(fvec_t * compspec, cvec_t * spectrum) {
[729a3c0]313  uint_t i;
[d95ff38]314  if (compspec->data[0] < 0) {
315    spectrum->phas[0] = PI;
316  } else {
317    spectrum->phas[0] = 0.;
318  }
[729a3c0]319  for (i=1; i < spectrum->length - 1; i++) {
320    spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i],
321        compspec->data[i]);
[d95ff38]322  }
323  if (compspec->data[compspec->length/2] < 0) {
324    spectrum->phas[spectrum->length - 1] = PI;
325  } else {
326    spectrum->phas[spectrum->length - 1] = 0.;
[aadd27a]327  }
[f88a326]328}
329
[aadd27a]330void aubio_fft_get_norm(fvec_t * compspec, cvec_t * spectrum) {
[729a3c0]331  uint_t i = 0;
[d95ff38]332  spectrum->norm[0] = ABS(compspec->data[0]);
[729a3c0]333  for (i=1; i < spectrum->length - 1; i++) {
334    spectrum->norm[i] = SQRT(SQR(compspec->data[i])
335        + SQR(compspec->data[compspec->length - i]) );
[a47cd35]336  }
[729a3c0]337  spectrum->norm[spectrum->length-1] =
[d95ff38]338    ABS(compspec->data[compspec->length/2]);
[f88a326]339}
340
[aadd27a]341void aubio_fft_get_imag(cvec_t * spectrum, fvec_t * compspec) {
[729a3c0]342  uint_t i;
343  for (i = 1; i < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; i++) {
344    compspec->data[compspec->length - i] =
345      spectrum->norm[i]*SIN(spectrum->phas[i]);
[a47cd35]346  }
[f88a326]347}
348
[aadd27a]349void aubio_fft_get_real(cvec_t * spectrum, fvec_t * compspec) {
[729a3c0]350  uint_t i;
351  for (i = 0; i < compspec->length / 2 + 1; i++) {
352    compspec->data[i] = 
353      spectrum->norm[i]*COS(spectrum->phas[i]);
[aadd27a]354  }
[f88a326]355}
Note: See TracBrowser for help on using the repository browser.