source: src/spectral/fft.c @ 202ffa5

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 202ffa5 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
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  uint_t i;
87  s->winsize  = winsize;
88  s->channels = channels;
89  /* allocate memory */
90  s->in       = AUBIO_ARRAY(real_t,winsize);
91  s->out      = AUBIO_ARRAY(real_t,winsize);
92  s->compspec = new_fvec(winsize,channels);
93  /* create plans */
94#ifdef HAVE_COMPLEX_H
95  s->fft_size = winsize/2 + 1;
96  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
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);
99#else
100  s->fft_size = winsize;
101  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
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);
104#endif
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  }
112  return s;
113}
114
115void del_aubio_fft(aubio_fft_t * s) {
116  /* destroy data */
117  del_fvec(s->compspec);
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);
124}
125
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);
129}
130
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);
134}
135
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);
143#ifdef HAVE_COMPLEX_H
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
155  }
156}
157
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++) {
162#ifdef HAVE_COMPLEX_H
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];
169#else
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  }
179}
180
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);
184}
185
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);
189}
190
191void aubio_fft_get_phas(fvec_t * compspec, cvec_t * spectrum) {
192  uint_t i, j;
193  for (i = 0; i < spectrum->channels; i++) {
194    if (compspec->data[i][0] < 0) {
195      spectrum->phas[i][0] = PI;
196    } else {
197      spectrum->phas[i][0] = 0.;
198    }
199    for (j=1; j < spectrum->length - 1; j++) {
200      spectrum->phas[i][j] = ATAN2(compspec->data[i][compspec->length-j],
201          compspec->data[i][j]);
202    }
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    }
208  }
209}
210
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++) {
214    spectrum->norm[i][0] = ABS(compspec->data[i][0]);
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    }
219    spectrum->norm[i][spectrum->length-1] = 
220      ABS(compspec->data[i][compspec->length/2]);
221  }
222}
223
224void aubio_fft_get_imag(cvec_t * spectrum, fvec_t * compspec) {
225  uint_t i, j;
226  for (i = 0; i < compspec->channels; i++) {
227    for (j = 1; j < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; j++) {
228      compspec->data[i][compspec->length - j] =
229        spectrum->norm[i][j]*SIN(spectrum->phas[i][j]);
230    }
231  }
232}
233
234void aubio_fft_get_real(cvec_t * spectrum, fvec_t * compspec) {
235  uint_t i, j;
236  for (i = 0; i < compspec->channels; i++) {
237    for (j = 0; j < compspec->length / 2 + 1; j++) {
238      compspec->data[i][j] = 
239        spectrum->norm[i][j]*COS(spectrum->phas[i][j]);
240    }
241  }
242}
Note: See TracBrowser for help on using the repository browser.