[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 | |
---|
[986131d] | 80 | #elif defined HAVE_ACCELERATE // using ACCELERATE |
---|
[54dd945] | 81 | // https://developer.apple.com/library/mac/#documentation/Accelerate/Reference/vDSPRef/Reference/reference.html |
---|
| 82 | #include <Accelerate/Accelerate.h> |
---|
| 83 | |
---|
[39c4721] | 84 | #if !HAVE_AUBIO_DOUBLE |
---|
| 85 | #define aubio_vDSP_ctoz vDSP_ctoz |
---|
| 86 | #define aubio_vDSP_fft_zrip vDSP_fft_zrip |
---|
| 87 | #define aubio_vDSP_ztoc vDSP_ztoc |
---|
| 88 | #define aubio_vDSP_zvmags vDSP_zvmags |
---|
| 89 | #define aubio_vDSP_zvphas vDSP_zvphas |
---|
| 90 | #define aubio_vDSP_vsadd vDSP_vsadd |
---|
| 91 | #define aubio_vDSP_vsmul vDSP_vsmul |
---|
| 92 | #define aubio_vDSP_create_fftsetup vDSP_create_fftsetup |
---|
| 93 | #define aubio_vDSP_destroy_fftsetup vDSP_destroy_fftsetup |
---|
| 94 | #define aubio_DSPComplex DSPComplex |
---|
| 95 | #define aubio_DSPSplitComplex DSPSplitComplex |
---|
| 96 | #define aubio_FFTSetup FFTSetup |
---|
| 97 | #define aubio_vvsqrt vvsqrtf |
---|
| 98 | #else |
---|
| 99 | #define aubio_vDSP_ctoz vDSP_ctozD |
---|
| 100 | #define aubio_vDSP_fft_zrip vDSP_fft_zripD |
---|
| 101 | #define aubio_vDSP_ztoc vDSP_ztocD |
---|
| 102 | #define aubio_vDSP_zvmags vDSP_zvmagsD |
---|
| 103 | #define aubio_vDSP_zvphas vDSP_zvphasD |
---|
| 104 | #define aubio_vDSP_vsadd vDSP_vsaddD |
---|
| 105 | #define aubio_vDSP_vsmul vDSP_vsmulD |
---|
| 106 | #define aubio_vDSP_create_fftsetup vDSP_create_fftsetupD |
---|
| 107 | #define aubio_vDSP_destroy_fftsetup vDSP_destroy_fftsetupD |
---|
| 108 | #define aubio_DSPComplex DSPDoubleComplex |
---|
| 109 | #define aubio_DSPSplitComplex DSPDoubleSplitComplex |
---|
| 110 | #define aubio_FFTSetup FFTSetupD |
---|
| 111 | #define aubio_vvsqrt vvsqrt |
---|
| 112 | #endif /* HAVE_AUBIO_DOUBLE */ |
---|
| 113 | |
---|
[986131d] | 114 | #elif defined HAVE_INTEL_IPP // using INTEL IPP |
---|
| 115 | |
---|
| 116 | #include <ippcore.h> |
---|
| 117 | #include <ippvm.h> |
---|
| 118 | #include <ipps.h> |
---|
| 119 | |
---|
| 120 | #else // using OOURA |
---|
[54dd945] | 121 | // let's use ooura instead |
---|
[f50c9503] | 122 | extern void aubio_ooura_rdft(int, int, smpl_t *, int *, smpl_t *); |
---|
[54dd945] | 123 | |
---|
[986131d] | 124 | #endif |
---|
[729a3c0] | 125 | |
---|
[96fb8ad] | 126 | struct _aubio_fft_t { |
---|
[aadd27a] | 127 | uint_t winsize; |
---|
| 128 | uint_t fft_size; |
---|
[986131d] | 129 | |
---|
[54dd945] | 130 | #ifdef HAVE_FFTW3 // using FFTW3 |
---|
[aadd27a] | 131 | real_t *in, *out; |
---|
[4b6937b] | 132 | fftw_plan pfw, pbw; |
---|
[986131d] | 133 | fft_data_t * specdata; /* complex spectral data */ |
---|
| 134 | |
---|
| 135 | #elif defined HAVE_ACCELERATE // using ACCELERATE |
---|
[54dd945] | 136 | int log2fftsize; |
---|
[39c4721] | 137 | aubio_FFTSetup fftSetup; |
---|
| 138 | aubio_DSPSplitComplex spec; |
---|
| 139 | smpl_t *in, *out; |
---|
[986131d] | 140 | |
---|
| 141 | #elif defined HAVE_INTEL_IPP // using Intel IPP |
---|
| 142 | // mark FFT impl as Intel IPP |
---|
| 143 | #define INTEL_IPP_FFT 1 |
---|
| 144 | smpl_t *in, *out; |
---|
| 145 | Ipp8u* memSpec; |
---|
| 146 | Ipp8u* memInit; |
---|
| 147 | Ipp8u* memBuffer; |
---|
| 148 | #if HAVE_AUBIO_DOUBLE |
---|
| 149 | struct FFTSpec_R_64f* fftSpec; |
---|
| 150 | Ipp64fc* complexOut; |
---|
| 151 | #else |
---|
| 152 | struct FFTSpec_R_32f* fftSpec; |
---|
| 153 | Ipp32fc* complexOut; |
---|
| 154 | #endif |
---|
[54dd945] | 155 | #else // using OOURA |
---|
[5c6b264] | 156 | smpl_t *in, *out; |
---|
| 157 | smpl_t *w; |
---|
[729a3c0] | 158 | int *ip; |
---|
[986131d] | 159 | #endif /* using OOURA */ |
---|
| 160 | |
---|
[aadd27a] | 161 | fvec_t * compspec; |
---|
[96fb8ad] | 162 | }; |
---|
| 163 | |
---|
[729a3c0] | 164 | aubio_fft_t * new_aubio_fft (uint_t winsize) { |
---|
[a47cd35] | 165 | aubio_fft_t * s = AUBIO_NEW(aubio_fft_t); |
---|
[d897aae] | 166 | if ((sint_t)winsize < 2) { |
---|
| 167 | AUBIO_ERR("fft: got winsize %d, but can not be < 2\n", winsize); |
---|
[a984461] | 168 | goto beach; |
---|
| 169 | } |
---|
[986131d] | 170 | |
---|
[729a3c0] | 171 | #ifdef HAVE_FFTW3 |
---|
[8b3a7e7] | 172 | uint_t i; |
---|
[aadd27a] | 173 | s->winsize = winsize; |
---|
[a47cd35] | 174 | /* allocate memory */ |
---|
[aadd27a] | 175 | s->in = AUBIO_ARRAY(real_t,winsize); |
---|
| 176 | s->out = AUBIO_ARRAY(real_t,winsize); |
---|
[d95ff38] | 177 | s->compspec = new_fvec(winsize); |
---|
[a47cd35] | 178 | /* create plans */ |
---|
[d453a4a] | 179 | pthread_mutex_lock(&aubio_fftw_mutex); |
---|
[237f632] | 180 | #ifdef HAVE_COMPLEX_H |
---|
[4b6937b] | 181 | s->fft_size = winsize/2 + 1; |
---|
[a47cd35] | 182 | s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size); |
---|
[aadd27a] | 183 | s->pfw = fftw_plan_dft_r2c_1d(winsize, s->in, s->specdata, FFTW_ESTIMATE); |
---|
| 184 | s->pbw = fftw_plan_dft_c2r_1d(winsize, s->specdata, s->out, FFTW_ESTIMATE); |
---|
[237f632] | 185 | #else |
---|
[aadd27a] | 186 | s->fft_size = winsize; |
---|
[a47cd35] | 187 | s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size); |
---|
[aadd27a] | 188 | s->pfw = fftw_plan_r2r_1d(winsize, s->in, s->specdata, FFTW_R2HC, FFTW_ESTIMATE); |
---|
| 189 | s->pbw = fftw_plan_r2r_1d(winsize, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE); |
---|
[237f632] | 190 | #endif |
---|
[d453a4a] | 191 | pthread_mutex_unlock(&aubio_fftw_mutex); |
---|
[4f4299d] | 192 | for (i = 0; i < s->winsize; i++) { |
---|
| 193 | s->in[i] = 0.; |
---|
| 194 | s->out[i] = 0.; |
---|
| 195 | } |
---|
| 196 | for (i = 0; i < s->fft_size; i++) { |
---|
| 197 | s->specdata[i] = 0.; |
---|
| 198 | } |
---|
[986131d] | 199 | |
---|
| 200 | #elif defined HAVE_ACCELERATE // using ACCELERATE |
---|
[54dd945] | 201 | s->winsize = winsize; |
---|
| 202 | s->fft_size = winsize; |
---|
| 203 | s->compspec = new_fvec(winsize); |
---|
[986131d] | 204 | s->log2fftsize = aubio_power_of_two_order(s->fft_size); |
---|
[39c4721] | 205 | s->in = AUBIO_ARRAY(smpl_t, s->fft_size); |
---|
| 206 | s->out = AUBIO_ARRAY(smpl_t, s->fft_size); |
---|
| 207 | s->spec.realp = AUBIO_ARRAY(smpl_t, s->fft_size/2); |
---|
| 208 | s->spec.imagp = AUBIO_ARRAY(smpl_t, s->fft_size/2); |
---|
| 209 | s->fftSetup = aubio_vDSP_create_fftsetup(s->log2fftsize, FFT_RADIX2); |
---|
[986131d] | 210 | |
---|
| 211 | #elif defined HAVE_INTEL_IPP // using Intel IPP |
---|
| 212 | const IppHintAlgorithm qualityHint = ippAlgHintAccurate; // OR ippAlgHintFast; |
---|
| 213 | const int flags = IPP_FFT_NODIV_BY_ANY; // we're scaling manually afterwards |
---|
| 214 | int order = aubio_power_of_two_order(winsize); |
---|
| 215 | int sizeSpec, sizeInit, sizeBuffer; |
---|
| 216 | IppStatus status; |
---|
| 217 | |
---|
| 218 | if (winsize <= 4 || aubio_is_power_of_two(winsize) != 1) |
---|
| 219 | { |
---|
| 220 | AUBIO_ERR("intel IPP fft: can only create with sizes > 4 and power of two, requested %d," |
---|
| 221 | " try recompiling aubio with --enable-fftw3\n", winsize); |
---|
| 222 | goto beach; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | #if HAVE_AUBIO_DOUBLE |
---|
| 226 | status = ippsFFTGetSize_R_64f(order, flags, qualityHint, |
---|
| 227 | &sizeSpec, &sizeInit, &sizeBuffer); |
---|
| 228 | #else |
---|
| 229 | status = ippsFFTGetSize_R_32f(order, flags, qualityHint, |
---|
| 230 | &sizeSpec, &sizeInit, &sizeBuffer); |
---|
| 231 | #endif |
---|
| 232 | if (status != ippStsNoErr) { |
---|
| 233 | AUBIO_ERR("fft: failed to initialize fft. IPP error: %d\n", status); |
---|
| 234 | goto beach; |
---|
| 235 | } |
---|
| 236 | s->fft_size = s->winsize = winsize; |
---|
| 237 | s->compspec = new_fvec(winsize); |
---|
| 238 | s->in = AUBIO_ARRAY(smpl_t, s->winsize); |
---|
| 239 | s->out = AUBIO_ARRAY(smpl_t, s->winsize); |
---|
| 240 | s->memSpec = ippsMalloc_8u(sizeSpec); |
---|
| 241 | s->memBuffer = ippsMalloc_8u(sizeBuffer); |
---|
| 242 | if (sizeInit > 0 ) { |
---|
| 243 | s->memInit = ippsMalloc_8u(sizeInit); |
---|
| 244 | } |
---|
| 245 | #if HAVE_AUBIO_DOUBLE |
---|
| 246 | s->complexOut = ippsMalloc_64fc(s->fft_size / 2 + 1); |
---|
| 247 | status = ippsFFTInit_R_64f( |
---|
| 248 | &s->fftSpec, order, flags, qualityHint, s->memSpec, s->memInit); |
---|
| 249 | #else |
---|
| 250 | s->complexOut = ippsMalloc_32fc(s->fft_size / 2 + 1); |
---|
| 251 | status = ippsFFTInit_R_32f( |
---|
| 252 | &s->fftSpec, order, flags, qualityHint, s->memSpec, s->memInit); |
---|
| 253 | #endif |
---|
| 254 | if (status != ippStsNoErr) { |
---|
| 255 | AUBIO_ERR("fft: failed to initialize. IPP error: %d\n", status); |
---|
| 256 | goto beach; |
---|
| 257 | } |
---|
| 258 | |
---|
[54dd945] | 259 | #else // using OOURA |
---|
[3c6f584] | 260 | if (aubio_is_power_of_two(winsize) != 1) { |
---|
[1b57274] | 261 | AUBIO_ERR("fft: can only create with sizes power of two, requested %d," |
---|
| 262 | " try recompiling aubio with --enable-fftw3\n", winsize); |
---|
[a984461] | 263 | goto beach; |
---|
[3c6f584] | 264 | } |
---|
[729a3c0] | 265 | s->winsize = winsize; |
---|
| 266 | s->fft_size = winsize / 2 + 1; |
---|
| 267 | s->compspec = new_fvec(winsize); |
---|
[5c6b264] | 268 | s->in = AUBIO_ARRAY(smpl_t, s->winsize); |
---|
| 269 | s->out = AUBIO_ARRAY(smpl_t, s->winsize); |
---|
[729a3c0] | 270 | s->ip = AUBIO_ARRAY(int , s->fft_size); |
---|
[5c6b264] | 271 | s->w = AUBIO_ARRAY(smpl_t, s->fft_size); |
---|
[729a3c0] | 272 | s->ip[0] = 0; |
---|
[986131d] | 273 | #endif /* using OOURA */ |
---|
| 274 | |
---|
[a47cd35] | 275 | return s; |
---|
[986131d] | 276 | |
---|
[a984461] | 277 | beach: |
---|
| 278 | AUBIO_FREE(s); |
---|
| 279 | return NULL; |
---|
[96fb8ad] | 280 | } |
---|
| 281 | |
---|
| 282 | void del_aubio_fft(aubio_fft_t * s) { |
---|
[a47cd35] | 283 | /* destroy data */ |
---|
[54dd945] | 284 | #ifdef HAVE_FFTW3 // using FFTW3 |
---|
[4b251ae] | 285 | pthread_mutex_lock(&aubio_fftw_mutex); |
---|
[a47cd35] | 286 | fftw_destroy_plan(s->pfw); |
---|
| 287 | fftw_destroy_plan(s->pbw); |
---|
| 288 | fftw_free(s->specdata); |
---|
[4b251ae] | 289 | pthread_mutex_unlock(&aubio_fftw_mutex); |
---|
[986131d] | 290 | |
---|
| 291 | #elif defined HAVE_ACCELERATE // using ACCELERATE |
---|
[54dd945] | 292 | AUBIO_FREE(s->spec.realp); |
---|
| 293 | AUBIO_FREE(s->spec.imagp); |
---|
[39c4721] | 294 | aubio_vDSP_destroy_fftsetup(s->fftSetup); |
---|
[986131d] | 295 | |
---|
| 296 | #elif defined HAVE_INTEL_IPP // using Intel IPP |
---|
| 297 | ippFree(s->memSpec); |
---|
| 298 | ippFree(s->memInit); |
---|
| 299 | ippFree(s->memBuffer); |
---|
| 300 | ippFree(s->complexOut); |
---|
| 301 | |
---|
[54dd945] | 302 | #else // using OOURA |
---|
[729a3c0] | 303 | AUBIO_FREE(s->w); |
---|
| 304 | AUBIO_FREE(s->ip); |
---|
[986131d] | 305 | #endif |
---|
| 306 | |
---|
| 307 | del_fvec(s->compspec); |
---|
[729a3c0] | 308 | AUBIO_FREE(s->in); |
---|
[986131d] | 309 | AUBIO_FREE(s->out); |
---|
[a47cd35] | 310 | AUBIO_FREE(s); |
---|
[96fb8ad] | 311 | } |
---|
| 312 | |
---|
[feb694b] | 313 | void aubio_fft_do(aubio_fft_t * s, const fvec_t * input, cvec_t * spectrum) { |
---|
[aadd27a] | 314 | aubio_fft_do_complex(s, input, s->compspec); |
---|
[986131d] | 315 | aubio_fft_get_spectrum(s, s->compspec, spectrum); |
---|
[96fb8ad] | 316 | } |
---|
| 317 | |
---|
[feb694b] | 318 | void aubio_fft_rdo(aubio_fft_t * s, const cvec_t * spectrum, fvec_t * output) { |
---|
[986131d] | 319 | aubio_fft_get_realimag(s, spectrum, s->compspec); |
---|
[aadd27a] | 320 | aubio_fft_rdo_complex(s, s->compspec, output); |
---|
[96fb8ad] | 321 | } |
---|
| 322 | |
---|
[feb694b] | 323 | void aubio_fft_do_complex(aubio_fft_t * s, const fvec_t * input, fvec_t * compspec) { |
---|
[729a3c0] | 324 | uint_t i; |
---|
[b5d32cb] | 325 | #ifndef HAVE_MEMCPY_HACKS |
---|
[729a3c0] | 326 | for (i=0; i < s->winsize; i++) { |
---|
| 327 | s->in[i] = input->data[i]; |
---|
[d95ff38] | 328 | } |
---|
[b5d32cb] | 329 | #else |
---|
| 330 | memcpy(s->in, input->data, s->winsize * sizeof(smpl_t)); |
---|
| 331 | #endif /* HAVE_MEMCPY_HACKS */ |
---|
[986131d] | 332 | |
---|
[54dd945] | 333 | #ifdef HAVE_FFTW3 // using FFTW3 |
---|
[d95ff38] | 334 | fftw_execute(s->pfw); |
---|
[4b6937b] | 335 | #ifdef HAVE_COMPLEX_H |
---|
[d95ff38] | 336 | compspec->data[0] = REAL(s->specdata[0]); |
---|
[729a3c0] | 337 | for (i = 1; i < s->fft_size -1 ; i++) { |
---|
| 338 | compspec->data[i] = REAL(s->specdata[i]); |
---|
| 339 | compspec->data[compspec->length - i] = IMAG(s->specdata[i]); |
---|
[d95ff38] | 340 | } |
---|
| 341 | compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]); |
---|
[729a3c0] | 342 | #else /* HAVE_COMPLEX_H */ |
---|
| 343 | for (i = 0; i < s->fft_size; i++) { |
---|
| 344 | compspec->data[i] = s->specdata[i]; |
---|
[237f632] | 345 | } |
---|
[729a3c0] | 346 | #endif /* HAVE_COMPLEX_H */ |
---|
[986131d] | 347 | |
---|
| 348 | #elif defined HAVE_ACCELERATE // using ACCELERATE |
---|
[54dd945] | 349 | // convert real data to even/odd format used in vDSP |
---|
[39c4721] | 350 | aubio_vDSP_ctoz((aubio_DSPComplex*)s->in, 2, &s->spec, 1, s->fft_size/2); |
---|
[54dd945] | 351 | // compute the FFT |
---|
[39c4721] | 352 | aubio_vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_FORWARD); |
---|
[54dd945] | 353 | // convert from vDSP complex split to [ r0, r1, ..., rN, iN-1, .., i2, i1] |
---|
| 354 | compspec->data[0] = s->spec.realp[0]; |
---|
| 355 | compspec->data[s->fft_size / 2] = s->spec.imagp[0]; |
---|
| 356 | for (i = 1; i < s->fft_size / 2; i++) { |
---|
| 357 | compspec->data[i] = s->spec.realp[i]; |
---|
| 358 | compspec->data[s->fft_size - i] = s->spec.imagp[i]; |
---|
| 359 | } |
---|
| 360 | // apply scaling |
---|
| 361 | smpl_t scale = 1./2.; |
---|
[39c4721] | 362 | aubio_vDSP_vsmul(compspec->data, 1, &scale, compspec->data, 1, s->fft_size); |
---|
[986131d] | 363 | |
---|
| 364 | #elif defined HAVE_INTEL_IPP // using Intel IPP |
---|
| 365 | |
---|
| 366 | // apply fft |
---|
| 367 | #if HAVE_AUBIO_DOUBLE |
---|
| 368 | ippsFFTFwd_RToCCS_64f(s->in, (Ipp64f*)s->complexOut, s->fftSpec, s->memBuffer); |
---|
| 369 | #else |
---|
| 370 | ippsFFTFwd_RToCCS_32f(s->in, (Ipp32f*)s->complexOut, s->fftSpec, s->memBuffer); |
---|
| 371 | #endif |
---|
| 372 | // convert complex buffer to [ r0, r1, ..., rN, iN-1, .., i2, i1] |
---|
| 373 | compspec->data[0] = s->complexOut[0].re; |
---|
| 374 | compspec->data[s->fft_size / 2] = s->complexOut[s->fft_size / 2].re; |
---|
| 375 | for (i = 1; i < s->fft_size / 2; i++) { |
---|
| 376 | compspec->data[i] = s->complexOut[i].re; |
---|
| 377 | compspec->data[s->fft_size - i] = s->complexOut[i].im; |
---|
| 378 | } |
---|
| 379 | // apply scaling |
---|
| 380 | #if HAVE_AUBIO_DOUBLE |
---|
| 381 | ippsMulC_64f(compspec->data, 1.0 / 2.0, compspec->data, s->fft_size); |
---|
| 382 | #else |
---|
| 383 | ippsMulC_32f(compspec->data, 1.0 / 2.0, compspec->data, s->fft_size); |
---|
| 384 | #endif |
---|
| 385 | |
---|
[54dd945] | 386 | #else // using OOURA |
---|
[f50c9503] | 387 | aubio_ooura_rdft(s->winsize, 1, s->in, s->ip, s->w); |
---|
[729a3c0] | 388 | compspec->data[0] = s->in[0]; |
---|
| 389 | compspec->data[s->winsize / 2] = s->in[1]; |
---|
| 390 | for (i = 1; i < s->fft_size - 1; i++) { |
---|
| 391 | compspec->data[i] = s->in[2 * i]; |
---|
| 392 | compspec->data[s->winsize - i] = - s->in[2 * i + 1]; |
---|
| 393 | } |
---|
[986131d] | 394 | #endif /* using OOURA */ |
---|
[96fb8ad] | 395 | } |
---|
| 396 | |
---|
[feb694b] | 397 | void aubio_fft_rdo_complex(aubio_fft_t * s, const fvec_t * compspec, fvec_t * output) { |
---|
[729a3c0] | 398 | uint_t i; |
---|
| 399 | #ifdef HAVE_FFTW3 |
---|
[aadd27a] | 400 | const smpl_t renorm = 1./(smpl_t)s->winsize; |
---|
[4b6937b] | 401 | #ifdef HAVE_COMPLEX_H |
---|
[d95ff38] | 402 | s->specdata[0] = compspec->data[0]; |
---|
[729a3c0] | 403 | for (i=1; i < s->fft_size - 1; i++) { |
---|
| 404 | s->specdata[i] = compspec->data[i] + |
---|
| 405 | I * compspec->data[compspec->length - i]; |
---|
[d95ff38] | 406 | } |
---|
| 407 | s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1]; |
---|
[237f632] | 408 | #else |
---|
[729a3c0] | 409 | for (i=0; i < s->fft_size; i++) { |
---|
| 410 | s->specdata[i] = compspec->data[i]; |
---|
[d95ff38] | 411 | } |
---|
[aadd27a] | 412 | #endif |
---|
[d95ff38] | 413 | fftw_execute(s->pbw); |
---|
[729a3c0] | 414 | for (i = 0; i < output->length; i++) { |
---|
| 415 | output->data[i] = s->out[i]*renorm; |
---|
| 416 | } |
---|
[986131d] | 417 | |
---|
| 418 | #elif defined HAVE_ACCELERATE // using ACCELERATE |
---|
[54dd945] | 419 | // convert from real imag [ r0, r1, ..., rN, iN-1, .., i2, i1] |
---|
| 420 | // to vDSP packed format [ r0, rN, r1, i1, ..., rN-1, iN-1 ] |
---|
| 421 | s->out[0] = compspec->data[0]; |
---|
| 422 | s->out[1] = compspec->data[s->winsize / 2]; |
---|
| 423 | for (i = 1; i < s->fft_size / 2; i++) { |
---|
| 424 | s->out[2 * i] = compspec->data[i]; |
---|
| 425 | s->out[2 * i + 1] = compspec->data[s->winsize - i]; |
---|
| 426 | } |
---|
| 427 | // convert to split complex format used in vDSP |
---|
[39c4721] | 428 | aubio_vDSP_ctoz((aubio_DSPComplex*)s->out, 2, &s->spec, 1, s->fft_size/2); |
---|
[54dd945] | 429 | // compute the FFT |
---|
[39c4721] | 430 | aubio_vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_INVERSE); |
---|
[54dd945] | 431 | // convert result to real output |
---|
[39c4721] | 432 | aubio_vDSP_ztoc(&s->spec, 1, (aubio_DSPComplex*)output->data, 2, s->fft_size/2); |
---|
[54dd945] | 433 | // apply scaling |
---|
| 434 | smpl_t scale = 1.0 / s->winsize; |
---|
[39c4721] | 435 | aubio_vDSP_vsmul(output->data, 1, &scale, output->data, 1, s->fft_size); |
---|
[986131d] | 436 | |
---|
| 437 | #elif defined HAVE_INTEL_IPP // using Intel IPP |
---|
| 438 | |
---|
| 439 | // convert from real imag [ r0, 0, ..., rN, iN-1, .., i2, i1, iN-1] to complex format |
---|
| 440 | s->complexOut[0].re = compspec->data[0]; |
---|
| 441 | s->complexOut[0].im = 0; |
---|
| 442 | s->complexOut[s->fft_size / 2].re = compspec->data[s->fft_size / 2]; |
---|
| 443 | s->complexOut[s->fft_size / 2].im = 0.0; |
---|
| 444 | for (i = 1; i < s->fft_size / 2; i++) { |
---|
| 445 | s->complexOut[i].re = compspec->data[i]; |
---|
| 446 | s->complexOut[i].im = compspec->data[s->fft_size - i]; |
---|
| 447 | } |
---|
| 448 | #if HAVE_AUBIO_DOUBLE |
---|
| 449 | // apply fft |
---|
| 450 | ippsFFTInv_CCSToR_64f((const Ipp64f *)s->complexOut, output->data, s->fftSpec, s->memBuffer); |
---|
| 451 | // apply scaling |
---|
| 452 | ippsMulC_64f(output->data, 1.0 / s->winsize, output->data, s->fft_size); |
---|
| 453 | #else |
---|
| 454 | // apply fft |
---|
| 455 | ippsFFTInv_CCSToR_32f((const Ipp32f *)s->complexOut, output->data, s->fftSpec, s->memBuffer); |
---|
| 456 | // apply scaling |
---|
| 457 | ippsMulC_32f(output->data, 1.0f / s->winsize, output->data, s->fft_size); |
---|
| 458 | #endif /* HAVE_AUBIO_DOUBLE */ |
---|
| 459 | |
---|
[54dd945] | 460 | #else // using OOURA |
---|
[986131d] | 461 | smpl_t scale = 1.0 / s->winsize; |
---|
[729a3c0] | 462 | s->out[0] = compspec->data[0]; |
---|
| 463 | s->out[1] = compspec->data[s->winsize / 2]; |
---|
| 464 | for (i = 1; i < s->fft_size - 1; i++) { |
---|
| 465 | s->out[2 * i] = compspec->data[i]; |
---|
| 466 | s->out[2 * i + 1] = - compspec->data[s->winsize - i]; |
---|
[aadd27a] | 467 | } |
---|
[f50c9503] | 468 | aubio_ooura_rdft(s->winsize, -1, s->out, s->ip, s->w); |
---|
[729a3c0] | 469 | for (i=0; i < s->winsize; i++) { |
---|
| 470 | output->data[i] = s->out[i] * scale; |
---|
| 471 | } |
---|
[986131d] | 472 | #endif |
---|
[237f632] | 473 | } |
---|
| 474 | |
---|
[986131d] | 475 | void aubio_fft_get_spectrum(aubio_fft_t *s, const fvec_t * compspec, cvec_t * spectrum) { |
---|
| 476 | aubio_fft_get_phas(s, compspec, spectrum); |
---|
| 477 | aubio_fft_get_norm(s, compspec, spectrum); |
---|
[237f632] | 478 | } |
---|
| 479 | |
---|
[986131d] | 480 | void aubio_fft_get_realimag(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) { |
---|
| 481 | aubio_fft_get_imag(s, spectrum, compspec); |
---|
| 482 | aubio_fft_get_real(s, spectrum, compspec); |
---|
[237f632] | 483 | } |
---|
| 484 | |
---|
[986131d] | 485 | void aubio_fft_get_phas(aubio_fft_t *s, const fvec_t * compspec, cvec_t * spectrum) { |
---|
| 486 | |
---|
| 487 | #ifdef INTEL_IPP_FFT // using Intel IPP FFT |
---|
| 488 | uint_t i; |
---|
| 489 | |
---|
| 490 | // convert from real imag [ r0, 0, ..., rN, iN-1, .., i2, i1, iN-1] to complex format |
---|
| 491 | s->complexOut[0].re = compspec->data[0]; |
---|
| 492 | s->complexOut[0].im = 0; |
---|
| 493 | s->complexOut[s->fft_size / 2].re = compspec->data[s->fft_size / 2]; |
---|
| 494 | s->complexOut[s->fft_size / 2].im = 0.0; |
---|
| 495 | for (i = 1; i < spectrum->length - 1; i++) { |
---|
| 496 | s->complexOut[i].re = compspec->data[i]; |
---|
| 497 | s->complexOut[i].im = compspec->data[compspec->length - i]; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | #if HAVE_AUBIO_DOUBLE |
---|
| 501 | IppStatus status = ippsPhase_64fc(s->complexOut, spectrum->phas, spectrum->length); |
---|
| 502 | #else |
---|
| 503 | IppStatus status = ippsPhase_32fc(s->complexOut, spectrum->phas, spectrum->length); |
---|
| 504 | #endif |
---|
| 505 | if (status != ippStsNoErr) { |
---|
| 506 | AUBIO_ERR("fft: failed to extract phase from fft. IPP error: %d\n", status); |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | #else // NOT using Intel IPP |
---|
[729a3c0] | 510 | uint_t i; |
---|
[d95ff38] | 511 | if (compspec->data[0] < 0) { |
---|
| 512 | spectrum->phas[0] = PI; |
---|
| 513 | } else { |
---|
| 514 | spectrum->phas[0] = 0.; |
---|
| 515 | } |
---|
[729a3c0] | 516 | for (i=1; i < spectrum->length - 1; i++) { |
---|
| 517 | spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i], |
---|
| 518 | compspec->data[i]); |
---|
[d95ff38] | 519 | } |
---|
| 520 | if (compspec->data[compspec->length/2] < 0) { |
---|
| 521 | spectrum->phas[spectrum->length - 1] = PI; |
---|
| 522 | } else { |
---|
| 523 | spectrum->phas[spectrum->length - 1] = 0.; |
---|
[aadd27a] | 524 | } |
---|
[986131d] | 525 | #endif |
---|
[f88a326] | 526 | } |
---|
| 527 | |
---|
[986131d] | 528 | void aubio_fft_get_norm(aubio_fft_t *s, const fvec_t * compspec, cvec_t * spectrum) { |
---|
[729a3c0] | 529 | uint_t i = 0; |
---|
[d95ff38] | 530 | spectrum->norm[0] = ABS(compspec->data[0]); |
---|
[729a3c0] | 531 | for (i=1; i < spectrum->length - 1; i++) { |
---|
| 532 | spectrum->norm[i] = SQRT(SQR(compspec->data[i]) |
---|
| 533 | + SQR(compspec->data[compspec->length - i]) ); |
---|
[a47cd35] | 534 | } |
---|
[729a3c0] | 535 | spectrum->norm[spectrum->length-1] = |
---|
[d95ff38] | 536 | ABS(compspec->data[compspec->length/2]); |
---|
[f88a326] | 537 | } |
---|
| 538 | |
---|
[986131d] | 539 | void aubio_fft_get_imag(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) { |
---|
[729a3c0] | 540 | uint_t i; |
---|
| 541 | for (i = 1; i < ( compspec->length + 1 ) / 2 /*- 1 + 1*/; i++) { |
---|
| 542 | compspec->data[compspec->length - i] = |
---|
| 543 | spectrum->norm[i]*SIN(spectrum->phas[i]); |
---|
[a47cd35] | 544 | } |
---|
[f88a326] | 545 | } |
---|
| 546 | |
---|
[986131d] | 547 | void aubio_fft_get_real(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) { |
---|
[729a3c0] | 548 | uint_t i; |
---|
| 549 | for (i = 0; i < compspec->length / 2 + 1; i++) { |
---|
[152ed05] | 550 | compspec->data[i] = |
---|
[729a3c0] | 551 | spectrum->norm[i]*COS(spectrum->phas[i]); |
---|
[aadd27a] | 552 | } |
---|
[f88a326] | 553 | } |
---|