source: src/spectral/fft.c @ 7280e67

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

src/spectral/fft.{c,h}: move fftw3.h include inside fft.c

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2   Copyright (C) 2003 Paul Brossier
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18*/
19
20#include "aubio_priv.h"
21#include "fvec.h"
22#include "cvec.h"
23#include "mathutils.h"
24#include "spectral/fft.h"
25
26/* note that <complex.h> is not included here but only in aubio_priv.h, so that
27 * c++ projects can still use their own complex definition. */
28#include <fftw3.h>
29
30#ifdef HAVE_COMPLEX_H
31#if HAVE_FFTW3F
32/** fft data type with complex.h and fftw3f */
33#define FFTW_TYPE fftwf_complex
34#else
35/** fft data type with complex.h and fftw3 */
36#define FFTW_TYPE fftw_complex
37#endif
38#else
39#if HAVE_FFTW3F
40/** fft data type without complex.h and with fftw3f */
41#define FFTW_TYPE float
42#else
43/** fft data type without complex.h and with fftw */
44#define FFTW_TYPE double
45#endif
46#endif
47
48/** fft data type */
49typedef FFTW_TYPE fft_data_t;
50
51#if HAVE_FFTW3F
52#define fftw_malloc            fftwf_malloc
53#define fftw_free              fftwf_free
54#define fftw_execute           fftwf_execute
55#define fftw_plan_dft_r2c_1d   fftwf_plan_dft_r2c_1d
56#define fftw_plan_dft_c2r_1d   fftwf_plan_dft_c2r_1d
57#define fftw_plan_r2r_1d       fftwf_plan_r2r_1d
58#define fftw_plan              fftwf_plan
59#define fftw_destroy_plan      fftwf_destroy_plan
60#endif
61
62#if HAVE_FFTW3F
63#if HAVE_AUBIO_DOUBLE
64#warning "Using aubio in double precision with fftw3 in single precision"
65#endif /* HAVE_AUBIO_DOUBLE */
66#define real_t float
67#else /* HAVE_FFTW3F */
68#if !HAVE_AUBIO_DOUBLE
69#warning "Using aubio in single precision with fftw3 in double precision"
70#endif /* HAVE_AUBIO_DOUBLE */
71#define real_t double
72#endif /* HAVE_FFTW3F */
73
74struct _aubio_fft_t {
75  uint_t winsize;
76  uint_t channels;
77  uint_t fft_size;
78  real_t *in, *out;
79  fftw_plan pfw, pbw;
80  fft_data_t * specdata;     /* complex spectral data */
81  fvec_t * compspec;
82};
83
84aubio_fft_t * new_aubio_fft(uint_t winsize, uint_t channels) {
85  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
86  s->winsize  = winsize;
87  s->channels = channels;
88  /* allocate memory */
89  s->in       = AUBIO_ARRAY(real_t,winsize);
90  s->out      = AUBIO_ARRAY(real_t,winsize);
91  s->compspec = new_fvec(winsize,channels);
92  /* create plans */
93#ifdef HAVE_COMPLEX_H
94  s->fft_size = winsize/2 + 1;
95  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
96  s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in,  s->specdata, FFTW_ESTIMATE);
97  s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE);
98#else
99  s->fft_size = winsize;
100  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
101  s->pfw = fftw_plan_r2r_1d(winsize, s->in,  s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
102  s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
103#endif
104  return s;
105}
106
107void del_aubio_fft(aubio_fft_t * s) {
108  /* destroy data */
109  del_fvec(s->compspec);
110  fftw_destroy_plan(s->pfw);
111  fftw_destroy_plan(s->pbw);
112  fftw_free(s->specdata);
113  AUBIO_FREE(s->out);
114  AUBIO_FREE(s->in );
115  AUBIO_FREE(s);
116}
117
118void aubio_fft_do(aubio_fft_t * s, fvec_t * input, cvec_t * spectrum) {
119  aubio_fft_do_complex(s, input, s->compspec);
120  aubio_fft_get_spectrum(s->compspec, spectrum);
121}
122
123void aubio_fft_rdo(aubio_fft_t * s, cvec_t * spectrum, fvec_t * output) {
124  aubio_fft_get_realimag(spectrum, s->compspec);
125  aubio_fft_rdo_complex(s, s->compspec, output);
126}
127
128void aubio_fft_do_complex(aubio_fft_t * s, fvec_t * input, fvec_t * compspec) {
129  uint_t i, j;
130  for (i = 0; i < s->channels; i++) {
131    for (j=0; j < s->winsize; j++) {
132      s->in[j] = input->data[i][j];
133    }
134    fftw_execute(s->pfw);
135#ifdef HAVE_COMPLEX_H
136    compspec->data[i][0] = REAL(s->specdata[0]);
137    for (j = 1; j < s->fft_size -1 ; j++) {
138      compspec->data[i][j] = REAL(s->specdata[j]);
139      compspec->data[i][compspec->length - j] = IMAG(s->specdata[j]);
140    }
141    compspec->data[i][s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
142#else
143    for (j = 0; j < s->fft_size; j++) {
144      compspec->data[i][j] = s->specdata[j];
145    }
146#endif
147  }
148}
149
150void aubio_fft_rdo_complex(aubio_fft_t * s, fvec_t * compspec, fvec_t * output) {
151  uint_t i, j;
152  const smpl_t renorm = 1./(smpl_t)s->winsize;
153  for (i = 0; i < compspec->channels; i++) {
154#ifdef HAVE_COMPLEX_H
155    s->specdata[0] = compspec->data[i][0];
156    for (j=1; j < s->fft_size - 1; j++) {
157      s->specdata[j] = compspec->data[i][j] + 
158        I * compspec->data[i][compspec->length - j];
159    }
160    s->specdata[s->fft_size - 1] = compspec->data[i][s->fft_size - 1];
161#else
162    for (j=0; j < s->fft_size; j++) {
163      s->specdata[j] = compspec->data[i][j];
164    }
165#endif
166    fftw_execute(s->pbw);
167    for (j = 0; j < output->length; j++) {
168      output->data[i][j] = s->out[j]*renorm;
169    }
170  }
171}
172
173void aubio_fft_get_spectrum(fvec_t * compspec, cvec_t * spectrum) {
174  aubio_fft_get_phas(compspec, spectrum);
175  aubio_fft_get_norm(compspec, spectrum);
176}
177
178void aubio_fft_get_realimag(cvec_t * spectrum, fvec_t * compspec) {
179  aubio_fft_get_imag(spectrum, compspec);
180  aubio_fft_get_real(spectrum, compspec);
181}
182
183void aubio_fft_get_phas(fvec_t * compspec, cvec_t * spectrum) {
184  uint_t i, j;
185  for (i = 0; i < spectrum->channels; i++) {
186    spectrum->phas[i][0] = 0.;
187    for (j=1; j < spectrum->length - 1; j++) {
188      if (compspec->data[i][j] == 0.) spectrum->phas[i][j] = 0;
189      else
190      spectrum->phas[i][j] = atan2f(compspec->data[i][compspec->length-j],
191          compspec->data[i][j]);
192    }
193    spectrum->phas[i][spectrum->length-1] = 0.;
194  }
195}
196
197void aubio_fft_get_norm(fvec_t * compspec, cvec_t * spectrum) {
198  uint_t i, j = 0;
199  for (i = 0; i < spectrum->channels; i++) {
200    spectrum->norm[i][0] = ABS(compspec->data[i][0]);
201    for (j=1; j < spectrum->length - 1; j++) {
202      spectrum->norm[i][j] = SQRT(SQR(compspec->data[i][j]) 
203          + SQR(compspec->data[i][compspec->length - j]) );
204    }
205    spectrum->norm[i][spectrum->length-1] = 
206      ABS(compspec->data[i][compspec->length/2]);
207  }
208}
209
210void aubio_fft_get_imag(cvec_t * spectrum, fvec_t * compspec) {
211  uint_t i, j;
212  for (i = 0; i < compspec->channels; i++) {
213    for (j = 1; j < compspec->length / 2 + 1; j++) {
214      compspec->data[i][compspec->length - j] =
215        spectrum->norm[i][j]*SIN(spectrum->phas[i][j]);
216    }
217  }
218}
219
220void aubio_fft_get_real(cvec_t * spectrum, fvec_t * compspec) {
221  uint_t i, j;
222  for (i = 0; i < compspec->channels; i++) {
223    for (j = 0; j< compspec->length / 2 + 1; j++) {
224      compspec->data[i][j] = 
225        spectrum->norm[i][j]*COS(spectrum->phas[i][j]);
226    }
227  }
228}
Note: See TracBrowser for help on using the repository browser.