source: src/spectral/fft.c @ cd77c15

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

src/spectral/fft.c: fix imag boundaries, including for odd fft sizes

  • Property mode set to 100644
File size: 7.2 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);
[4f4299d]86  uint_t i;
[aadd27a]87  s->winsize  = winsize;
88  s->channels = channels;
[a47cd35]89  /* allocate memory */
[aadd27a]90  s->in       = AUBIO_ARRAY(real_t,winsize);
91  s->out      = AUBIO_ARRAY(real_t,winsize);
92  s->compspec = new_fvec(winsize,channels);
[a47cd35]93  /* create plans */
[237f632]94#ifdef HAVE_COMPLEX_H
[4b6937b]95  s->fft_size = winsize/2 + 1;
[a47cd35]96  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]97  s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in,  s->specdata, FFTW_ESTIMATE);
98  s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE);
[237f632]99#else
[aadd27a]100  s->fft_size = winsize;
[a47cd35]101  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
[aadd27a]102  s->pfw = fftw_plan_r2r_1d(winsize, s->in,  s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
103  s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
[237f632]104#endif
[4f4299d]105  for (i = 0; i < s->winsize; i++) {
106    s->in[i] = 0.;
107    s->out[i] = 0.;
108  }
109  for (i = 0; i < s->fft_size; i++) {
110    s->specdata[i] = 0.;
111  }
[a47cd35]112  return s;
[96fb8ad]113}
114
115void del_aubio_fft(aubio_fft_t * s) {
[a47cd35]116  /* destroy data */
[aadd27a]117  del_fvec(s->compspec);
[a47cd35]118  fftw_destroy_plan(s->pfw);
119  fftw_destroy_plan(s->pbw);
120  fftw_free(s->specdata);
121  AUBIO_FREE(s->out);
122  AUBIO_FREE(s->in );
123  AUBIO_FREE(s);
[96fb8ad]124}
125
[aadd27a]126void aubio_fft_do(aubio_fft_t * s, fvec_t * input, cvec_t * spectrum) {
127  aubio_fft_do_complex(s, input, s->compspec);
128  aubio_fft_get_spectrum(s->compspec, spectrum);
[96fb8ad]129}
130
[aadd27a]131void aubio_fft_rdo(aubio_fft_t * s, cvec_t * spectrum, fvec_t * output) {
132  aubio_fft_get_realimag(spectrum, s->compspec);
133  aubio_fft_rdo_complex(s, s->compspec, output);
[96fb8ad]134}
135
[aadd27a]136void aubio_fft_do_complex(aubio_fft_t * s, fvec_t * input, fvec_t * compspec) {
137  uint_t i, j;
138  for (i = 0; i < s->channels; i++) {
139    for (j=0; j < s->winsize; j++) {
140      s->in[j] = input->data[i][j];
141    }
142    fftw_execute(s->pfw);
[4b6937b]143#ifdef HAVE_COMPLEX_H
[aadd27a]144    compspec->data[i][0] = REAL(s->specdata[0]);
145    for (j = 1; j < s->fft_size -1 ; j++) {
146      compspec->data[i][j] = REAL(s->specdata[j]);
147      compspec->data[i][compspec->length - j] = IMAG(s->specdata[j]);
148    }
149    compspec->data[i][s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
150#else
151    for (j = 0; j < s->fft_size; j++) {
152      compspec->data[i][j] = s->specdata[j];
153    }
154#endif
[237f632]155  }
[96fb8ad]156}
157
[aadd27a]158void aubio_fft_rdo_complex(aubio_fft_t * s, fvec_t * compspec, fvec_t * output) {
159  uint_t i, j;
160  const smpl_t renorm = 1./(smpl_t)s->winsize;
161  for (i = 0; i < compspec->channels; i++) {
[4b6937b]162#ifdef HAVE_COMPLEX_H
[aadd27a]163    s->specdata[0] = compspec->data[i][0];
164    for (j=1; j < s->fft_size - 1; j++) {
165      s->specdata[j] = compspec->data[i][j] + 
166        I * compspec->data[i][compspec->length - j];
167    }
168    s->specdata[s->fft_size - 1] = compspec->data[i][s->fft_size - 1];
[237f632]169#else
[aadd27a]170    for (j=0; j < s->fft_size; j++) {
171      s->specdata[j] = compspec->data[i][j];
172    }
173#endif
174    fftw_execute(s->pbw);
175    for (j = 0; j < output->length; j++) {
176      output->data[i][j] = s->out[j]*renorm;
177    }
178  }
[237f632]179}
180
[aadd27a]181void aubio_fft_get_spectrum(fvec_t * compspec, cvec_t * spectrum) {
182  aubio_fft_get_phas(compspec, spectrum);
183  aubio_fft_get_norm(compspec, spectrum);
[237f632]184}
185
[aadd27a]186void aubio_fft_get_realimag(cvec_t * spectrum, fvec_t * compspec) {
187  aubio_fft_get_imag(spectrum, compspec);
188  aubio_fft_get_real(spectrum, compspec);
[237f632]189}
190
[aadd27a]191void aubio_fft_get_phas(fvec_t * compspec, cvec_t * spectrum) {
192  uint_t i, j;
193  for (i = 0; i < spectrum->channels; i++) {
[8979752]194    if (compspec->data[i][0] < 0) {
195      spectrum->phas[i][0] = PI;
196    } else {
197      spectrum->phas[i][0] = 0.;
198    }
[aadd27a]199    for (j=1; j < spectrum->length - 1; j++) {
[2fde783]200      spectrum->phas[i][j] = ATAN2(compspec->data[i][compspec->length-j],
[aadd27a]201          compspec->data[i][j]);
202    }
[8979752]203    if (compspec->data[i][compspec->length/2] < 0) {
204      spectrum->phas[i][spectrum->length - 1] = PI;
205    } else {
206      spectrum->phas[i][spectrum->length - 1] = 0.;
207    }
[aadd27a]208  }
[f88a326]209}
210
[aadd27a]211void aubio_fft_get_norm(fvec_t * compspec, cvec_t * spectrum) {
212  uint_t i, j = 0;
213  for (i = 0; i < spectrum->channels; i++) {
[d53a3b5]214    spectrum->norm[i][0] = ABS(compspec->data[i][0]);
[aadd27a]215    for (j=1; j < spectrum->length - 1; j++) {
216      spectrum->norm[i][j] = SQRT(SQR(compspec->data[i][j]) 
217          + SQR(compspec->data[i][compspec->length - j]) );
218    }
[d53a3b5]219    spectrum->norm[i][spectrum->length-1] = 
220      ABS(compspec->data[i][compspec->length/2]);
[a47cd35]221  }
[f88a326]222}
223
[aadd27a]224void aubio_fft_get_imag(cvec_t * spectrum, fvec_t * compspec) {
225  uint_t i, j;
226  for (i = 0; i < compspec->channels; i++) {
[74a4865]227    for (j = 1; j < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; j++) {
[aadd27a]228      compspec->data[i][compspec->length - j] =
229        spectrum->norm[i][j]*SIN(spectrum->phas[i][j]);
230    }
[a47cd35]231  }
[f88a326]232}
233
[aadd27a]234void aubio_fft_get_real(cvec_t * spectrum, fvec_t * compspec) {
235  uint_t i, j;
236  for (i = 0; i < compspec->channels; i++) {
[74a4865]237    for (j = 0; j < compspec->length / 2 + 1; j++) {
[aadd27a]238      compspec->data[i][j] = 
239        spectrum->norm[i][j]*COS(spectrum->phas[i][j]);
240    }
241  }
[f88a326]242}
Note: See TracBrowser for help on using the repository browser.