2 Copyright (C) 2003 Paul Brossier
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.
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.
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.
20 #include "aubio_priv.h"
22 #include "mathutils.h"
26 #define fftw_malloc fftwf_malloc
27 #define fftw_free fftwf_free
28 #define fftw_execute fftwf_execute
29 #define fftw_plan_dft_r2c_1d fftwf_plan_dft_r2c_1d
30 #define fftw_plan_dft_c2r_1d fftwf_plan_dft_c2r_1d
31 #define fftw_plan fftwf_plan
32 #define fftw_destroy_plan fftwf_destroy_plan
49 aubio_fft_t * new_aubio_fft(uint_t size) {
50 aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
52 s->in = AUBIO_ARRAY(real_t,size);
53 s->out = AUBIO_ARRAY(real_t,size);
54 s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*size);
56 s->pfw = fftw_plan_dft_r2c_1d(size, s->in, s->specdata, FFTW_ESTIMATE);
57 s->pbw = fftw_plan_dft_c2r_1d(size, s->specdata, s->out, FFTW_ESTIMATE);
61 void del_aubio_fft(aubio_fft_t * s) {
63 fftw_destroy_plan(s->pfw);
64 fftw_destroy_plan(s->pbw);
65 fftw_free(s->specdata);
71 void aubio_fft_do(const aubio_fft_t * s,
72 const smpl_t * data, fft_data_t * spectrum,
75 for (i=0;i<size;i++) s->in[i] = data[i];
77 for (i=0;i<size;i++) spectrum[i] = s->specdata[i];
80 void aubio_fft_rdo(const aubio_fft_t * s,
81 const fft_data_t * spectrum,
85 const smpl_t renorm = 1./(smpl_t)size;
86 for (i=0;i<size;i++) s->specdata[i] = spectrum[i];
88 for (i=0;i<size;i++) data[i] = s->out[i]*renorm;
92 void aubio_fft_getnorm(smpl_t * norm, fft_data_t * spectrum, uint_t size) {
94 for (i=0;i<size;i++) norm[i] = ABSC(spectrum[i]);
97 void aubio_fft_getphas(smpl_t * phas, fft_data_t * spectrum, uint_t size) {
99 for (i=0;i<size;i++) phas[i] = ARGC(spectrum[i]);
103 /* new interface aubio_mfft */
104 struct _aubio_mfft_t {
105 aubio_fft_t * fft; /* fftw interface */
106 fft_data_t ** spec; /* complex spectral data */
111 aubio_mfft_t * new_aubio_mfft(uint_t winsize, uint_t channels){
113 aubio_mfft_t * fft = AUBIO_NEW(aubio_mfft_t);
114 fft->winsize = winsize;
115 fft->channels = channels;
116 fft->fft = new_aubio_fft(winsize);
117 fft->spec = AUBIO_ARRAY(fft_data_t*,channels);
118 for (i=0; i < channels; i++)
119 fft->spec[i] = AUBIO_ARRAY(fft_data_t,winsize);
124 void aubio_mfft_do (aubio_mfft_t * fft,fvec_t * in,cvec_t * fftgrain){
127 for (i=0; i < fft->channels; i++) {
128 aubio_fft_do (fft->fft,in->data[i],fft->spec[i],fft->winsize);
129 /* put norm and phase into fftgrain */
130 aubio_fft_getnorm(fftgrain->norm[i], fft->spec[i], fft->winsize/2+1);
131 aubio_fft_getphas(fftgrain->phas[i], fft->spec[i], fft->winsize/2+1);
135 /* execute inverse fourier transform */
136 void aubio_mfft_rdo(aubio_mfft_t * fft,cvec_t * fftgrain, fvec_t * out){
138 for (i=0; i < fft->channels; i++) {
139 for (j=0; j<fft->winsize/2+1; j++) {
140 fft->spec[i][j] = CEXPC(I*aubio_unwrap2pi(fftgrain->phas[i][j]));
141 fft->spec[i][j] *= fftgrain->norm[i][j];
143 aubio_fft_rdo(fft->fft,fft->spec[i],out->data[i],fft->winsize);
147 void del_aubio_mfft(aubio_mfft_t * fft) {
149 for (i=0; i < fft->channels; i++)
150 AUBIO_FREE(fft->spec[i]);
151 AUBIO_FREE(fft->spec);
152 del_aubio_fft(fft->fft);