[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 | |
---|
[54dd945] | 27 | #ifdef HAVE_FFTW3 // using FFTW3 |
---|
[f3bee79] | 28 | /* note that <complex.h> is not included here but only in aubio_priv.h, so that |
---|
| 29 | * c++ projects can still use their own complex definition. */ |
---|
| 30 | #include <fftw3.h> |
---|
[d453a4a] | 31 | #include <pthread.h> |
---|
[f3bee79] | 32 | |
---|
| 33 | #ifdef HAVE_COMPLEX_H |
---|
[729a3c0] | 34 | #ifdef HAVE_FFTW3F |
---|
[f3bee79] | 35 | /** fft data type with complex.h and fftw3f */ |
---|
| 36 | #define FFTW_TYPE fftwf_complex |
---|
| 37 | #else |
---|
| 38 | /** fft data type with complex.h and fftw3 */ |
---|
| 39 | #define FFTW_TYPE fftw_complex |
---|
| 40 | #endif |
---|
| 41 | #else |
---|
[729a3c0] | 42 | #ifdef HAVE_FFTW3F |
---|
[f3bee79] | 43 | /** fft data type without complex.h and with fftw3f */ |
---|
| 44 | #define FFTW_TYPE float |
---|
| 45 | #else |
---|
| 46 | /** fft data type without complex.h and with fftw */ |
---|
| 47 | #define FFTW_TYPE double |
---|
| 48 | #endif |
---|
| 49 | #endif |
---|
| 50 | |
---|
| 51 | /** fft data type */ |
---|
| 52 | typedef FFTW_TYPE fft_data_t; |
---|
| 53 | |
---|
[729a3c0] | 54 | #ifdef HAVE_FFTW3F |
---|
[a47cd35] | 55 | #define fftw_malloc fftwf_malloc |
---|
| 56 | #define fftw_free fftwf_free |
---|
| 57 | #define fftw_execute fftwf_execute |
---|
| 58 | #define fftw_plan_dft_r2c_1d fftwf_plan_dft_r2c_1d |
---|
| 59 | #define fftw_plan_dft_c2r_1d fftwf_plan_dft_c2r_1d |
---|
| 60 | #define fftw_plan_r2r_1d fftwf_plan_r2r_1d |
---|
| 61 | #define fftw_plan fftwf_plan |
---|
| 62 | #define fftw_destroy_plan fftwf_destroy_plan |
---|
[96fb8ad] | 63 | #endif |
---|
| 64 | |
---|
[729a3c0] | 65 | #ifdef HAVE_FFTW3F |
---|
[c204928] | 66 | #if HAVE_AUBIO_DOUBLE |
---|
[eeb7276] | 67 | #error "Using aubio in double precision with fftw3 in single precision" |
---|
[fd6b90f] | 68 | #endif /* HAVE_AUBIO_DOUBLE */ |
---|
[152ed05] | 69 | #define real_t float |
---|
[eeb7276] | 70 | #elif defined (HAVE_FFTW3) /* HAVE_FFTW3F */ |
---|
[c204928] | 71 | #if !HAVE_AUBIO_DOUBLE |
---|
[eeb7276] | 72 | #error "Using aubio in single precision with fftw3 in double precision" |
---|
[fd6b90f] | 73 | #endif /* HAVE_AUBIO_DOUBLE */ |
---|
[152ed05] | 74 | #define real_t double |
---|
[fd6b90f] | 75 | #endif /* HAVE_FFTW3F */ |
---|
[96fb8ad] | 76 | |
---|
[d453a4a] | 77 | // a global mutex for FFTW thread safety |
---|
| 78 | pthread_mutex_t aubio_fftw_mutex = PTHREAD_MUTEX_INITIALIZER; |
---|
| 79 | |
---|
[54dd945] | 80 | #else |
---|
| 81 | #ifdef HAVE_ACCELERATE // using ACCELERATE |
---|
| 82 | // https://developer.apple.com/library/mac/#documentation/Accelerate/Reference/vDSPRef/Reference/reference.html |
---|
| 83 | #include <Accelerate/Accelerate.h> |
---|
| 84 | |
---|
[39c4721] | 85 | #if !HAVE_AUBIO_DOUBLE |
---|
| 86 | #define aubio_vDSP_ctoz vDSP_ctoz |
---|
| 87 | #define aubio_vDSP_fft_zrip vDSP_fft_zrip |
---|
| 88 | #define aubio_vDSP_ztoc vDSP_ztoc |
---|
| 89 | #define aubio_vDSP_zvmags vDSP_zvmags |
---|
| 90 | #define aubio_vDSP_zvphas vDSP_zvphas |
---|
| 91 | #define aubio_vDSP_vsadd vDSP_vsadd |
---|
| 92 | #define aubio_vDSP_vsmul vDSP_vsmul |
---|
| 93 | #define aubio_vDSP_create_fftsetup vDSP_create_fftsetup |
---|
| 94 | #define aubio_vDSP_destroy_fftsetup vDSP_destroy_fftsetup |
---|
| 95 | #define aubio_DSPComplex DSPComplex |
---|
| 96 | #define aubio_DSPSplitComplex DSPSplitComplex |
---|
| 97 | #define aubio_FFTSetup FFTSetup |
---|
| 98 | #define aubio_vvsqrt vvsqrtf |
---|
| 99 | #else |
---|
| 100 | #define aubio_vDSP_ctoz vDSP_ctozD |
---|
| 101 | #define aubio_vDSP_fft_zrip vDSP_fft_zripD |
---|
| 102 | #define aubio_vDSP_ztoc vDSP_ztocD |
---|
| 103 | #define aubio_vDSP_zvmags vDSP_zvmagsD |
---|
| 104 | #define aubio_vDSP_zvphas vDSP_zvphasD |
---|
| 105 | #define aubio_vDSP_vsadd vDSP_vsaddD |
---|
| 106 | #define aubio_vDSP_vsmul vDSP_vsmulD |
---|
| 107 | #define aubio_vDSP_create_fftsetup vDSP_create_fftsetupD |
---|
| 108 | #define aubio_vDSP_destroy_fftsetup vDSP_destroy_fftsetupD |
---|
| 109 | #define aubio_DSPComplex DSPDoubleComplex |
---|
| 110 | #define aubio_DSPSplitComplex DSPDoubleSplitComplex |
---|
| 111 | #define aubio_FFTSetup FFTSetupD |
---|
| 112 | #define aubio_vvsqrt vvsqrt |
---|
| 113 | #endif /* HAVE_AUBIO_DOUBLE */ |
---|
| 114 | |
---|
[54dd945] | 115 | #else // using OOURA |
---|
| 116 | // let's use ooura instead |
---|
[f50c9503] | 117 | extern void aubio_ooura_rdft(int, int, smpl_t *, int *, smpl_t *); |
---|
[54dd945] | 118 | |
---|
| 119 | #endif /* HAVE_ACCELERATE */ |
---|
[729a3c0] | 120 | #endif /* HAVE_FFTW3 */ |
---|
| 121 | |
---|
[96fb8ad] | 122 | struct _aubio_fft_t { |
---|
[aadd27a] | 123 | uint_t winsize; |
---|
| 124 | uint_t fft_size; |
---|
[54dd945] | 125 | #ifdef HAVE_FFTW3 // using FFTW3 |
---|
[aadd27a] | 126 | real_t *in, *out; |
---|
[4b6937b] | 127 | fftw_plan pfw, pbw; |
---|
[152ed05] | 128 | fft_data_t * specdata; /* complex spectral data */ |
---|
[729a3c0] | 129 | #else |
---|
[54dd945] | 130 | #ifdef HAVE_ACCELERATE // using ACCELERATE |
---|
| 131 | int log2fftsize; |
---|
[39c4721] | 132 | aubio_FFTSetup fftSetup; |
---|
| 133 | aubio_DSPSplitComplex spec; |
---|
| 134 | smpl_t *in, *out; |
---|
[54dd945] | 135 | #else // using OOURA |
---|
[5c6b264] | 136 | smpl_t *in, *out; |
---|
| 137 | smpl_t *w; |
---|
[729a3c0] | 138 | int *ip; |
---|
[54dd945] | 139 | #endif /* HAVE_ACCELERATE */ |
---|
[729a3c0] | 140 | #endif /* HAVE_FFTW3 */ |
---|
[aadd27a] | 141 | fvec_t * compspec; |
---|
[96fb8ad] | 142 | }; |
---|
| 143 | |
---|
[729a3c0] | 144 | aubio_fft_t * new_aubio_fft (uint_t winsize) { |
---|
[a47cd35] | 145 | aubio_fft_t * s = AUBIO_NEW(aubio_fft_t); |
---|
[d897aae] | 146 | if ((sint_t)winsize < 2) { |
---|
| 147 | AUBIO_ERR("fft: got winsize %d, but can not be < 2\n", winsize); |
---|
[a984461] | 148 | goto beach; |
---|
| 149 | } |
---|
[729a3c0] | 150 | #ifdef HAVE_FFTW3 |
---|
[8b3a7e7] | 151 | uint_t i; |
---|
[aadd27a] | 152 | s->winsize = winsize; |
---|
[a47cd35] | 153 | /* allocate memory */ |
---|
[aadd27a] | 154 | s->in = AUBIO_ARRAY(real_t,winsize); |
---|
| 155 | s->out = AUBIO_ARRAY(real_t,winsize); |
---|
[d95ff38] | 156 | s->compspec = new_fvec(winsize); |
---|
[a47cd35] | 157 | /* create plans */ |
---|
[d453a4a] | 158 | pthread_mutex_lock(&aubio_fftw_mutex); |
---|
[237f632] | 159 | #ifdef HAVE_COMPLEX_H |
---|
[4b6937b] | 160 | s->fft_size = winsize/2 + 1; |
---|
[a47cd35] | 161 | s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size); |
---|
[aadd27a] | 162 | s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in, s->specdata, FFTW_ESTIMATE); |
---|
| 163 | s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE); |
---|
[237f632] | 164 | #else |
---|
[aadd27a] | 165 | s->fft_size = winsize; |
---|
[a47cd35] | 166 | s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size); |
---|
[aadd27a] | 167 | s->pfw = fftw_plan_r2r_1d(winsize, s->in, s->specdata, FFTW_R2HC, FFTW_ESTIMATE); |
---|
| 168 | s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE); |
---|
[237f632] | 169 | #endif |
---|
[d453a4a] | 170 | pthread_mutex_unlock(&aubio_fftw_mutex); |
---|
[4f4299d] | 171 | for (i = 0; i < s->winsize; i++) { |
---|
| 172 | s->in[i] = 0.; |
---|
| 173 | s->out[i] = 0.; |
---|
| 174 | } |
---|
| 175 | for (i = 0; i < s->fft_size; i++) { |
---|
| 176 | s->specdata[i] = 0.; |
---|
| 177 | } |
---|
[729a3c0] | 178 | #else |
---|
[54dd945] | 179 | #ifdef HAVE_ACCELERATE // using ACCELERATE |
---|
| 180 | s->winsize = winsize; |
---|
| 181 | s->fft_size = winsize; |
---|
| 182 | s->compspec = new_fvec(winsize); |
---|
| 183 | s->log2fftsize = (uint_t)log2f(s->fft_size); |
---|
[39c4721] | 184 | s->in = AUBIO_ARRAY(smpl_t, s->fft_size); |
---|
| 185 | s->out = AUBIO_ARRAY(smpl_t, s->fft_size); |
---|
| 186 | s->spec.realp = AUBIO_ARRAY(smpl_t, s->fft_size/2); |
---|
| 187 | s->spec.imagp = AUBIO_ARRAY(smpl_t, s->fft_size/2); |
---|
| 188 | s->fftSetup = aubio_vDSP_create_fftsetup(s->log2fftsize, FFT_RADIX2); |
---|
[54dd945] | 189 | #else // using OOURA |
---|
[3c6f584] | 190 | if (aubio_is_power_of_two(winsize) != 1) { |
---|
[1b57274] | 191 | AUBIO_ERR("fft: can only create with sizes power of two, requested %d," |
---|
| 192 | " try recompiling aubio with --enable-fftw3\n", winsize); |
---|
[a984461] | 193 | goto beach; |
---|
[3c6f584] | 194 | } |
---|
[729a3c0] | 195 | s->winsize = winsize; |
---|
| 196 | s->fft_size = winsize / 2 + 1; |
---|
| 197 | s->compspec = new_fvec(winsize); |
---|
[5c6b264] | 198 | s->in = AUBIO_ARRAY(smpl_t, s->winsize); |
---|
| 199 | s->out = AUBIO_ARRAY(smpl_t, s->winsize); |
---|
[729a3c0] | 200 | s->ip = AUBIO_ARRAY(int , s->fft_size); |
---|
[5c6b264] | 201 | s->w = AUBIO_ARRAY(smpl_t, s->fft_size); |
---|
[729a3c0] | 202 | s->ip[0] = 0; |
---|
[54dd945] | 203 | #endif /* HAVE_ACCELERATE */ |
---|
| 204 | #endif /* HAVE_FFTW3 */ |
---|
[a47cd35] | 205 | return s; |
---|
[a984461] | 206 | beach: |
---|
| 207 | AUBIO_FREE(s); |
---|
| 208 | return NULL; |
---|
[96fb8ad] | 209 | } |
---|
| 210 | |
---|
| 211 | void del_aubio_fft(aubio_fft_t * s) { |
---|
[a47cd35] | 212 | /* destroy data */ |
---|
[aadd27a] | 213 | del_fvec(s->compspec); |
---|
[54dd945] | 214 | #ifdef HAVE_FFTW3 // using FFTW3 |
---|
[4b251ae] | 215 | pthread_mutex_lock(&aubio_fftw_mutex); |
---|
[a47cd35] | 216 | fftw_destroy_plan(s->pfw); |
---|
| 217 | fftw_destroy_plan(s->pbw); |
---|
| 218 | fftw_free(s->specdata); |
---|
[4b251ae] | 219 | pthread_mutex_unlock(&aubio_fftw_mutex); |
---|
[729a3c0] | 220 | #else /* HAVE_FFTW3 */ |
---|
[54dd945] | 221 | #ifdef HAVE_ACCELERATE // using ACCELERATE |
---|
| 222 | AUBIO_FREE(s->spec.realp); |
---|
| 223 | AUBIO_FREE(s->spec.imagp); |
---|
[39c4721] | 224 | aubio_vDSP_destroy_fftsetup(s->fftSetup); |
---|
[54dd945] | 225 | #else // using OOURA |
---|
[729a3c0] | 226 | AUBIO_FREE(s->w); |
---|
| 227 | AUBIO_FREE(s->ip); |
---|
[54dd945] | 228 | #endif /* HAVE_ACCELERATE */ |
---|
[729a3c0] | 229 | #endif /* HAVE_FFTW3 */ |
---|
[a47cd35] | 230 | AUBIO_FREE(s->out); |
---|
[729a3c0] | 231 | AUBIO_FREE(s->in); |
---|
[a47cd35] | 232 | AUBIO_FREE(s); |
---|
[96fb8ad] | 233 | } |
---|
| 234 | |
---|
[feb694b] | 235 | void aubio_fft_do(aubio_fft_t * s, const fvec_t * input, cvec_t * spectrum) { |
---|
[aadd27a] | 236 | aubio_fft_do_complex(s, input, s->compspec); |
---|
| 237 | aubio_fft_get_spectrum(s->compspec, spectrum); |
---|
[96fb8ad] | 238 | } |
---|
| 239 | |
---|
[feb694b] | 240 | void aubio_fft_rdo(aubio_fft_t * s, const cvec_t * spectrum, fvec_t * output) { |
---|
[aadd27a] | 241 | aubio_fft_get_realimag(spectrum, s->compspec); |
---|
| 242 | aubio_fft_rdo_complex(s, s->compspec, output); |
---|
[96fb8ad] | 243 | } |
---|
| 244 | |
---|
[feb694b] | 245 | void aubio_fft_do_complex(aubio_fft_t * s, const fvec_t * input, fvec_t * compspec) { |
---|
[729a3c0] | 246 | uint_t i; |
---|
[b5d32cb] | 247 | #ifndef HAVE_MEMCPY_HACKS |
---|
[729a3c0] | 248 | for (i=0; i < s->winsize; i++) { |
---|
| 249 | s->in[i] = input->data[i]; |
---|
[d95ff38] | 250 | } |
---|
[b5d32cb] | 251 | #else |
---|
| 252 | memcpy(s->in, input->data, s->winsize * sizeof(smpl_t)); |
---|
| 253 | #endif /* HAVE_MEMCPY_HACKS */ |
---|
[54dd945] | 254 | #ifdef HAVE_FFTW3 // using FFTW3 |
---|
[d95ff38] | 255 | fftw_execute(s->pfw); |
---|
[4b6937b] | 256 | #ifdef HAVE_COMPLEX_H |
---|
[d95ff38] | 257 | compspec->data[0] = REAL(s->specdata[0]); |
---|
[729a3c0] | 258 | for (i = 1; i < s->fft_size -1 ; i++) { |
---|
| 259 | compspec->data[i] = REAL(s->specdata[i]); |
---|
| 260 | compspec->data[compspec->length - i] = IMAG(s->specdata[i]); |
---|
[d95ff38] | 261 | } |
---|
| 262 | compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]); |
---|
[729a3c0] | 263 | #else /* HAVE_COMPLEX_H */ |
---|
| 264 | for (i = 0; i < s->fft_size; i++) { |
---|
| 265 | compspec->data[i] = s->specdata[i]; |
---|
[237f632] | 266 | } |
---|
[729a3c0] | 267 | #endif /* HAVE_COMPLEX_H */ |
---|
| 268 | #else /* HAVE_FFTW3 */ |
---|
[54dd945] | 269 | #ifdef HAVE_ACCELERATE // using ACCELERATE |
---|
| 270 | // convert real data to even/odd format used in vDSP |
---|
[39c4721] | 271 | aubio_vDSP_ctoz((aubio_DSPComplex*)s->in, 2, &s->spec, 1, s->fft_size/2); |
---|
[54dd945] | 272 | // compute the FFT |
---|
[39c4721] | 273 | aubio_vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_FORWARD); |
---|
[54dd945] | 274 | // convert from vDSP complex split to [ r0, r1, ..., rN, iN-1, .., i2, i1] |
---|
| 275 | compspec->data[0] = s->spec.realp[0]; |
---|
| 276 | compspec->data[s->fft_size / 2] = s->spec.imagp[0]; |
---|
| 277 | for (i = 1; i < s->fft_size / 2; i++) { |
---|
| 278 | compspec->data[i] = s->spec.realp[i]; |
---|
| 279 | compspec->data[s->fft_size - i] = s->spec.imagp[i]; |
---|
| 280 | } |
---|
| 281 | // apply scaling |
---|
| 282 | smpl_t scale = 1./2.; |
---|
[39c4721] | 283 | aubio_vDSP_vsmul(compspec->data, 1, &scale, compspec->data, 1, s->fft_size); |
---|
[54dd945] | 284 | #else // using OOURA |
---|
[f50c9503] | 285 | aubio_ooura_rdft(s->winsize, 1, s->in, s->ip, s->w); |
---|
[729a3c0] | 286 | compspec->data[0] = s->in[0]; |
---|
| 287 | compspec->data[s->winsize / 2] = s->in[1]; |
---|
| 288 | for (i = 1; i < s->fft_size - 1; i++) { |
---|
| 289 | compspec->data[i] = s->in[2 * i]; |
---|
| 290 | compspec->data[s->winsize - i] = - s->in[2 * i + 1]; |
---|
| 291 | } |
---|
[54dd945] | 292 | #endif /* HAVE_ACCELERATE */ |
---|
[729a3c0] | 293 | #endif /* HAVE_FFTW3 */ |
---|
[96fb8ad] | 294 | } |
---|
| 295 | |
---|
[feb694b] | 296 | void aubio_fft_rdo_complex(aubio_fft_t * s, const fvec_t * compspec, fvec_t * output) { |
---|
[729a3c0] | 297 | uint_t i; |
---|
| 298 | #ifdef HAVE_FFTW3 |
---|
[aadd27a] | 299 | const smpl_t renorm = 1./(smpl_t)s->winsize; |
---|
[4b6937b] | 300 | #ifdef HAVE_COMPLEX_H |
---|
[d95ff38] | 301 | s->specdata[0] = compspec->data[0]; |
---|
[729a3c0] | 302 | for (i=1; i < s->fft_size - 1; i++) { |
---|
| 303 | s->specdata[i] = compspec->data[i] + |
---|
| 304 | I * compspec->data[compspec->length - i]; |
---|
[d95ff38] | 305 | } |
---|
| 306 | s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1]; |
---|
[237f632] | 307 | #else |
---|
[729a3c0] | 308 | for (i=0; i < s->fft_size; i++) { |
---|
| 309 | s->specdata[i] = compspec->data[i]; |
---|
[d95ff38] | 310 | } |
---|
[aadd27a] | 311 | #endif |
---|
[d95ff38] | 312 | fftw_execute(s->pbw); |
---|
[729a3c0] | 313 | for (i = 0; i < output->length; i++) { |
---|
| 314 | output->data[i] = s->out[i]*renorm; |
---|
| 315 | } |
---|
| 316 | #else /* HAVE_FFTW3 */ |
---|
[54dd945] | 317 | #ifdef HAVE_ACCELERATE // using ACCELERATE |
---|
| 318 | // convert from real imag [ r0, r1, ..., rN, iN-1, .., i2, i1] |
---|
| 319 | // to vDSP packed format [ r0, rN, r1, i1, ..., rN-1, iN-1 ] |
---|
| 320 | s->out[0] = compspec->data[0]; |
---|
| 321 | s->out[1] = compspec->data[s->winsize / 2]; |
---|
| 322 | for (i = 1; i < s->fft_size / 2; i++) { |
---|
| 323 | s->out[2 * i] = compspec->data[i]; |
---|
| 324 | s->out[2 * i + 1] = compspec->data[s->winsize - i]; |
---|
| 325 | } |
---|
| 326 | // convert to split complex format used in vDSP |
---|
[39c4721] | 327 | aubio_vDSP_ctoz((aubio_DSPComplex*)s->out, 2, &s->spec, 1, s->fft_size/2); |
---|
[54dd945] | 328 | // compute the FFT |
---|
[39c4721] | 329 | aubio_vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_INVERSE); |
---|
[54dd945] | 330 | // convert result to real output |
---|
[39c4721] | 331 | aubio_vDSP_ztoc(&s->spec, 1, (aubio_DSPComplex*)output->data, 2, s->fft_size/2); |
---|
[54dd945] | 332 | // apply scaling |
---|
| 333 | smpl_t scale = 1.0 / s->winsize; |
---|
[39c4721] | 334 | aubio_vDSP_vsmul(output->data, 1, &scale, output->data, 1, s->fft_size); |
---|
[54dd945] | 335 | #else // using OOURA |
---|
[729a3c0] | 336 | smpl_t scale = 2.0 / s->winsize; |
---|
| 337 | s->out[0] = compspec->data[0]; |
---|
| 338 | s->out[1] = compspec->data[s->winsize / 2]; |
---|
| 339 | for (i = 1; i < s->fft_size - 1; i++) { |
---|
| 340 | s->out[2 * i] = compspec->data[i]; |
---|
| 341 | s->out[2 * i + 1] = - compspec->data[s->winsize - i]; |
---|
[aadd27a] | 342 | } |
---|
[f50c9503] | 343 | aubio_ooura_rdft(s->winsize, -1, s->out, s->ip, s->w); |
---|
[729a3c0] | 344 | for (i=0; i < s->winsize; i++) { |
---|
| 345 | output->data[i] = s->out[i] * scale; |
---|
| 346 | } |
---|
[54dd945] | 347 | #endif /* HAVE_ACCELERATE */ |
---|
[729a3c0] | 348 | #endif /* HAVE_FFTW3 */ |
---|
[237f632] | 349 | } |
---|
| 350 | |
---|
[feb694b] | 351 | void aubio_fft_get_spectrum(const fvec_t * compspec, cvec_t * spectrum) { |
---|
[aadd27a] | 352 | aubio_fft_get_phas(compspec, spectrum); |
---|
| 353 | aubio_fft_get_norm(compspec, spectrum); |
---|
[237f632] | 354 | } |
---|
| 355 | |
---|
[feb694b] | 356 | void aubio_fft_get_realimag(const cvec_t * spectrum, fvec_t * compspec) { |
---|
[aadd27a] | 357 | aubio_fft_get_imag(spectrum, compspec); |
---|
| 358 | aubio_fft_get_real(spectrum, compspec); |
---|
[237f632] | 359 | } |
---|
| 360 | |
---|
[feb694b] | 361 | void aubio_fft_get_phas(const fvec_t * compspec, cvec_t * spectrum) { |
---|
[729a3c0] | 362 | uint_t i; |
---|
[d95ff38] | 363 | if (compspec->data[0] < 0) { |
---|
| 364 | spectrum->phas[0] = PI; |
---|
| 365 | } else { |
---|
| 366 | spectrum->phas[0] = 0.; |
---|
| 367 | } |
---|
[729a3c0] | 368 | for (i=1; i < spectrum->length - 1; i++) { |
---|
| 369 | spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i], |
---|
| 370 | compspec->data[i]); |
---|
[d95ff38] | 371 | } |
---|
| 372 | if (compspec->data[compspec->length/2] < 0) { |
---|
| 373 | spectrum->phas[spectrum->length - 1] = PI; |
---|
| 374 | } else { |
---|
| 375 | spectrum->phas[spectrum->length - 1] = 0.; |
---|
[aadd27a] | 376 | } |
---|
[f88a326] | 377 | } |
---|
| 378 | |
---|
[feb694b] | 379 | void aubio_fft_get_norm(const fvec_t * compspec, cvec_t * spectrum) { |
---|
[729a3c0] | 380 | uint_t i = 0; |
---|
[d95ff38] | 381 | spectrum->norm[0] = ABS(compspec->data[0]); |
---|
[729a3c0] | 382 | for (i=1; i < spectrum->length - 1; i++) { |
---|
| 383 | spectrum->norm[i] = SQRT(SQR(compspec->data[i]) |
---|
| 384 | + SQR(compspec->data[compspec->length - i]) ); |
---|
[a47cd35] | 385 | } |
---|
[729a3c0] | 386 | spectrum->norm[spectrum->length-1] = |
---|
[d95ff38] | 387 | ABS(compspec->data[compspec->length/2]); |
---|
[f88a326] | 388 | } |
---|
| 389 | |
---|
[feb694b] | 390 | void aubio_fft_get_imag(const cvec_t * spectrum, fvec_t * compspec) { |
---|
[729a3c0] | 391 | uint_t i; |
---|
| 392 | for (i = 1; i < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; i++) { |
---|
| 393 | compspec->data[compspec->length - i] = |
---|
| 394 | spectrum->norm[i]*SIN(spectrum->phas[i]); |
---|
[a47cd35] | 395 | } |
---|
[f88a326] | 396 | } |
---|
| 397 | |
---|
[feb694b] | 398 | void aubio_fft_get_real(const cvec_t * spectrum, fvec_t * compspec) { |
---|
[729a3c0] | 399 | uint_t i; |
---|
| 400 | for (i = 0; i < compspec->length / 2 + 1; i++) { |
---|
[152ed05] | 401 | compspec->data[i] = |
---|
[729a3c0] | 402 | spectrum->norm[i]*COS(spectrum->phas[i]); |
---|
[aadd27a] | 403 | } |
---|
[f88a326] | 404 | } |
---|