[96fb8ad] | 1 | /* |
---|
[e6a78ea] | 2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[e6a78ea] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[e6a78ea] | 6 | aubio is free software: you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | (at your option) any later version. |
---|
[96fb8ad] | 10 | |
---|
[e6a78ea] | 11 | aubio is distributed in the hope that it will be useful, |
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | GNU General Public License for more details. |
---|
| 15 | |
---|
| 16 | You should have received a copy of the GNU General Public License |
---|
| 17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
[96fb8ad] | 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
[aadd27a] | 22 | #include "fvec.h" |
---|
| 23 | #include "cvec.h" |
---|
[96fb8ad] | 24 | #include "mathutils.h" |
---|
[32d6958] | 25 | #include "spectral/fft.h" |
---|
[96fb8ad] | 26 | |
---|
[f3bee79] | 27 | /* note that <complex.h> is not included here but only in aubio_priv.h, so that |
---|
| 28 | * c++ projects can still use their own complex definition. */ |
---|
| 29 | #include <fftw3.h> |
---|
| 30 | |
---|
| 31 | #ifdef HAVE_COMPLEX_H |
---|
| 32 | #if HAVE_FFTW3F |
---|
| 33 | /** fft data type with complex.h and fftw3f */ |
---|
| 34 | #define FFTW_TYPE fftwf_complex |
---|
| 35 | #else |
---|
| 36 | /** fft data type with complex.h and fftw3 */ |
---|
| 37 | #define FFTW_TYPE fftw_complex |
---|
| 38 | #endif |
---|
| 39 | #else |
---|
| 40 | #if HAVE_FFTW3F |
---|
| 41 | /** fft data type without complex.h and with fftw3f */ |
---|
| 42 | #define FFTW_TYPE float |
---|
| 43 | #else |
---|
| 44 | /** fft data type without complex.h and with fftw */ |
---|
| 45 | #define FFTW_TYPE double |
---|
| 46 | #endif |
---|
| 47 | #endif |
---|
| 48 | |
---|
| 49 | /** fft data type */ |
---|
| 50 | typedef FFTW_TYPE fft_data_t; |
---|
| 51 | |
---|
[b511fa9] | 52 | #if HAVE_FFTW3F |
---|
[a47cd35] | 53 | #define fftw_malloc fftwf_malloc |
---|
| 54 | #define fftw_free fftwf_free |
---|
| 55 | #define fftw_execute fftwf_execute |
---|
| 56 | #define fftw_plan_dft_r2c_1d fftwf_plan_dft_r2c_1d |
---|
| 57 | #define fftw_plan_dft_c2r_1d fftwf_plan_dft_c2r_1d |
---|
| 58 | #define fftw_plan_r2r_1d fftwf_plan_r2r_1d |
---|
| 59 | #define fftw_plan fftwf_plan |
---|
| 60 | #define fftw_destroy_plan fftwf_destroy_plan |
---|
[96fb8ad] | 61 | #endif |
---|
| 62 | |
---|
[b511fa9] | 63 | #if HAVE_FFTW3F |
---|
[6f0b8a0] | 64 | #if HAVE_AUBIO_DOUBLE |
---|
[4369cb9] | 65 | #warning "Using aubio in double precision with fftw3 in single precision" |
---|
[fd6b90f] | 66 | #endif /* HAVE_AUBIO_DOUBLE */ |
---|
[4369cb9] | 67 | #define real_t float |
---|
[fd6b90f] | 68 | #else /* HAVE_FFTW3F */ |
---|
| 69 | #if !HAVE_AUBIO_DOUBLE |
---|
| 70 | #warning "Using aubio in single precision with fftw3 in double precision" |
---|
| 71 | #endif /* HAVE_AUBIO_DOUBLE */ |
---|
[4369cb9] | 72 | #define real_t double |
---|
[fd6b90f] | 73 | #endif /* HAVE_FFTW3F */ |
---|
[96fb8ad] | 74 | |
---|
| 75 | struct _aubio_fft_t { |
---|
[aadd27a] | 76 | uint_t winsize; |
---|
[a47cd35] | 77 | uint_t channels; |
---|
[aadd27a] | 78 | uint_t fft_size; |
---|
| 79 | real_t *in, *out; |
---|
[4b6937b] | 80 | fftw_plan pfw, pbw; |
---|
[aadd27a] | 81 | fft_data_t * specdata; /* complex spectral data */ |
---|
| 82 | fvec_t * compspec; |
---|
[96fb8ad] | 83 | }; |
---|
| 84 | |
---|
[aadd27a] | 85 | aubio_fft_t * new_aubio_fft(uint_t winsize, uint_t channels) { |
---|
[a47cd35] | 86 | aubio_fft_t * s = AUBIO_NEW(aubio_fft_t); |
---|
[4f4299d] | 87 | uint_t i; |
---|
[aadd27a] | 88 | s->winsize = winsize; |
---|
| 89 | s->channels = channels; |
---|
[a47cd35] | 90 | /* allocate memory */ |
---|
[aadd27a] | 91 | s->in = AUBIO_ARRAY(real_t,winsize); |
---|
| 92 | s->out = AUBIO_ARRAY(real_t,winsize); |
---|
| 93 | s->compspec = new_fvec(winsize,channels); |
---|
[a47cd35] | 94 | /* create plans */ |
---|
[237f632] | 95 | #ifdef HAVE_COMPLEX_H |
---|
[4b6937b] | 96 | s->fft_size = winsize/2 + 1; |
---|
[a47cd35] | 97 | s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size); |
---|
[aadd27a] | 98 | s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in, s->specdata, FFTW_ESTIMATE); |
---|
| 99 | s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE); |
---|
[237f632] | 100 | #else |
---|
[aadd27a] | 101 | s->fft_size = winsize; |
---|
[a47cd35] | 102 | s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size); |
---|
[aadd27a] | 103 | s->pfw = fftw_plan_r2r_1d(winsize, s->in, s->specdata, FFTW_R2HC, FFTW_ESTIMATE); |
---|
| 104 | s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE); |
---|
[237f632] | 105 | #endif |
---|
[4f4299d] | 106 | for (i = 0; i < s->winsize; i++) { |
---|
| 107 | s->in[i] = 0.; |
---|
| 108 | s->out[i] = 0.; |
---|
| 109 | } |
---|
| 110 | for (i = 0; i < s->fft_size; i++) { |
---|
| 111 | s->specdata[i] = 0.; |
---|
| 112 | } |
---|
[a47cd35] | 113 | return s; |
---|
[96fb8ad] | 114 | } |
---|
| 115 | |
---|
| 116 | void del_aubio_fft(aubio_fft_t * s) { |
---|
[a47cd35] | 117 | /* destroy data */ |
---|
[aadd27a] | 118 | del_fvec(s->compspec); |
---|
[a47cd35] | 119 | fftw_destroy_plan(s->pfw); |
---|
| 120 | fftw_destroy_plan(s->pbw); |
---|
| 121 | fftw_free(s->specdata); |
---|
| 122 | AUBIO_FREE(s->out); |
---|
| 123 | AUBIO_FREE(s->in ); |
---|
| 124 | AUBIO_FREE(s); |
---|
[96fb8ad] | 125 | } |
---|
| 126 | |
---|
[aadd27a] | 127 | void aubio_fft_do(aubio_fft_t * s, fvec_t * input, cvec_t * spectrum) { |
---|
| 128 | aubio_fft_do_complex(s, input, s->compspec); |
---|
| 129 | aubio_fft_get_spectrum(s->compspec, spectrum); |
---|
[96fb8ad] | 130 | } |
---|
| 131 | |
---|
[aadd27a] | 132 | void aubio_fft_rdo(aubio_fft_t * s, cvec_t * spectrum, fvec_t * output) { |
---|
| 133 | aubio_fft_get_realimag(spectrum, s->compspec); |
---|
| 134 | aubio_fft_rdo_complex(s, s->compspec, output); |
---|
[96fb8ad] | 135 | } |
---|
| 136 | |
---|
[aadd27a] | 137 | void aubio_fft_do_complex(aubio_fft_t * s, fvec_t * input, fvec_t * compspec) { |
---|
| 138 | uint_t i, j; |
---|
| 139 | for (i = 0; i < s->channels; i++) { |
---|
| 140 | for (j=0; j < s->winsize; j++) { |
---|
| 141 | s->in[j] = input->data[i][j]; |
---|
| 142 | } |
---|
| 143 | fftw_execute(s->pfw); |
---|
[4b6937b] | 144 | #ifdef HAVE_COMPLEX_H |
---|
[aadd27a] | 145 | compspec->data[i][0] = REAL(s->specdata[0]); |
---|
| 146 | for (j = 1; j < s->fft_size -1 ; j++) { |
---|
| 147 | compspec->data[i][j] = REAL(s->specdata[j]); |
---|
| 148 | compspec->data[i][compspec->length - j] = IMAG(s->specdata[j]); |
---|
| 149 | } |
---|
| 150 | compspec->data[i][s->fft_size-1] = REAL(s->specdata[s->fft_size-1]); |
---|
| 151 | #else |
---|
| 152 | for (j = 0; j < s->fft_size; j++) { |
---|
| 153 | compspec->data[i][j] = s->specdata[j]; |
---|
| 154 | } |
---|
| 155 | #endif |
---|
[237f632] | 156 | } |
---|
[96fb8ad] | 157 | } |
---|
| 158 | |
---|
[aadd27a] | 159 | void aubio_fft_rdo_complex(aubio_fft_t * s, fvec_t * compspec, fvec_t * output) { |
---|
| 160 | uint_t i, j; |
---|
| 161 | const smpl_t renorm = 1./(smpl_t)s->winsize; |
---|
| 162 | for (i = 0; i < compspec->channels; i++) { |
---|
[4b6937b] | 163 | #ifdef HAVE_COMPLEX_H |
---|
[aadd27a] | 164 | s->specdata[0] = compspec->data[i][0]; |
---|
| 165 | for (j=1; j < s->fft_size - 1; j++) { |
---|
| 166 | s->specdata[j] = compspec->data[i][j] + |
---|
| 167 | I * compspec->data[i][compspec->length - j]; |
---|
| 168 | } |
---|
| 169 | s->specdata[s->fft_size - 1] = compspec->data[i][s->fft_size - 1]; |
---|
[237f632] | 170 | #else |
---|
[aadd27a] | 171 | for (j=0; j < s->fft_size; j++) { |
---|
| 172 | s->specdata[j] = compspec->data[i][j]; |
---|
| 173 | } |
---|
| 174 | #endif |
---|
| 175 | fftw_execute(s->pbw); |
---|
| 176 | for (j = 0; j < output->length; j++) { |
---|
| 177 | output->data[i][j] = s->out[j]*renorm; |
---|
| 178 | } |
---|
| 179 | } |
---|
[237f632] | 180 | } |
---|
| 181 | |
---|
[aadd27a] | 182 | void aubio_fft_get_spectrum(fvec_t * compspec, cvec_t * spectrum) { |
---|
| 183 | aubio_fft_get_phas(compspec, spectrum); |
---|
| 184 | aubio_fft_get_norm(compspec, spectrum); |
---|
[237f632] | 185 | } |
---|
| 186 | |
---|
[aadd27a] | 187 | void aubio_fft_get_realimag(cvec_t * spectrum, fvec_t * compspec) { |
---|
| 188 | aubio_fft_get_imag(spectrum, compspec); |
---|
| 189 | aubio_fft_get_real(spectrum, compspec); |
---|
[237f632] | 190 | } |
---|
| 191 | |
---|
[aadd27a] | 192 | void aubio_fft_get_phas(fvec_t * compspec, cvec_t * spectrum) { |
---|
| 193 | uint_t i, j; |
---|
| 194 | for (i = 0; i < spectrum->channels; i++) { |
---|
[8979752] | 195 | if (compspec->data[i][0] < 0) { |
---|
| 196 | spectrum->phas[i][0] = PI; |
---|
| 197 | } else { |
---|
| 198 | spectrum->phas[i][0] = 0.; |
---|
| 199 | } |
---|
[aadd27a] | 200 | for (j=1; j < spectrum->length - 1; j++) { |
---|
[2fde783] | 201 | spectrum->phas[i][j] = ATAN2(compspec->data[i][compspec->length-j], |
---|
[aadd27a] | 202 | compspec->data[i][j]); |
---|
| 203 | } |
---|
[8979752] | 204 | if (compspec->data[i][compspec->length/2] < 0) { |
---|
| 205 | spectrum->phas[i][spectrum->length - 1] = PI; |
---|
| 206 | } else { |
---|
| 207 | spectrum->phas[i][spectrum->length - 1] = 0.; |
---|
| 208 | } |
---|
[aadd27a] | 209 | } |
---|
[f88a326] | 210 | } |
---|
| 211 | |
---|
[aadd27a] | 212 | void aubio_fft_get_norm(fvec_t * compspec, cvec_t * spectrum) { |
---|
| 213 | uint_t i, j = 0; |
---|
| 214 | for (i = 0; i < spectrum->channels; i++) { |
---|
[d53a3b5] | 215 | spectrum->norm[i][0] = ABS(compspec->data[i][0]); |
---|
[aadd27a] | 216 | for (j=1; j < spectrum->length - 1; j++) { |
---|
| 217 | spectrum->norm[i][j] = SQRT(SQR(compspec->data[i][j]) |
---|
| 218 | + SQR(compspec->data[i][compspec->length - j]) ); |
---|
| 219 | } |
---|
[d53a3b5] | 220 | spectrum->norm[i][spectrum->length-1] = |
---|
| 221 | ABS(compspec->data[i][compspec->length/2]); |
---|
[a47cd35] | 222 | } |
---|
[f88a326] | 223 | } |
---|
| 224 | |
---|
[aadd27a] | 225 | void aubio_fft_get_imag(cvec_t * spectrum, fvec_t * compspec) { |
---|
| 226 | uint_t i, j; |
---|
| 227 | for (i = 0; i < compspec->channels; i++) { |
---|
[74a4865] | 228 | for (j = 1; j < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; j++) { |
---|
[aadd27a] | 229 | compspec->data[i][compspec->length - j] = |
---|
| 230 | spectrum->norm[i][j]*SIN(spectrum->phas[i][j]); |
---|
| 231 | } |
---|
[a47cd35] | 232 | } |
---|
[f88a326] | 233 | } |
---|
| 234 | |
---|
[aadd27a] | 235 | void aubio_fft_get_real(cvec_t * spectrum, fvec_t * compspec) { |
---|
| 236 | uint_t i, j; |
---|
| 237 | for (i = 0; i < compspec->channels; i++) { |
---|
[74a4865] | 238 | for (j = 0; j < compspec->length / 2 + 1; j++) { |
---|
[aadd27a] | 239 | compspec->data[i][j] = |
---|
| 240 | spectrum->norm[i][j]*COS(spectrum->phas[i][j]); |
---|
| 241 | } |
---|
| 242 | } |
---|
[f88a326] | 243 | } |
---|