source: src/spectral/fft.c @ f107e12

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

src/spectral/fft.c: fix in and out sizes for ooura

  • Property mode set to 100644
File size: 8.3 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
[729a3c0]27#ifdef HAVE_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
[729a3c0]80#endif /* HAVE_FFTW3 */
81
[96fb8ad]82struct _aubio_fft_t {
[aadd27a]83  uint_t winsize;
84  uint_t fft_size;
[729a3c0]85#ifdef HAVE_FFTW3
[aadd27a]86  real_t *in, *out;
[4b6937b]87  fftw_plan pfw, pbw;
[aadd27a]88  fft_data_t * specdata;     /* complex spectral data */
[729a3c0]89#else
90  double *in, *out;
91  double *w;
92  int *ip;
93#endif /* HAVE_FFTW3 */
[aadd27a]94  fvec_t * compspec;
[96fb8ad]95};
96
[729a3c0]97#ifndef HAVE_FFTW3
98// let's use ooura instead
99extern void rdft(int, int, double *, int *, double *);
100#endif
101
102aubio_fft_t * new_aubio_fft (uint_t winsize) {
[a47cd35]103  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
[729a3c0]104#ifdef HAVE_FFTW3
[8b3a7e7]105  uint_t i;
[aadd27a]106  s->winsize  = winsize;
[a47cd35]107  /* allocate memory */
[aadd27a]108  s->in       = AUBIO_ARRAY(real_t,winsize);
109  s->out      = AUBIO_ARRAY(real_t,winsize);
[d95ff38]110  s->compspec = new_fvec(winsize);
[a47cd35]111  /* create plans */
[d453a4a]112  pthread_mutex_lock(&aubio_fftw_mutex);
[237f632]113#ifdef HAVE_COMPLEX_H
[4b6937b]114  s->fft_size = winsize/2 + 1;
[a47cd35]115  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]116  s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in,  s->specdata, FFTW_ESTIMATE);
117  s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE);
[237f632]118#else
[aadd27a]119  s->fft_size = winsize;
[a47cd35]120  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]121  s->pfw = fftw_plan_r2r_1d(winsize, s->in,  s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
122  s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
[237f632]123#endif
[d453a4a]124  pthread_mutex_unlock(&aubio_fftw_mutex);
[4f4299d]125  for (i = 0; i < s->winsize; i++) {
126    s->in[i] = 0.;
127    s->out[i] = 0.;
128  }
129  for (i = 0; i < s->fft_size; i++) {
130    s->specdata[i] = 0.;
131  }
[729a3c0]132#else
133  s->winsize = winsize;
134  s->fft_size = winsize / 2 + 1;
135  s->compspec = new_fvec(winsize);
[f61c88a]136  s->in    = AUBIO_ARRAY(double, s->winsize);
137  s->out   = AUBIO_ARRAY(double, s->winsize);
[729a3c0]138  s->ip    = AUBIO_ARRAY(int   , s->fft_size);
139  s->w     = AUBIO_ARRAY(double, s->fft_size);
140  s->ip[0] = 0;
141#endif
[a47cd35]142  return s;
[96fb8ad]143}
144
145void del_aubio_fft(aubio_fft_t * s) {
[a47cd35]146  /* destroy data */
[aadd27a]147  del_fvec(s->compspec);
[729a3c0]148#ifdef HAVE_FFTW3
[a47cd35]149  fftw_destroy_plan(s->pfw);
150  fftw_destroy_plan(s->pbw);
151  fftw_free(s->specdata);
[729a3c0]152#else /* HAVE_FFTW3 */
153  AUBIO_FREE(s->w);
154  AUBIO_FREE(s->ip);
155#endif /* HAVE_FFTW3 */
[a47cd35]156  AUBIO_FREE(s->out);
[729a3c0]157  AUBIO_FREE(s->in);
[a47cd35]158  AUBIO_FREE(s);
[96fb8ad]159}
160
[aadd27a]161void aubio_fft_do(aubio_fft_t * s, fvec_t * input, cvec_t * spectrum) {
162  aubio_fft_do_complex(s, input, s->compspec);
163  aubio_fft_get_spectrum(s->compspec, spectrum);
[96fb8ad]164}
165
[aadd27a]166void aubio_fft_rdo(aubio_fft_t * s, cvec_t * spectrum, fvec_t * output) {
167  aubio_fft_get_realimag(spectrum, s->compspec);
168  aubio_fft_rdo_complex(s, s->compspec, output);
[96fb8ad]169}
170
[aadd27a]171void aubio_fft_do_complex(aubio_fft_t * s, fvec_t * input, fvec_t * compspec) {
[729a3c0]172  uint_t i;
173  for (i=0; i < s->winsize; i++) {
174    s->in[i] = input->data[i];
[d95ff38]175  }
[729a3c0]176#ifdef HAVE_FFTW3
[d95ff38]177  fftw_execute(s->pfw);
[4b6937b]178#ifdef HAVE_COMPLEX_H
[d95ff38]179  compspec->data[0] = REAL(s->specdata[0]);
[729a3c0]180  for (i = 1; i < s->fft_size -1 ; i++) {
181    compspec->data[i] = REAL(s->specdata[i]);
182    compspec->data[compspec->length - i] = IMAG(s->specdata[i]);
[d95ff38]183  }
184  compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
[729a3c0]185#else /* HAVE_COMPLEX_H  */
186  for (i = 0; i < s->fft_size; i++) {
187    compspec->data[i] = s->specdata[i];
[237f632]188  }
[729a3c0]189#endif /* HAVE_COMPLEX_H */
190#else /* HAVE_FFTW3 */
191  rdft(s->winsize, 1, s->in, s->ip, s->w);
192  compspec->data[0] = s->in[0];
193  compspec->data[s->winsize / 2] = s->in[1];
194  for (i = 1; i < s->fft_size - 1; i++) {
195    compspec->data[i] = s->in[2 * i];
196    compspec->data[s->winsize - i] = - s->in[2 * i + 1];
197  }
198#endif /* HAVE_FFTW3 */
[96fb8ad]199}
200
[aadd27a]201void aubio_fft_rdo_complex(aubio_fft_t * s, fvec_t * compspec, fvec_t * output) {
[729a3c0]202  uint_t i;
203#ifdef HAVE_FFTW3
[aadd27a]204  const smpl_t renorm = 1./(smpl_t)s->winsize;
[4b6937b]205#ifdef HAVE_COMPLEX_H
[d95ff38]206  s->specdata[0] = compspec->data[0];
[729a3c0]207  for (i=1; i < s->fft_size - 1; i++) {
208    s->specdata[i] = compspec->data[i] +
209      I * compspec->data[compspec->length - i];
[d95ff38]210  }
211  s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1];
[237f632]212#else
[729a3c0]213  for (i=0; i < s->fft_size; i++) {
214    s->specdata[i] = compspec->data[i];
[d95ff38]215  }
[aadd27a]216#endif
[d95ff38]217  fftw_execute(s->pbw);
[729a3c0]218  for (i = 0; i < output->length; i++) {
219    output->data[i] = s->out[i]*renorm;
220  }
221#else /* HAVE_FFTW3 */
222  smpl_t scale = 2.0 / s->winsize;
223  s->out[0] = compspec->data[0];
224  s->out[1] = compspec->data[s->winsize / 2];
225  for (i = 1; i < s->fft_size - 1; i++) {
226    s->out[2 * i] = compspec->data[i];
227    s->out[2 * i + 1] = - compspec->data[s->winsize - i];
[aadd27a]228  }
[729a3c0]229  rdft(s->winsize, -1, s->out, s->ip, s->w);
230  for (i=0; i < s->winsize; i++) {
231    output->data[i] = s->out[i] * scale;
232  }
233#endif /* HAVE_FFTW3 */
[237f632]234}
235
[aadd27a]236void aubio_fft_get_spectrum(fvec_t * compspec, cvec_t * spectrum) {
237  aubio_fft_get_phas(compspec, spectrum);
238  aubio_fft_get_norm(compspec, spectrum);
[237f632]239}
240
[aadd27a]241void aubio_fft_get_realimag(cvec_t * spectrum, fvec_t * compspec) {
242  aubio_fft_get_imag(spectrum, compspec);
243  aubio_fft_get_real(spectrum, compspec);
[237f632]244}
245
[aadd27a]246void aubio_fft_get_phas(fvec_t * compspec, cvec_t * spectrum) {
[729a3c0]247  uint_t i;
[d95ff38]248  if (compspec->data[0] < 0) {
249    spectrum->phas[0] = PI;
250  } else {
251    spectrum->phas[0] = 0.;
252  }
[729a3c0]253  for (i=1; i < spectrum->length - 1; i++) {
254    spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i],
255        compspec->data[i]);
[d95ff38]256  }
257  if (compspec->data[compspec->length/2] < 0) {
258    spectrum->phas[spectrum->length - 1] = PI;
259  } else {
260    spectrum->phas[spectrum->length - 1] = 0.;
[aadd27a]261  }
[f88a326]262}
263
[aadd27a]264void aubio_fft_get_norm(fvec_t * compspec, cvec_t * spectrum) {
[729a3c0]265  uint_t i = 0;
[d95ff38]266  spectrum->norm[0] = ABS(compspec->data[0]);
[729a3c0]267  for (i=1; i < spectrum->length - 1; i++) {
268    spectrum->norm[i] = SQRT(SQR(compspec->data[i])
269        + SQR(compspec->data[compspec->length - i]) );
[a47cd35]270  }
[729a3c0]271  spectrum->norm[spectrum->length-1] =
[d95ff38]272    ABS(compspec->data[compspec->length/2]);
[f88a326]273}
274
[aadd27a]275void aubio_fft_get_imag(cvec_t * spectrum, fvec_t * compspec) {
[729a3c0]276  uint_t i;
277  for (i = 1; i < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; i++) {
278    compspec->data[compspec->length - i] =
279      spectrum->norm[i]*SIN(spectrum->phas[i]);
[a47cd35]280  }
[f88a326]281}
282
[aadd27a]283void aubio_fft_get_real(cvec_t * spectrum, fvec_t * compspec) {
[729a3c0]284  uint_t i;
285  for (i = 0; i < compspec->length / 2 + 1; i++) {
286    compspec->data[i] = 
287      spectrum->norm[i]*COS(spectrum->phas[i]);
[aadd27a]288  }
[f88a326]289}
Note: See TracBrowser for help on using the repository browser.