source: src/spectral/fft.c @ a253fd4

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

src/spectral/fft.c: add a warning if using fftw3 with aubio single

  • Property mode set to 100644
File size: 6.3 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
[b511fa9]26#if HAVE_FFTW3F
[a47cd35]27#define fftw_malloc            fftwf_malloc
28#define fftw_free              fftwf_free
29#define fftw_execute           fftwf_execute
30#define fftw_plan_dft_r2c_1d   fftwf_plan_dft_r2c_1d
31#define fftw_plan_dft_c2r_1d   fftwf_plan_dft_c2r_1d
32#define fftw_plan_r2r_1d       fftwf_plan_r2r_1d
33#define fftw_plan              fftwf_plan
34#define fftw_destroy_plan      fftwf_destroy_plan
[96fb8ad]35#endif
36
[b511fa9]37#if HAVE_FFTW3F
[6f0b8a0]38#if HAVE_AUBIO_DOUBLE
[4369cb9]39#warning "Using aubio in double precision with fftw3 in single precision"
[fd6b90f]40#endif /* HAVE_AUBIO_DOUBLE */
[4369cb9]41#define real_t float
[fd6b90f]42#else /* HAVE_FFTW3F */
43#if !HAVE_AUBIO_DOUBLE
44#warning "Using aubio in single precision with fftw3 in double precision"
45#endif /* HAVE_AUBIO_DOUBLE */
[4369cb9]46#define real_t double
[fd6b90f]47#endif /* HAVE_FFTW3F */
[96fb8ad]48
49struct _aubio_fft_t {
[aadd27a]50  uint_t winsize;
[a47cd35]51  uint_t channels;
[aadd27a]52  uint_t fft_size;
53  real_t *in, *out;
[4b6937b]54  fftw_plan pfw, pbw;
[aadd27a]55  fft_data_t * specdata;     /* complex spectral data */
56  fvec_t * compspec;
[96fb8ad]57};
58
[aadd27a]59aubio_fft_t * new_aubio_fft(uint_t winsize, uint_t channels) {
[a47cd35]60  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
[aadd27a]61  s->winsize  = winsize;
62  s->channels = channels;
[a47cd35]63  /* allocate memory */
[aadd27a]64  s->in       = AUBIO_ARRAY(real_t,winsize);
65  s->out      = AUBIO_ARRAY(real_t,winsize);
66  s->compspec = new_fvec(winsize,channels);
[a47cd35]67  /* create plans */
[237f632]68#ifdef HAVE_COMPLEX_H
[4b6937b]69  s->fft_size = winsize/2 + 1;
[a47cd35]70  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]71  s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in,  s->specdata, FFTW_ESTIMATE);
72  s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE);
[237f632]73#else
[aadd27a]74  s->fft_size = winsize;
[a47cd35]75  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]76  s->pfw = fftw_plan_r2r_1d(winsize, s->in,  s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
77  s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
[237f632]78#endif
[a47cd35]79  return s;
[96fb8ad]80}
81
82void del_aubio_fft(aubio_fft_t * s) {
[a47cd35]83  /* destroy data */
[aadd27a]84  del_fvec(s->compspec);
[a47cd35]85  fftw_destroy_plan(s->pfw);
86  fftw_destroy_plan(s->pbw);
87  fftw_free(s->specdata);
88  AUBIO_FREE(s->out);
89  AUBIO_FREE(s->in );
90  AUBIO_FREE(s);
[96fb8ad]91}
92
[aadd27a]93void aubio_fft_do(aubio_fft_t * s, fvec_t * input, cvec_t * spectrum) {
94  aubio_fft_do_complex(s, input, s->compspec);
95  aubio_fft_get_spectrum(s->compspec, spectrum);
[96fb8ad]96}
97
[aadd27a]98void aubio_fft_rdo(aubio_fft_t * s, cvec_t * spectrum, fvec_t * output) {
99  aubio_fft_get_realimag(spectrum, s->compspec);
100  aubio_fft_rdo_complex(s, s->compspec, output);
[96fb8ad]101}
102
[aadd27a]103void aubio_fft_do_complex(aubio_fft_t * s, fvec_t * input, fvec_t * compspec) {
104  uint_t i, j;
105  for (i = 0; i < s->channels; i++) {
106    for (j=0; j < s->winsize; j++) {
107      s->in[j] = input->data[i][j];
108    }
109    fftw_execute(s->pfw);
[4b6937b]110#ifdef HAVE_COMPLEX_H
[aadd27a]111    compspec->data[i][0] = REAL(s->specdata[0]);
112    for (j = 1; j < s->fft_size -1 ; j++) {
113      compspec->data[i][j] = REAL(s->specdata[j]);
114      compspec->data[i][compspec->length - j] = IMAG(s->specdata[j]);
115    }
116    compspec->data[i][s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
117#else
118    for (j = 0; j < s->fft_size; j++) {
119      compspec->data[i][j] = s->specdata[j];
120    }
121#endif
[237f632]122  }
[96fb8ad]123}
124
[aadd27a]125void aubio_fft_rdo_complex(aubio_fft_t * s, fvec_t * compspec, fvec_t * output) {
126  uint_t i, j;
127  const smpl_t renorm = 1./(smpl_t)s->winsize;
128  for (i = 0; i < compspec->channels; i++) {
[4b6937b]129#ifdef HAVE_COMPLEX_H
[aadd27a]130    s->specdata[0] = compspec->data[i][0];
131    for (j=1; j < s->fft_size - 1; j++) {
132      s->specdata[j] = compspec->data[i][j] + 
133        I * compspec->data[i][compspec->length - j];
134    }
135    s->specdata[s->fft_size - 1] = compspec->data[i][s->fft_size - 1];
[237f632]136#else
[aadd27a]137    for (j=0; j < s->fft_size; j++) {
138      s->specdata[j] = compspec->data[i][j];
139    }
140#endif
141    fftw_execute(s->pbw);
142    for (j = 0; j < output->length; j++) {
143      output->data[i][j] = s->out[j]*renorm;
144    }
145  }
[237f632]146}
147
[aadd27a]148void aubio_fft_get_spectrum(fvec_t * compspec, cvec_t * spectrum) {
149  aubio_fft_get_phas(compspec, spectrum);
150  aubio_fft_get_norm(compspec, spectrum);
[237f632]151}
152
[aadd27a]153void aubio_fft_get_realimag(cvec_t * spectrum, fvec_t * compspec) {
154  aubio_fft_get_imag(spectrum, compspec);
155  aubio_fft_get_real(spectrum, compspec);
[237f632]156}
157
[aadd27a]158void aubio_fft_get_phas(fvec_t * compspec, cvec_t * spectrum) {
159  uint_t i, j;
160  for (i = 0; i < spectrum->channels; i++) {
161    spectrum->phas[i][0] = 0.;
162    for (j=1; j < spectrum->length - 1; j++) {
[41ed384]163      if (compspec->data[i][j] == 0.) spectrum->phas[i][j] = 0;
164      else
[aadd27a]165      spectrum->phas[i][j] = atan2f(compspec->data[i][compspec->length-j],
166          compspec->data[i][j]);
167    }
168    spectrum->phas[i][spectrum->length-1] = 0.;
169  }
[f88a326]170}
171
[aadd27a]172void aubio_fft_get_norm(fvec_t * compspec, cvec_t * spectrum) {
173  uint_t i, j = 0;
174  for (i = 0; i < spectrum->channels; i++) {
[d53a3b5]175    spectrum->norm[i][0] = ABS(compspec->data[i][0]);
[aadd27a]176    for (j=1; j < spectrum->length - 1; j++) {
177      spectrum->norm[i][j] = SQRT(SQR(compspec->data[i][j]) 
178          + SQR(compspec->data[i][compspec->length - j]) );
179    }
[d53a3b5]180    spectrum->norm[i][spectrum->length-1] = 
181      ABS(compspec->data[i][compspec->length/2]);
[a47cd35]182  }
[f88a326]183}
184
[aadd27a]185void aubio_fft_get_imag(cvec_t * spectrum, fvec_t * compspec) {
186  uint_t i, j;
187  for (i = 0; i < compspec->channels; i++) {
188    for (j = 1; j < compspec->length / 2 + 1; j++) {
189      compspec->data[i][compspec->length - j] =
190        spectrum->norm[i][j]*SIN(spectrum->phas[i][j]);
191    }
[a47cd35]192  }
[f88a326]193}
194
[aadd27a]195void aubio_fft_get_real(cvec_t * spectrum, fvec_t * compspec) {
196  uint_t i, j;
197  for (i = 0; i < compspec->channels; i++) {
198    for (j = 0; j< compspec->length / 2 + 1; j++) {
199      compspec->data[i][j] = 
200        spectrum->norm[i][j]*COS(spectrum->phas[i][j]);
201    }
202  }
[f88a326]203}
Note: See TracBrowser for help on using the repository browser.