source: src/spectral/fft.c @ 4b943729

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 4b943729 was 95f5c83, checked in by Paul Brossier <piem@piem.org>, 7 years ago

src/spectral/fft.c: remove trailing space

  • Property mode set to 100644
File size: 18.0 KB
Line 
1/*
2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
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.
10
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/>.
18
19*/
20
21#include "aubio_priv.h"
22#include "fvec.h"
23#include "cvec.h"
24#include "mathutils.h"
25#include "spectral/fft.h"
26
27#ifdef HAVE_FFTW3             // using FFTW3
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>
31#include <pthread.h>
32
33#ifdef HAVE_COMPLEX_H
34#ifdef HAVE_FFTW3F
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
42#ifdef HAVE_FFTW3F
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 */
52typedef FFTW_TYPE fft_data_t;
53
54#ifdef HAVE_FFTW3F
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
63#endif
64
65#ifdef HAVE_FFTW3F
66#if HAVE_AUBIO_DOUBLE
67#error "Using aubio in double precision with fftw3 in single precision"
68#endif /* HAVE_AUBIO_DOUBLE */
69#define real_t float
70#elif defined (HAVE_FFTW3) /* HAVE_FFTW3F */
71#if !HAVE_AUBIO_DOUBLE
72#error "Using aubio in single precision with fftw3 in double precision"
73#endif /* HAVE_AUBIO_DOUBLE */
74#define real_t double
75#endif /* HAVE_FFTW3F */
76
77// a global mutex for FFTW thread safety
78pthread_mutex_t aubio_fftw_mutex = PTHREAD_MUTEX_INITIALIZER;
79
80#elif defined HAVE_ACCELERATE        // using ACCELERATE
81// https://developer.apple.com/library/mac/#documentation/Accelerate/Reference/vDSPRef/Reference/reference.html
82#include <Accelerate/Accelerate.h>
83
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
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
121// let's use ooura instead
122extern void aubio_ooura_rdft(int, int, smpl_t *, int *, smpl_t *);
123
124#endif
125
126struct _aubio_fft_t {
127  uint_t winsize;
128  uint_t fft_size;
129
130#ifdef HAVE_FFTW3             // using FFTW3
131  real_t *in, *out;
132  fftw_plan pfw, pbw;
133  fft_data_t * specdata; /* complex spectral data */
134
135#elif defined HAVE_ACCELERATE  // using ACCELERATE
136  int log2fftsize;
137  aubio_FFTSetup fftSetup;
138  aubio_DSPSplitComplex spec;
139  smpl_t *in, *out;
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
155#else                         // using OOURA
156  smpl_t *in, *out;
157  smpl_t *w;
158  int *ip;
159#endif /* using OOURA */
160
161  fvec_t * compspec;
162};
163
164aubio_fft_t * new_aubio_fft (uint_t winsize) {
165  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
166  if ((sint_t)winsize < 2) {
167    AUBIO_ERR("fft: got winsize %d, but can not be < 2\n", winsize);
168    goto beach;
169  }
170
171#ifdef HAVE_FFTW3
172  uint_t i;
173  s->winsize  = winsize;
174  /* allocate memory */
175  s->in       = AUBIO_ARRAY(real_t,winsize);
176  s->out      = AUBIO_ARRAY(real_t,winsize);
177  s->compspec = new_fvec(winsize);
178  /* create plans */
179  pthread_mutex_lock(&aubio_fftw_mutex);
180#ifdef HAVE_COMPLEX_H
181  s->fft_size = winsize/2 + 1;
182  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
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);
185#else
186  s->fft_size = winsize;
187  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
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);
190#endif
191  pthread_mutex_unlock(&aubio_fftw_mutex);
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  }
199
200#elif defined HAVE_ACCELERATE  // using ACCELERATE
201  s->winsize = winsize;
202  s->fft_size = winsize;
203  s->compspec = new_fvec(winsize);
204  s->log2fftsize = aubio_power_of_two_order(s->fft_size);
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);
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
259#else                         // using OOURA
260  if (aubio_is_power_of_two(winsize) != 1) {
261    AUBIO_ERR("fft: can only create with sizes power of two, requested %d,"
262        " try recompiling aubio with --enable-fftw3\n", winsize);
263    goto beach;
264  }
265  s->winsize = winsize;
266  s->fft_size = winsize / 2 + 1;
267  s->compspec = new_fvec(winsize);
268  s->in    = AUBIO_ARRAY(smpl_t, s->winsize);
269  s->out   = AUBIO_ARRAY(smpl_t, s->winsize);
270  s->ip    = AUBIO_ARRAY(int   , s->fft_size);
271  s->w     = AUBIO_ARRAY(smpl_t, s->fft_size);
272  s->ip[0] = 0;
273#endif /* using OOURA */
274
275  return s;
276
277beach:
278  AUBIO_FREE(s);
279  return NULL;
280}
281
282void del_aubio_fft(aubio_fft_t * s) {
283  /* destroy data */
284#ifdef HAVE_FFTW3             // using FFTW3
285  pthread_mutex_lock(&aubio_fftw_mutex);
286  fftw_destroy_plan(s->pfw);
287  fftw_destroy_plan(s->pbw);
288  fftw_free(s->specdata);
289  pthread_mutex_unlock(&aubio_fftw_mutex);
290
291#elif defined HAVE_ACCELERATE // using ACCELERATE
292  AUBIO_FREE(s->spec.realp);
293  AUBIO_FREE(s->spec.imagp);
294  aubio_vDSP_destroy_fftsetup(s->fftSetup);
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
302#else                         // using OOURA
303  AUBIO_FREE(s->w);
304  AUBIO_FREE(s->ip);
305#endif
306
307  del_fvec(s->compspec);
308  AUBIO_FREE(s->in);
309  AUBIO_FREE(s->out);
310  AUBIO_FREE(s);
311}
312
313void aubio_fft_do(aubio_fft_t * s, const fvec_t * input, cvec_t * spectrum) {
314  aubio_fft_do_complex(s, input, s->compspec);
315  aubio_fft_get_spectrum(s, s->compspec, spectrum);
316}
317
318void aubio_fft_rdo(aubio_fft_t * s, const cvec_t * spectrum, fvec_t * output) {
319  aubio_fft_get_realimag(s, spectrum, s->compspec);
320  aubio_fft_rdo_complex(s, s->compspec, output);
321}
322
323void aubio_fft_do_complex(aubio_fft_t * s, const fvec_t * input, fvec_t * compspec) {
324  uint_t i;
325#ifndef HAVE_MEMCPY_HACKS
326  for (i=0; i < s->winsize; i++) {
327    s->in[i] = input->data[i];
328  }
329#else
330  memcpy(s->in, input->data, s->winsize * sizeof(smpl_t));
331#endif /* HAVE_MEMCPY_HACKS */
332
333#ifdef HAVE_FFTW3             // using FFTW3
334  fftw_execute(s->pfw);
335#ifdef HAVE_COMPLEX_H
336  compspec->data[0] = REAL(s->specdata[0]);
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]);
340  }
341  compspec->data[s->fft_size-1] = REAL(s->specdata[s->fft_size-1]);
342#else /* HAVE_COMPLEX_H  */
343  for (i = 0; i < s->fft_size; i++) {
344    compspec->data[i] = s->specdata[i];
345  }
346#endif /* HAVE_COMPLEX_H */
347
348#elif defined HAVE_ACCELERATE // using ACCELERATE
349  // convert real data to even/odd format used in vDSP
350  aubio_vDSP_ctoz((aubio_DSPComplex*)s->in, 2, &s->spec, 1, s->fft_size/2);
351  // compute the FFT
352  aubio_vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_FORWARD);
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.;
362  aubio_vDSP_vsmul(compspec->data, 1, &scale, compspec->data, 1, s->fft_size);
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
386#else                         // using OOURA
387  aubio_ooura_rdft(s->winsize, 1, s->in, s->ip, s->w);
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  }
394#endif /* using OOURA */
395}
396
397void aubio_fft_rdo_complex(aubio_fft_t * s, const fvec_t * compspec, fvec_t * output) {
398  uint_t i;
399#ifdef HAVE_FFTW3
400  const smpl_t renorm = 1./(smpl_t)s->winsize;
401#ifdef HAVE_COMPLEX_H
402  s->specdata[0] = compspec->data[0];
403  for (i=1; i < s->fft_size - 1; i++) {
404    s->specdata[i] = compspec->data[i] +
405      I * compspec->data[compspec->length - i];
406  }
407  s->specdata[s->fft_size - 1] = compspec->data[s->fft_size - 1];
408#else
409  for (i=0; i < s->fft_size; i++) {
410    s->specdata[i] = compspec->data[i];
411  }
412#endif
413  fftw_execute(s->pbw);
414  for (i = 0; i < output->length; i++) {
415    output->data[i] = s->out[i]*renorm;
416  }
417
418#elif defined HAVE_ACCELERATE // using ACCELERATE
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
428  aubio_vDSP_ctoz((aubio_DSPComplex*)s->out, 2, &s->spec, 1, s->fft_size/2);
429  // compute the FFT
430  aubio_vDSP_fft_zrip(s->fftSetup, &s->spec, 1, s->log2fftsize, FFT_INVERSE);
431  // convert result to real output
432  aubio_vDSP_ztoc(&s->spec, 1, (aubio_DSPComplex*)output->data, 2, s->fft_size/2);
433  // apply scaling
434  smpl_t scale = 1.0 / s->winsize;
435  aubio_vDSP_vsmul(output->data, 1, &scale, output->data, 1, s->fft_size);
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, 2.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, 2.0f / s->winsize, output->data, s->fft_size);
458#endif /* HAVE_AUBIO_DOUBLE */
459
460#else                         // using OOURA
461  smpl_t scale = 2.0 / s->winsize;
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];
467  }
468  aubio_ooura_rdft(s->winsize, -1, s->out, s->ip, s->w);
469  for (i=0; i < s->winsize; i++) {
470    output->data[i] = s->out[i] * scale;
471  }
472#endif
473}
474
475void 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);
478}
479
480void 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);
483}
484
485void 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
510  uint_t i;
511  if (compspec->data[0] < 0) {
512    spectrum->phas[0] = PI;
513  } else {
514    spectrum->phas[0] = 0.;
515  }
516  for (i=1; i < spectrum->length - 1; i++) {
517    spectrum->phas[i] = ATAN2(compspec->data[compspec->length-i],
518        compspec->data[i]);
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.;
524  }
525#endif
526}
527
528void aubio_fft_get_norm(aubio_fft_t *s, const fvec_t * compspec, cvec_t * spectrum) {
529  uint_t i = 0;
530  spectrum->norm[0] = ABS(compspec->data[0]);
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]) );
534  }
535  spectrum->norm[spectrum->length-1] =
536    ABS(compspec->data[compspec->length/2]);
537}
538
539void aubio_fft_get_imag(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) {
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]);
544  }
545}
546
547void aubio_fft_get_real(aubio_fft_t *s, const cvec_t * spectrum, fvec_t * compspec) {
548  uint_t i;
549  for (i = 0; i < compspec->length / 2 + 1; i++) {
550    compspec->data[i] =
551      spectrum->norm[i]*COS(spectrum->phas[i]);
552  }
553}
Note: See TracBrowser for help on using the repository browser.