source: src/spectral/fft.c @ fa6f6aa

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

src/spectral/ooura_fft8g.c: use float when double is not needed

  • Property mode set to 100644
File size: 12.5 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#warning "Using aubio in double precision with fftw3 in single precision"
68#endif /* HAVE_AUBIO_DOUBLE */
69#define real_t float
70#else /* HAVE_FFTW3F */
71#if !HAVE_AUBIO_DOUBLE
72#warning "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#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, smpl_t *, int *, smpl_t *);
88
89#endif /* HAVE_ACCELERATE */
90#endif /* HAVE_FFTW3 */
91
92struct _aubio_fft_t {
93  uint_t winsize;
94  uint_t fft_size;
95#ifdef HAVE_FFTW3             // using FFTW3
96  real_t *in, *out;
97  fftw_plan pfw, pbw;
98  fft_data_t * specdata;     /* complex spectral data */
99#else
100#ifdef HAVE_ACCELERATE        // using ACCELERATE
101  int log2fftsize;
102#if !HAVE_AUBIO_DOUBLE
103  FFTSetup fftSetup;
104  DSPSplitComplex spec;
105  float *in, *out;
106#else
107  FFTSetupD fftSetup;
108  DSPDoubleSplitComplex spec;
109  double *in, *out;
110#endif
111#else                         // using OOURA
112  smpl_t *in, *out;
113  smpl_t *w;
114  int *ip;
115#endif /* HAVE_ACCELERATE */
116#endif /* HAVE_FFTW3 */
117  fvec_t * compspec;
118};
119
120aubio_fft_t * new_aubio_fft (uint_t winsize) {
121  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
122#ifdef HAVE_FFTW3
123  uint_t i;
124  s->winsize  = winsize;
125  /* allocate memory */
126  s->in       = AUBIO_ARRAY(real_t,winsize);
127  s->out      = AUBIO_ARRAY(real_t,winsize);
128  s->compspec = new_fvec(winsize);
129  /* create plans */
130  pthread_mutex_lock(&aubio_fftw_mutex);
131#ifdef HAVE_COMPLEX_H
132  s->fft_size = winsize/2 + 1;
133  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
134  s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in,  s->specdata, FFTW_ESTIMATE);
135  s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE);
136#else
137  s->fft_size = winsize;
138  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
139  s->pfw = fftw_plan_r2r_1d(winsize, s->in,  s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
140  s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
141#endif
142  pthread_mutex_unlock(&aubio_fftw_mutex);
143  for (i = 0; i < s->winsize; i++) {
144    s->in[i] = 0.;
145    s->out[i] = 0.;
146  }
147  for (i = 0; i < s->fft_size; i++) {
148    s->specdata[i] = 0.;
149  }
150#else
151#ifdef HAVE_ACCELERATE        // using ACCELERATE
152  s->winsize = winsize;
153  s->fft_size = winsize;
154  s->compspec = new_fvec(winsize);
155  s->log2fftsize = (uint_t)log2f(s->fft_size);
156#if !HAVE_AUBIO_DOUBLE
157  s->in = AUBIO_ARRAY(float, s->fft_size);
158  s->out = AUBIO_ARRAY(float, s->fft_size);
159  s->spec.realp = AUBIO_ARRAY(float, s->fft_size/2);
160  s->spec.imagp = AUBIO_ARRAY(float, s->fft_size/2);
161  s->fftSetup = vDSP_create_fftsetup(s->log2fftsize, FFT_RADIX2);
162#else
163  s->in = AUBIO_ARRAY(double, s->fft_size);
164  s->out = AUBIO_ARRAY(double, s->fft_size);
165  s->spec.realp = AUBIO_ARRAY(double, s->fft_size/2);
166  s->spec.imagp = AUBIO_ARRAY(double, s->fft_size/2);
167  s->fftSetup = vDSP_create_fftsetupD(s->log2fftsize, FFT_RADIX2);
168#endif
169#else                         // using OOURA
170  s->winsize = winsize;
171  s->fft_size = winsize / 2 + 1;
172  s->compspec = new_fvec(winsize);
173  s->in    = AUBIO_ARRAY(smpl_t, s->winsize);
174  s->out   = AUBIO_ARRAY(smpl_t, s->winsize);
175  s->ip    = AUBIO_ARRAY(int   , s->fft_size);
176  s->w     = AUBIO_ARRAY(smpl_t, s->fft_size);
177  s->ip[0] = 0;
178#endif /* HAVE_ACCELERATE */
179#endif /* HAVE_FFTW3 */
180  return s;
181}
182
183void del_aubio_fft(aubio_fft_t * s) {
184  /* destroy data */
185  del_fvec(s->compspec);
186#ifdef HAVE_FFTW3             // using FFTW3
187  fftw_destroy_plan(s->pfw);
188  fftw_destroy_plan(s->pbw);
189  fftw_free(s->specdata);
190#else /* HAVE_FFTW3 */
191#ifdef HAVE_ACCELERATE        // using ACCELERATE
192  AUBIO_FREE(s->spec.realp);
193  AUBIO_FREE(s->spec.imagp);
194#if !HAVE_AUBIO_DOUBLE
195  vDSP_destroy_fftsetup(s->fftSetup);
196#else
197  vDSP_destroy_fftsetupD(s->fftSetup);
198#endif
199#else                         // using OOURA
200  AUBIO_FREE(s->w);
201  AUBIO_FREE(s->ip);
202#endif /* HAVE_ACCELERATE */
203#endif /* HAVE_FFTW3 */
204  AUBIO_FREE(s->out);
205  AUBIO_FREE(s->in);
206  AUBIO_FREE(s);
207}
208
209void aubio_fft_do(aubio_fft_t * s, fvec_t * input, cvec_t * spectrum) {
210  aubio_fft_do_complex(s, input, s->compspec);
211  aubio_fft_get_spectrum(s->compspec, spectrum);
212}
213
214void aubio_fft_rdo(aubio_fft_t * s, cvec_t * spectrum, fvec_t * output) {
215  aubio_fft_get_realimag(spectrum, s->compspec);
216  aubio_fft_rdo_complex(s, s->compspec, output);
217}
218
219void aubio_fft_do_complex(aubio_fft_t * s, fvec_t * input, fvec_t * compspec) {
220  uint_t i;
221  for (i=0; i < s->winsize; i++) {
222    s->in[i] = input->data[i];
223  }
224#ifdef HAVE_FFTW3             // using FFTW3
225  fftw_execute(s->pfw);
226#ifdef HAVE_COMPLEX_H
227  compspec->data[0] = REAL(s->specdata[0]);
228  for (i = 1; i < s->fft_size -1 ; i++) {
229    compspec->data[i] = REAL(s->specdata[i]);
230    compspec->data[compspec->length - i] = IMAG(s->specdata[i]);
231  }
232  compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
233#else /* HAVE_COMPLEX_H  */
234  for (i = 0; i < s->fft_size; i++) {
235    compspec->data[i] = s->specdata[i];
236  }
237#endif /* HAVE_COMPLEX_H */
238#else /* HAVE_FFTW3 */
239#ifdef HAVE_ACCELERATE        // using ACCELERATE
240#if !HAVE_AUBIO_DOUBLE
241  // convert real data to even/odd format used in vDSP
242  vDSP_ctoz((DSPComplex*)s->in, 2, &s->spec, 1, s->fft_size/2);
243  // compute the FFT
244  vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_FORWARD);
245#else
246  // convert real data to even/odd format used in vDSP
247  vDSP_ctozD((DSPDoubleComplex*)s->in, 2, &s->spec, 1, s->fft_size/2);
248  // compute the FFT
249  vDSP_fft_zripD(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_FORWARD);
250#endif
251  // convert from vDSP complex split to [ r0, r1, ..., rN, iN-1, .., i2, i1]
252  compspec->data[0] = s->spec.realp[0];
253  compspec->data[s->fft_size / 2] = s->spec.imagp[0];
254  for (i = 1; i < s->fft_size / 2; i++) {
255    compspec->data[i] = s->spec.realp[i];
256    compspec->data[s->fft_size - i] = s->spec.imagp[i];
257  }
258  // apply scaling
259  smpl_t scale = 1./2.;
260#if !HAVE_AUBIO_DOUBLE
261  vDSP_vsmul(compspec->data, 1, &scale, compspec->data, 1, s->fft_size);
262#else
263  vDSP_vsmulD(compspec->data, 1, &scale, compspec->data, 1, s->fft_size);
264#endif
265#else                         // using OOURA
266  rdft(s->winsize, 1, s->in, s->ip, s->w);
267  compspec->data[0] = s->in[0];
268  compspec->data[s->winsize / 2] = s->in[1];
269  for (i = 1; i < s->fft_size - 1; i++) {
270    compspec->data[i] = s->in[2 * i];
271    compspec->data[s->winsize - i] = - s->in[2 * i + 1];
272  }
273#endif /* HAVE_ACCELERATE */
274#endif /* HAVE_FFTW3 */
275}
276
277void aubio_fft_rdo_complex(aubio_fft_t * s, fvec_t * compspec, fvec_t * output) {
278  uint_t i;
279#ifdef HAVE_FFTW3
280  const smpl_t renorm = 1./(smpl_t)s->winsize;
281#ifdef HAVE_COMPLEX_H
282  s->specdata[0] = compspec->data[0];
283  for (i=1; i < s->fft_size - 1; i++) {
284    s->specdata[i] = compspec->data[i] +
285      I * compspec->data[compspec->length - i];
286  }
287  s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1];
288#else
289  for (i=0; i < s->fft_size; i++) {
290    s->specdata[i] = compspec->data[i];
291  }
292#endif
293  fftw_execute(s->pbw);
294  for (i = 0; i < output->length; i++) {
295    output->data[i] = s->out[i]*renorm;
296  }
297#else /* HAVE_FFTW3 */
298#ifdef HAVE_ACCELERATE        // using ACCELERATE
299  // convert from real imag  [ r0, r1, ..., rN, iN-1, .., i2, i1]
300  // to vDSP packed format   [ r0, rN, r1, i1, ..., rN-1, iN-1 ]
301  s->out[0] = compspec->data[0];
302  s->out[1] = compspec->data[s->winsize / 2];
303  for (i = 1; i < s->fft_size / 2; i++) {
304    s->out[2 * i] = compspec->data[i];
305    s->out[2 * i + 1] = compspec->data[s->winsize - i];
306  }
307#if !HAVE_AUBIO_DOUBLE
308  // convert to split complex format used in vDSP
309  vDSP_ctoz((DSPComplex*)s->out, 2, &s->spec, 1, s->fft_size/2);
310  // compute the FFT
311  vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_INVERSE);
312  // convert result to real output
313  vDSP_ztoc(&s->spec, 1, (DSPComplex*)output->data, 2, s->fft_size/2);
314  // apply scaling
315  smpl_t scale = 1.0 / s->winsize;
316  vDSP_vsmul(output->data, 1, &scale, output->data, 1, s->fft_size);
317#else
318  // convert to split complex format used in vDSP
319  vDSP_ctozD((DSPDoubleComplex*)s->out, 2, &s->spec, 1, s->fft_size/2);
320  // compute the FFT
321  vDSP_fft_zripD(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_INVERSE);
322  // convert result to real output
323  vDSP_ztocD(&s->spec, 1, (DSPDoubleComplex*)output->data, 2, s->fft_size/2);
324  // apply scaling
325  smpl_t scale = 1.0 / s->winsize;
326  vDSP_vsmulD(output->data, 1, &scale, output->data, 1, s->fft_size);
327#endif
328#else                         // using OOURA
329  smpl_t scale = 2.0 / s->winsize;
330  s->out[0] = compspec->data[0];
331  s->out[1] = compspec->data[s->winsize / 2];
332  for (i = 1; i < s->fft_size - 1; i++) {
333    s->out[2 * i] = compspec->data[i];
334    s->out[2 * i + 1] = - compspec->data[s->winsize - i];
335  }
336  rdft(s->winsize, -1, s->out, s->ip, s->w);
337  for (i=0; i < s->winsize; i++) {
338    output->data[i] = s->out[i] * scale;
339  }
340#endif /* HAVE_ACCELERATE */
341#endif /* HAVE_FFTW3 */
342}
343
344void aubio_fft_get_spectrum(fvec_t * compspec, cvec_t * spectrum) {
345  aubio_fft_get_phas(compspec, spectrum);
346  aubio_fft_get_norm(compspec, spectrum);
347}
348
349void aubio_fft_get_realimag(cvec_t * spectrum, fvec_t * compspec) {
350  aubio_fft_get_imag(spectrum, compspec);
351  aubio_fft_get_real(spectrum, compspec);
352}
353
354void aubio_fft_get_phas(fvec_t * compspec, cvec_t * spectrum) {
355  uint_t i;
356  if (compspec->data[0] < 0) {
357    spectrum->phas[0] = PI;
358  } else {
359    spectrum->phas[0] = 0.;
360  }
361  for (i=1; i < spectrum->length - 1; i++) {
362    spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i],
363        compspec->data[i]);
364  }
365  if (compspec->data[compspec->length/2] < 0) {
366    spectrum->phas[spectrum->length - 1] = PI;
367  } else {
368    spectrum->phas[spectrum->length - 1] = 0.;
369  }
370}
371
372void aubio_fft_get_norm(fvec_t * compspec, cvec_t * spectrum) {
373  uint_t i = 0;
374  spectrum->norm[0] = ABS(compspec->data[0]);
375  for (i=1; i < spectrum->length - 1; i++) {
376    spectrum->norm[i] = SQRT(SQR(compspec->data[i])
377        + SQR(compspec->data[compspec->length - i]) );
378  }
379  spectrum->norm[spectrum->length-1] =
380    ABS(compspec->data[compspec->length/2]);
381}
382
383void aubio_fft_get_imag(cvec_t * spectrum, fvec_t * compspec) {
384  uint_t i;
385  for (i = 1; i < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; i++) {
386    compspec->data[compspec->length - i] =
387      spectrum->norm[i]*SIN(spectrum->phas[i]);
388  }
389}
390
391void aubio_fft_get_real(cvec_t * spectrum, fvec_t * compspec) {
392  uint_t i;
393  for (i = 0; i < compspec->length / 2 + 1; i++) {
394    compspec->data[i] = 
395      spectrum->norm[i]*COS(spectrum->phas[i]);
396  }
397}
Note: See TracBrowser for help on using the repository browser.