source: src/spectral/fft.c @ e6a78ea

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

src: update all headers to GPLv3

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