source: src/spectral/fft.c @ 06f2d78

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 06f2d78 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
RevLine 
[96fb8ad]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"
[aadd27a]21#include "fvec.h"
22#include "cvec.h"
[96fb8ad]23#include "mathutils.h"
[32d6958]24#include "spectral/fft.h"
[96fb8ad]25
[f3bee79]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
[b511fa9]51#if HAVE_FFTW3F
[a47cd35]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
[96fb8ad]60#endif
61
[b511fa9]62#if HAVE_FFTW3F
[6f0b8a0]63#if HAVE_AUBIO_DOUBLE
[4369cb9]64#warning "Using aubio in double precision with fftw3 in single precision"
[fd6b90f]65#endif /* HAVE_AUBIO_DOUBLE */
[4369cb9]66#define real_t float
[fd6b90f]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 */
[4369cb9]71#define real_t double
[fd6b90f]72#endif /* HAVE_FFTW3F */
[96fb8ad]73
74struct _aubio_fft_t {
[aadd27a]75  uint_t winsize;
[a47cd35]76  uint_t channels;
[aadd27a]77  uint_t fft_size;
78  real_t *in, *out;
[4b6937b]79  fftw_plan pfw, pbw;
[aadd27a]80  fft_data_t * specdata;     /* complex spectral data */
81  fvec_t * compspec;
[96fb8ad]82};
83
[aadd27a]84aubio_fft_t * new_aubio_fft(uint_t winsize, uint_t channels) {
[a47cd35]85  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
[aadd27a]86  s->winsize  = winsize;
87  s->channels = channels;
[a47cd35]88  /* allocate memory */
[aadd27a]89  s->in       = AUBIO_ARRAY(real_t,winsize);
90  s->out      = AUBIO_ARRAY(real_t,winsize);
91  s->compspec = new_fvec(winsize,channels);
[a47cd35]92  /* create plans */
[237f632]93#ifdef HAVE_COMPLEX_H
[4b6937b]94  s->fft_size = winsize/2 + 1;
[a47cd35]95  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]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);
[237f632]98#else
[aadd27a]99  s->fft_size = winsize;
[a47cd35]100  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]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);
[237f632]103#endif
[a47cd35]104  return s;
[96fb8ad]105}
106
107void del_aubio_fft(aubio_fft_t * s) {
[a47cd35]108  /* destroy data */
[aadd27a]109  del_fvec(s->compspec);
[a47cd35]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);
[96fb8ad]116}
117
[aadd27a]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);
[96fb8ad]121}
122
[aadd27a]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);
[96fb8ad]126}
127
[aadd27a]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);
[4b6937b]135#ifdef HAVE_COMPLEX_H
[aadd27a]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
[237f632]147  }
[96fb8ad]148}
149
[aadd27a]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++) {
[4b6937b]154#ifdef HAVE_COMPLEX_H
[aadd27a]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];
[237f632]161#else
[aadd27a]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  }
[237f632]171}
172
[aadd27a]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);
[237f632]176}
177
[aadd27a]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);
[237f632]181}
182
[aadd27a]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++) {
[41ed384]188      if (compspec->data[i][j] == 0.) spectrum->phas[i][j] = 0;
189      else
[aadd27a]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  }
[f88a326]195}
196
[aadd27a]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++) {
[d53a3b5]200    spectrum->norm[i][0] = ABS(compspec->data[i][0]);
[aadd27a]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    }
[d53a3b5]205    spectrum->norm[i][spectrum->length-1] = 
206      ABS(compspec->data[i][compspec->length/2]);
[a47cd35]207  }
[f88a326]208}
209
[aadd27a]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    }
[a47cd35]217  }
[f88a326]218}
219
[aadd27a]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  }
[f88a326]228}
Note: See TracBrowser for help on using the repository browser.