source: src/spectral/fft.c @ b5d32cb

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since b5d32cb was b5d32cb, checked in by Paul Brossier <piem@piem.org>, 5 years ago

src/spectral/fft.c: use memcpy

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