source: src/spectral/fft.c @ d453a4a

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

src/spectral/fft.c: lock fftw plan creation (closes #9), thanks to Hedde Bosman

  • Property mode set to 100644
File size: 6.8 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/* note that <complex.h> is not included here but only in aubio_priv.h, so that
28 * c++ projects can still use their own complex definition. */
29#include <fftw3.h>
30#include <pthread.h>
31
32#ifdef HAVE_COMPLEX_H
33#if HAVE_FFTW3F
34/** fft data type with complex.h and fftw3f */
35#define FFTW_TYPE fftwf_complex
36#else
37/** fft data type with complex.h and fftw3 */
38#define FFTW_TYPE fftw_complex
39#endif
40#else
41#if HAVE_FFTW3F
42/** fft data type without complex.h and with fftw3f */
43#define FFTW_TYPE float
44#else
45/** fft data type without complex.h and with fftw */
46#define FFTW_TYPE double
47#endif
48#endif
49
50/** fft data type */
51typedef FFTW_TYPE fft_data_t;
52
53#if HAVE_FFTW3F
54#define fftw_malloc            fftwf_malloc
55#define fftw_free              fftwf_free
56#define fftw_execute           fftwf_execute
57#define fftw_plan_dft_r2c_1d   fftwf_plan_dft_r2c_1d
58#define fftw_plan_dft_c2r_1d   fftwf_plan_dft_c2r_1d
59#define fftw_plan_r2r_1d       fftwf_plan_r2r_1d
60#define fftw_plan              fftwf_plan
61#define fftw_destroy_plan      fftwf_destroy_plan
62#endif
63
64#if HAVE_FFTW3F
65#if HAVE_AUBIO_DOUBLE
66#warning "Using aubio in double precision with fftw3 in single precision"
67#endif /* HAVE_AUBIO_DOUBLE */
68#define real_t float
69#else /* HAVE_FFTW3F */
70#if !HAVE_AUBIO_DOUBLE
71#warning "Using aubio in single precision with fftw3 in double precision"
72#endif /* HAVE_AUBIO_DOUBLE */
73#define real_t double
74#endif /* HAVE_FFTW3F */
75
76// a global mutex for FFTW thread safety
77pthread_mutex_t aubio_fftw_mutex = PTHREAD_MUTEX_INITIALIZER;
78
79struct _aubio_fft_t {
80  uint_t winsize;
81  uint_t fft_size;
82  real_t *in, *out;
83  fftw_plan pfw, pbw;
84  fft_data_t * specdata;     /* complex spectral data */
85  fvec_t * compspec;
86};
87
88aubio_fft_t * new_aubio_fft(uint_t winsize) {
89  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
90  uint_t i;
91  s->winsize  = winsize;
92  /* allocate memory */
93  s->in       = AUBIO_ARRAY(real_t,winsize);
94  s->out      = AUBIO_ARRAY(real_t,winsize);
95  s->compspec = new_fvec(winsize);
96  /* create plans */
97  pthread_mutex_lock(&aubio_fftw_mutex);
98#ifdef HAVE_COMPLEX_H
99  s->fft_size = winsize/2 + 1;
100  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
101  s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in,  s->specdata, FFTW_ESTIMATE);
102  s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE);
103#else
104  s->fft_size = winsize;
105  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
106  s->pfw = fftw_plan_r2r_1d(winsize, s->in,  s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
107  s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
108#endif
109  pthread_mutex_unlock(&aubio_fftw_mutex);
110  for (i = 0; i < s->winsize; i++) {
111    s->in[i] = 0.;
112    s->out[i] = 0.;
113  }
114  for (i = 0; i < s->fft_size; i++) {
115    s->specdata[i] = 0.;
116  }
117  return s;
118}
119
120void del_aubio_fft(aubio_fft_t * s) {
121  /* destroy data */
122  del_fvec(s->compspec);
123  fftw_destroy_plan(s->pfw);
124  fftw_destroy_plan(s->pbw);
125  fftw_free(s->specdata);
126  AUBIO_FREE(s->out);
127  AUBIO_FREE(s->in );
128  AUBIO_FREE(s);
129}
130
131void aubio_fft_do(aubio_fft_t * s, fvec_t * input, cvec_t * spectrum) {
132  aubio_fft_do_complex(s, input, s->compspec);
133  aubio_fft_get_spectrum(s->compspec, spectrum);
134}
135
136void aubio_fft_rdo(aubio_fft_t * s, cvec_t * spectrum, fvec_t * output) {
137  aubio_fft_get_realimag(spectrum, s->compspec);
138  aubio_fft_rdo_complex(s, s->compspec, output);
139}
140
141void aubio_fft_do_complex(aubio_fft_t * s, fvec_t * input, fvec_t * compspec) {
142  uint_t j;
143  for (j=0; j < s->winsize; j++) {
144    s->in[j] = input->data[j];
145  }
146  fftw_execute(s->pfw);
147#ifdef HAVE_COMPLEX_H
148  compspec->data[0] = REAL(s->specdata[0]);
149  for (j = 1; j < s->fft_size -1 ; j++) {
150    compspec->data[j] = REAL(s->specdata[j]);
151    compspec->data[compspec->length - j] = IMAG(s->specdata[j]);
152  }
153  compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
154#else
155  for (j = 0; j < s->fft_size; j++) {
156    compspec->data[j] = s->specdata[j];
157  }
158#endif
159}
160
161void aubio_fft_rdo_complex(aubio_fft_t * s, fvec_t * compspec, fvec_t * output) {
162  uint_t j;
163  const smpl_t renorm = 1./(smpl_t)s->winsize;
164#ifdef HAVE_COMPLEX_H
165  s->specdata[0] = compspec->data[0];
166  for (j=1; j < s->fft_size - 1; j++) {
167    s->specdata[j] = compspec->data[j] + 
168      I * compspec->data[compspec->length - j];
169  }
170  s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1];
171#else
172  for (j=0; j < s->fft_size; j++) {
173    s->specdata[j] = compspec->data[j];
174  }
175#endif
176  fftw_execute(s->pbw);
177  for (j = 0; j < output->length; j++) {
178    output->data[j] = s->out[j]*renorm;
179  }
180}
181
182void aubio_fft_get_spectrum(fvec_t * compspec, cvec_t * spectrum) {
183  aubio_fft_get_phas(compspec, spectrum);
184  aubio_fft_get_norm(compspec, spectrum);
185}
186
187void aubio_fft_get_realimag(cvec_t * spectrum, fvec_t * compspec) {
188  aubio_fft_get_imag(spectrum, compspec);
189  aubio_fft_get_real(spectrum, compspec);
190}
191
192void aubio_fft_get_phas(fvec_t * compspec, cvec_t * spectrum) {
193  uint_t j;
194  if (compspec->data[0] < 0) {
195    spectrum->phas[0] = PI;
196  } else {
197    spectrum->phas[0] = 0.;
198  }
199  for (j=1; j < spectrum->length - 1; j++) {
200    spectrum->phas[j] = ATAN2(compspec->data[compspec->length-j],
201        compspec->data[j]);
202  }
203  if (compspec->data[compspec->length/2] < 0) {
204    spectrum->phas[spectrum->length - 1] = PI;
205  } else {
206    spectrum->phas[spectrum->length - 1] = 0.;
207  }
208}
209
210void aubio_fft_get_norm(fvec_t * compspec, cvec_t * spectrum) {
211  uint_t j = 0;
212  spectrum->norm[0] = ABS(compspec->data[0]);
213  for (j=1; j < spectrum->length - 1; j++) {
214    spectrum->norm[j] = SQRT(SQR(compspec->data[j]) 
215        + SQR(compspec->data[compspec->length - j]) );
216  }
217  spectrum->norm[spectrum->length-1] = 
218    ABS(compspec->data[compspec->length/2]);
219}
220
221void aubio_fft_get_imag(cvec_t * spectrum, fvec_t * compspec) {
222  uint_t j;
223  for (j = 1; j < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; j++) {
224    compspec->data[compspec->length - j] =
225      spectrum->norm[j]*SIN(spectrum->phas[j]);
226  }
227}
228
229void aubio_fft_get_real(cvec_t * spectrum, fvec_t * compspec) {
230  uint_t j;
231  for (j = 0; j < compspec->length / 2 + 1; j++) {
232    compspec->data[j] = 
233      spectrum->norm[j]*COS(spectrum->phas[j]);
234  }
235}
Note: See TracBrowser for help on using the repository browser.