[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" |
---|
| 21 | #include "sample.h" |
---|
| 22 | #include "mathutils.h" |
---|
| 23 | #include "fft.h" |
---|
| 24 | |
---|
| 25 | #if FFTW3F_SUPPORT |
---|
| 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 |
---|
| 33 | #endif |
---|
| 34 | |
---|
| 35 | #if FFTW3F_SUPPORT |
---|
| 36 | #define real_t smpl_t |
---|
[66834b6] | 37 | #else |
---|
[96fb8ad] | 38 | #define real_t lsmp_t |
---|
| 39 | #endif |
---|
| 40 | |
---|
| 41 | struct _aubio_fft_t { |
---|
| 42 | uint_t fft_size; |
---|
| 43 | uint_t channels; |
---|
| 44 | real_t *in, *out; |
---|
| 45 | fft_data_t *specdata; |
---|
| 46 | fftw_plan pfw, pbw; |
---|
| 47 | }; |
---|
| 48 | |
---|
[9935735] | 49 | aubio_fft_t * new_aubio_fft(uint_t size) { |
---|
[96fb8ad] | 50 | aubio_fft_t * s = AUBIO_NEW(aubio_fft_t); |
---|
| 51 | /* allocate memory */ |
---|
| 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); |
---|
| 55 | /* create plans */ |
---|
| 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); |
---|
| 58 | return s; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void del_aubio_fft(aubio_fft_t * s) { |
---|
[9935735] | 62 | /* destroy data */ |
---|
| 63 | fftw_destroy_plan(s->pfw); |
---|
| 64 | fftw_destroy_plan(s->pbw); |
---|
| 65 | fftw_free(s->specdata); |
---|
| 66 | AUBIO_FREE(s->out); |
---|
| 67 | AUBIO_FREE(s->in ); |
---|
| 68 | AUBIO_FREE(s); |
---|
[96fb8ad] | 69 | } |
---|
| 70 | |
---|
| 71 | void aubio_fft_do(const aubio_fft_t * s, |
---|
| 72 | const smpl_t * data, fft_data_t * spectrum, |
---|
| 73 | const uint_t size) { |
---|
| 74 | uint_t i; |
---|
| 75 | for (i=0;i<size;i++) s->in[i] = data[i]; |
---|
| 76 | fftw_execute(s->pfw); |
---|
| 77 | for (i=0;i<size;i++) spectrum[i] = s->specdata[i]; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | void aubio_fft_rdo(const aubio_fft_t * s, |
---|
| 81 | const fft_data_t * spectrum, |
---|
| 82 | smpl_t * data, |
---|
| 83 | const uint_t size) { |
---|
| 84 | uint_t i; |
---|
| 85 | const smpl_t renorm = 1./(smpl_t)size; |
---|
| 86 | for (i=0;i<size;i++) s->specdata[i] = spectrum[i]; |
---|
| 87 | fftw_execute(s->pbw); |
---|
| 88 | for (i=0;i<size;i++) data[i] = s->out[i]*renorm; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | void aubio_fft_getnorm(smpl_t * norm, fft_data_t * spectrum, uint_t size) { |
---|
| 93 | uint_t i; |
---|
| 94 | for (i=0;i<size;i++) norm[i] = ABSC(spectrum[i]); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void aubio_fft_getphas(smpl_t * phas, fft_data_t * spectrum, uint_t size) { |
---|
| 98 | uint_t i; |
---|
| 99 | for (i=0;i<size;i++) phas[i] = ARGC(spectrum[i]); |
---|
| 100 | } |
---|
| 101 | |
---|
[f88a326] | 102 | |
---|
| 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 */ |
---|
| 107 | uint_t winsize; |
---|
| 108 | uint_t channels; |
---|
| 109 | }; |
---|
| 110 | |
---|
| 111 | aubio_mfft_t * new_aubio_mfft(uint_t winsize, uint_t channels){ |
---|
| 112 | uint_t i; |
---|
| 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); |
---|
| 120 | return fft; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /* execute stft */ |
---|
| 124 | void aubio_mfft_do (aubio_mfft_t * fft,fvec_t * in,cvec_t * fftgrain){ |
---|
| 125 | uint_t i=0; |
---|
| 126 | /* execute stft */ |
---|
| 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); |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /* execute inverse fourier transform */ |
---|
| 136 | void aubio_mfft_rdo(aubio_mfft_t * fft,cvec_t * fftgrain, fvec_t * out){ |
---|
| 137 | uint_t i=0,j; |
---|
| 138 | for (i=0; i < fft->channels; i++) { |
---|
| 139 | for (j=0; j<fft->winsize/2+1; j++) { |
---|
[28d8c4a] | 140 | fft->spec[i][j] = CEXPC(I*aubio_unwrap2pi(fftgrain->phas[i][j])); |
---|
[f88a326] | 141 | fft->spec[i][j] *= fftgrain->norm[i][j]; |
---|
| 142 | } |
---|
| 143 | aubio_fft_rdo(fft->fft,fft->spec[i],out->data[i],fft->winsize); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | void del_aubio_mfft(aubio_mfft_t * fft) { |
---|
| 148 | uint_t i; |
---|
| 149 | for (i=0; i < fft->channels; i++) |
---|
| 150 | AUBIO_FREE(fft->spec[i]); |
---|
| 151 | AUBIO_FREE(fft->spec); |
---|
[9935735] | 152 | del_aubio_fft(fft->fft); |
---|
[f88a326] | 153 | AUBIO_FREE(fft); |
---|
| 154 | } |
---|