source: src/spectral/fft.c @ 7efeffd

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 7efeffd 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
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
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#endif /* HAVE_FFTW3 */
81
82struct _aubio_fft_t {
83  uint_t winsize;
84  uint_t fft_size;
85#ifdef HAVE_FFTW3
86  real_t *in, *out;
87  fftw_plan pfw, pbw;
88  fft_data_t * specdata;     /* complex spectral data */
89#else
90  double *in, *out;
91  double *w;
92  int *ip;
93#endif /* HAVE_FFTW3 */
94  fvec_t * compspec;
95};
96
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) {
103  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
104#ifdef HAVE_FFTW3
105  uint_t i;
106  s->winsize  = winsize;
107  /* allocate memory */
108  s->in       = AUBIO_ARRAY(real_t,winsize);
109  s->out      = AUBIO_ARRAY(real_t,winsize);
110  s->compspec = new_fvec(winsize);
111  /* create plans */
112  pthread_mutex_lock(&aubio_fftw_mutex);
113#ifdef HAVE_COMPLEX_H
114  s->fft_size = winsize/2 + 1;
115  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
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);
118#else
119  s->fft_size = winsize;
120  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
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);
123#endif
124  pthread_mutex_unlock(&aubio_fftw_mutex);
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  }
132#else
133  s->winsize = winsize;
134  s->fft_size = winsize / 2 + 1;
135  s->compspec = new_fvec(winsize);
136  s->in    = AUBIO_ARRAY(double, s->winsize);
137  s->out   = AUBIO_ARRAY(double, s->winsize);
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
142  return s;
143}
144
145void del_aubio_fft(aubio_fft_t * s) {
146  /* destroy data */
147  del_fvec(s->compspec);
148#ifdef HAVE_FFTW3
149  fftw_destroy_plan(s->pfw);
150  fftw_destroy_plan(s->pbw);
151  fftw_free(s->specdata);
152#else /* HAVE_FFTW3 */
153  AUBIO_FREE(s->w);
154  AUBIO_FREE(s->ip);
155#endif /* HAVE_FFTW3 */
156  AUBIO_FREE(s->out);
157  AUBIO_FREE(s->in);
158  AUBIO_FREE(s);
159}
160
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);
164}
165
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);
169}
170
171void aubio_fft_do_complex(aubio_fft_t * s, fvec_t * input, fvec_t * compspec) {
172  uint_t i;
173  for (i=0; i < s->winsize; i++) {
174    s->in[i] = input->data[i];
175  }
176#ifdef HAVE_FFTW3
177  fftw_execute(s->pfw);
178#ifdef HAVE_COMPLEX_H
179  compspec->data[0] = REAL(s->specdata[0]);
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]);
183  }
184  compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
185#else /* HAVE_COMPLEX_H  */
186  for (i = 0; i < s->fft_size; i++) {
187    compspec->data[i] = s->specdata[i];
188  }
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 */
199}
200
201void aubio_fft_rdo_complex(aubio_fft_t * s, fvec_t * compspec, fvec_t * output) {
202  uint_t i;
203#ifdef HAVE_FFTW3
204  const smpl_t renorm = 1./(smpl_t)s->winsize;
205#ifdef HAVE_COMPLEX_H
206  s->specdata[0] = compspec->data[0];
207  for (i=1; i < s->fft_size - 1; i++) {
208    s->specdata[i] = compspec->data[i] +
209      I * compspec->data[compspec->length - i];
210  }
211  s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1];
212#else
213  for (i=0; i < s->fft_size; i++) {
214    s->specdata[i] = compspec->data[i];
215  }
216#endif
217  fftw_execute(s->pbw);
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];
228  }
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 */
234}
235
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);
239}
240
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);
244}
245
246void aubio_fft_get_phas(fvec_t * compspec, cvec_t * spectrum) {
247  uint_t i;
248  if (compspec->data[0] < 0) {
249    spectrum->phas[0] = PI;
250  } else {
251    spectrum->phas[0] = 0.;
252  }
253  for (i=1; i < spectrum->length - 1; i++) {
254    spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i],
255        compspec->data[i]);
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.;
261  }
262}
263
264void aubio_fft_get_norm(fvec_t * compspec, cvec_t * spectrum) {
265  uint_t i = 0;
266  spectrum->norm[0] = ABS(compspec->data[0]);
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]) );
270  }
271  spectrum->norm[spectrum->length-1] =
272    ABS(compspec->data[compspec->length/2]);
273}
274
275void aubio_fft_get_imag(cvec_t * spectrum, fvec_t * compspec) {
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]);
280  }
281}
282
283void aubio_fft_get_real(cvec_t * spectrum, fvec_t * compspec) {
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]);
288  }
289}
Note: See TracBrowser for help on using the repository browser.