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