Changeset c721874 for src/fft.c


Ignore:
Timestamp:
Nov 3, 2007, 7:03:04 PM (13 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/constantq, feature/pitchshift, feature/pydocstrings, feature/timestretch, master, pitchshift, sampler, timestretch, yinfft+
Children:
d4a6eb9
Parents:
1da7e08 (diff), 2d8880d5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

merge changes from banane, more fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/fft.c

    r1da7e08 rc721874  
    2424
    2525#if FFTW3F_SUPPORT
    26 #define fftw_malloc             fftwf_malloc
    27 #define fftw_free               fftwf_free
    28 #define fftw_execute            fftwf_execute
    29 #define fftw_plan_dft_r2c_1d    fftwf_plan_dft_r2c_1d
    30 #define fftw_plan_dft_c2r_1d    fftwf_plan_dft_c2r_1d
    31 #define fftw_plan_r2r_1d      fftwf_plan_r2r_1d
    32 #define fftw_plan               fftwf_plan
    33 #define fftw_destroy_plan       fftwf_destroy_plan
     26#define fftw_malloc            fftwf_malloc
     27#define fftw_free              fftwf_free
     28#define fftw_execute           fftwf_execute
     29#define fftw_plan_dft_r2c_1d   fftwf_plan_dft_r2c_1d
     30#define fftw_plan_dft_c2r_1d   fftwf_plan_dft_c2r_1d
     31#define fftw_plan_r2r_1d       fftwf_plan_r2r_1d
     32#define fftw_plan              fftwf_plan
     33#define fftw_destroy_plan      fftwf_destroy_plan
    3434#endif
    3535
     
    4141
    4242struct _aubio_fft_t {
    43         uint_t fft_size;
    44         uint_t channels;
    45         real_t          *in, *out;
    46         fft_data_t      *specdata;
    47         fftw_plan       pfw, pbw;
     43  uint_t fft_size;
     44  uint_t channels;
     45  real_t    *in, *out;
     46  fft_data_t   *specdata;
     47  fftw_plan   pfw, pbw;
    4848};
    4949
     
    5151
    5252aubio_fft_t * new_aubio_fft(uint_t size) {
    53         aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
    54         /* allocate memory */
    55         s->in       = AUBIO_ARRAY(real_t,size);
    56         s->out      = AUBIO_ARRAY(real_t,size);
    57         s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*size);
    58         /* create plans */
     53  aubio_fft_t * s = AUBIO_NEW(aubio_fft_t);
     54  /* allocate memory */
     55  s->in       = AUBIO_ARRAY(real_t,size);
     56  s->out      = AUBIO_ARRAY(real_t,size);
     57  /* create plans */
    5958#ifdef HAVE_COMPLEX_H
    60         s->pfw = fftw_plan_dft_r2c_1d(size, s->in,  s->specdata, FFTW_ESTIMATE);
    61         s->pbw = fftw_plan_dft_c2r_1d(size, s->specdata, s->out, FFTW_ESTIMATE);
     59  s->fft_size = size/2+1;
     60  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
     61  s->pfw = fftw_plan_dft_r2c_1d(size, s->in,  s->specdata, FFTW_ESTIMATE);
     62  s->pbw = fftw_plan_dft_c2r_1d(size, s->specdata, s->out, FFTW_ESTIMATE);
    6263#else
    63         s->pfw = fftw_plan_r2r_1d(size, s->in,  s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
    64         s->pbw = fftw_plan_r2r_1d(size, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
     64  s->fft_size = size;
     65  s->specdata = (fft_data_t*)fftw_malloc(sizeof(fft_data_t)*s->fft_size);
     66  s->pfw = fftw_plan_r2r_1d(size, s->in,  s->specdata, FFTW_R2HC, FFTW_ESTIMATE);
     67  s->pbw = fftw_plan_r2r_1d(size, s->specdata, s->out, FFTW_HC2R, FFTW_ESTIMATE);
    6568#endif
    66         return s;
     69  return s;
    6770}
    6871
    6972void del_aubio_fft(aubio_fft_t * s) {
    70         /* destroy data */
    71         fftw_destroy_plan(s->pfw);
    72         fftw_destroy_plan(s->pbw);
    73         fftw_free(s->specdata);
    74         AUBIO_FREE(s->out);
    75         AUBIO_FREE(s->in );
    76         AUBIO_FREE(s);
     73  /* destroy data */
     74  fftw_destroy_plan(s->pfw);
     75  fftw_destroy_plan(s->pbw);
     76  fftw_free(s->specdata);
     77  AUBIO_FREE(s->out);
     78  AUBIO_FREE(s->in );
     79  AUBIO_FREE(s);
    7780}
    7881
    7982void aubio_fft_do(const aubio_fft_t * s,
    80                 const smpl_t * data, fft_data_t * spectrum,
    81                 const uint_t size) {
    82         uint_t i;
    83         for (i=0;i<size;i++) s->in[i] = data[i];
    84         fftw_execute(s->pfw);
    85         for (i=0;i<size;i++) spectrum[i] = s->specdata[i];
     83    const smpl_t * data, fft_data_t * spectrum, const uint_t size) {
     84  uint_t i;
     85  for (i=0;i<size;i++) s->in[i] = data[i];
     86  fftw_execute(s->pfw);
     87  for (i=0; i < s->fft_size; i++) spectrum[i] = s->specdata[i];
    8688}
    8789
    8890void aubio_fft_rdo(const aubio_fft_t * s,
    89                 const fft_data_t * spectrum,
    90                 smpl_t * data,
    91                 const uint_t size) {
    92         uint_t i;
    93         const smpl_t renorm = 1./(smpl_t)size;
    94         for (i=0;i<size;i++) s->specdata[i] = spectrum[i];
    95         fftw_execute(s->pbw);
    96         for (i=0;i<size;i++) data[i] = s->out[i]*renorm;
     91    const fft_data_t * spectrum, smpl_t * data, const uint_t size) {
     92  uint_t i;
     93  const smpl_t renorm = 1./(smpl_t)size;
     94  for (i=0; i < s->fft_size; i++) s->specdata[i] = spectrum[i];
     95  fftw_execute(s->pbw);
     96  for (i=0;i<size;i++) data[i] = s->out[i]*renorm;
    9797}
    9898
     
    100100
    101101void aubio_fft_getnorm(smpl_t * norm, fft_data_t * spectrum, uint_t size) {
    102         uint_t i;
    103         for (i=0;i<size/2+1;i++) norm[i] = ABSC(spectrum[i]);
    104         //for (i=0;i<size/2+1;i++) AUBIO_DBG("%f\n", norm[i]);
     102  uint_t i;
     103  for (i=0;i<size/2+1;i++) norm[i] = ABSC(spectrum[i]);
    105104}
    106105
    107106void aubio_fft_getphas(smpl_t * phas, fft_data_t * spectrum, uint_t size) {
    108         uint_t i;
    109         for (i=0;i<size/2+1;i++) phas[i] = ARGC(spectrum[i]);
    110         //for (i=0;i<size/2+1;i++) AUBIO_DBG("%f\n", phas[i]);
     107  uint_t i;
     108  for (i=0;i<size/2+1;i++) phas[i] = ARGC(spectrum[i]);
    111109}
    112110
     
    122120
    123121void aubio_fft_getnorm(smpl_t * norm, fft_data_t * spectrum, uint_t size) {
    124         uint_t i;
    125   norm[0] = -spectrum[0];
    126         for (i=1;i<size/2+1;i++) norm[i] = SQRT(SQR(spectrum[i]) + SQR(spectrum[size-i]));
    127         //for (i=0;i<size/2+1;i++) AUBIO_DBG("%f\n", norm[i]);
     122  uint_t i;
     123  norm[0] = SQR(spectrum[0]);
     124  for (i=1;i<size/2;i++) norm[i] = (SQR(spectrum[i]) + SQR(spectrum[size-i]));
     125  norm[size/2] = SQR(spectrum[size/2]);
    128126}
    129127
    130128void aubio_fft_getphas(smpl_t * phas, fft_data_t * spectrum, uint_t size) {
    131         uint_t i;
    132   phas[0] = PI;
    133         for (i=1;i<size/2+1;i++) phas[i] = atan2f(spectrum[size-i] , spectrum[i]);
    134         //for (i=0;i<size/2+1;i++) AUBIO_DBG("%f\n", phas[i]);
     129  uint_t i;
     130  phas[0] = 0;
     131  for (i=1;i<size/2+1;i++) phas[i] = atan2f(spectrum[size-i] , spectrum[i]);
     132  phas[size/2] = 0;
    135133}
    136134
     
    156154
    157155aubio_mfft_t * new_aubio_mfft(uint_t winsize, uint_t channels){
    158         uint_t i;
    159         aubio_mfft_t * fft = AUBIO_NEW(aubio_mfft_t);
    160         fft->winsize       = winsize;
    161         fft->channels      = channels;
    162         fft->fft           = new_aubio_fft(winsize);
    163         fft->spec          = AUBIO_ARRAY(fft_data_t*,channels);
    164         for (i=0; i < channels; i++)
    165                 fft->spec[i] = AUBIO_ARRAY(fft_data_t,winsize);
    166         return fft;
     156  uint_t i;
     157  aubio_mfft_t * fft = AUBIO_NEW(aubio_mfft_t);
     158  fft->winsize       = winsize;
     159  fft->channels      = channels;
     160  fft->fft           = new_aubio_fft(winsize);
     161  fft->spec          = AUBIO_ARRAY(fft_data_t*,channels);
     162  for (i=0; i < channels; i++)
     163    fft->spec[i] = AUBIO_ARRAY(fft_data_t,winsize);
     164  return fft;
    167165}
    168166
    169167/* execute stft */
    170168void aubio_mfft_do (aubio_mfft_t * fft,fvec_t * in,cvec_t * fftgrain){
    171         uint_t i=0;
    172         /* execute stft */
    173         for (i=0; i < fft->channels; i++) {
    174                 aubio_fft_do (fft->fft,in->data[i],fft->spec[i],fft->winsize);
    175                 /* put norm and phase into fftgrain */
    176                 aubio_fft_getnorm(fftgrain->norm[i], fft->spec[i], fft->winsize);
    177                 aubio_fft_getphas(fftgrain->phas[i], fft->spec[i], fft->winsize);
    178         }
     169  uint_t i=0;
     170  /* execute stft */
     171  for (i=0; i < fft->channels; i++) {
     172    aubio_fft_do (fft->fft,in->data[i],fft->spec[i],fft->winsize);
     173    /* put norm and phase into fftgrain */
     174    aubio_fft_getnorm(fftgrain->norm[i], fft->spec[i], fft->winsize);
     175    aubio_fft_getphas(fftgrain->phas[i], fft->spec[i], fft->winsize);
     176  }
    179177}
    180178
    181179/* execute inverse fourier transform */
    182180void aubio_mfft_rdo(aubio_mfft_t * fft,cvec_t * fftgrain, fvec_t * out){
    183         uint_t i=0;
    184         for (i=0; i < fft->channels; i++) {
    185                 aubio_fft_getspectrum(fft->spec[i],fftgrain->norm[i],fftgrain->phas[i],fft->winsize);
    186                 aubio_fft_rdo(fft->fft,fft->spec[i],out->data[i],fft->winsize);
    187         }
     181  uint_t i=0;
     182  for (i=0; i < fft->channels; i++) {
     183    aubio_fft_getspectrum(fft->spec[i],fftgrain->norm[i],fftgrain->phas[i],fft->winsize);
     184    aubio_fft_rdo(fft->fft,fft->spec[i],out->data[i],fft->winsize);
     185  }
    188186}
    189187
    190188void del_aubio_mfft(aubio_mfft_t * fft) {
    191         uint_t i;
    192         for (i=0; i < fft->channels; i++)
    193                 AUBIO_FREE(fft->spec[i]);
    194         AUBIO_FREE(fft->spec);
    195         del_aubio_fft(fft->fft);
    196         AUBIO_FREE(fft);       
     189  uint_t i;
     190  for (i=0; i < fft->channels; i++)
     191    AUBIO_FREE(fft->spec[i]);
     192  AUBIO_FREE(fft->spec);
     193  del_aubio_fft(fft->fft);
     194  AUBIO_FREE(fft);       
    197195}
Note: See TracChangeset for help on using the changeset viewer.