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