Changes in / [e680926:db3eb5c]


Ignore:
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/spectral/mfcc.c

    re680926 rdb3eb5c  
    3232#include "spectral/mfcc.h"
    3333
    34 #ifdef HAVE_NOOPT
    35 #define HAVE_SLOW_DCT 1
    36 #endif
    37 
    3834/** Internal structure for mfcc object */
    3935
     
    4642  aubio_filterbank_t *fb;   /** filter bank */
    4743  fvec_t *in_dct;           /** input buffer for dct * [fb->n_filters] */
    48 #if defined(HAVE_SLOW_DCT)
    49   fmat_t *dct_coeffs;       /** DCT transform n_filters * n_coeffs */
    50 #else
    51   aubio_dct_t *dct;
    52   fvec_t *output;
    53 #endif
     44  aubio_dct_t *dct;         /** dct object */
     45  fvec_t *output;           /** dct output */
    5446  smpl_t scale;
    5547};
     
    6355  /* allocate space for mfcc object */
    6456  aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t);
    65 #if defined(HAVE_SLOW_DCT)
    66   smpl_t scaling;
    6757
    68   uint_t i, j;
    69 #endif
     58  if ((sint_t)n_coefs <= 0) {
     59    AUBIO_ERR("mfcc: n_coefs should be > 0, got %d\n", n_coefs);
     60    goto failure;
     61  }
     62  if ((sint_t)samplerate <= 0) {
     63    AUBIO_ERR("mfcc: samplerate should be > 0, got %d\n", samplerate);
     64    goto failure;
     65  }
    7066
    7167  mfcc->win_s = win_s;
     
    7672  /* filterbank allocation */
    7773  mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s);
     74
     75  if (!mfcc->fb)
     76    goto failure;
     77
    7878  if (n_filters == 40)
    7979    aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate);
     
    8585  mfcc->in_dct = new_fvec (n_filters);
    8686
    87 #if defined(HAVE_SLOW_DCT)
    88   mfcc->dct_coeffs = new_fmat (n_coefs, n_filters);
    89 
    90   /* compute DCT transform dct_coeffs[j][i] as
    91      cos ( j * (i+.5) * PI / n_filters ) */
    92   scaling = 1. / SQRT (n_filters / 2.);
    93   for (i = 0; i < n_filters; i++) {
    94     for (j = 0; j < n_coefs; j++) {
    95       mfcc->dct_coeffs->data[j][i] =
    96           scaling * COS (j * (i + 0.5) * PI / n_filters);
    97     }
    98     mfcc->dct_coeffs->data[0][i] *= SQRT (2.) / 2.;
    99   }
    100 #else
    10187  mfcc->dct = new_aubio_dct (n_filters);
    10288  mfcc->output = new_fvec (n_filters);
    103 #endif
     89
     90  if (!mfcc->in_dct || !mfcc->dct || !mfcc->output)
     91    goto failure;
    10492
    10593  mfcc->scale = 1.;
    10694
    10795  return mfcc;
     96
     97failure:
     98  del_aubio_mfcc(mfcc);
     99  return NULL;
    108100}
    109101
     
    111103del_aubio_mfcc (aubio_mfcc_t * mf)
    112104{
    113 
    114   /* delete filterbank */
    115   del_aubio_filterbank (mf->fb);
    116 
    117   /* delete buffers */
    118   del_fvec (mf->in_dct);
    119 #if defined(HAVE_SLOW_DCT)
    120   del_fmat (mf->dct_coeffs);
    121 #else
    122   del_aubio_dct (mf->dct);
    123   del_fvec (mf->output);
    124 #endif
    125 
    126   /* delete mfcc object */
     105  if (mf->fb)
     106    del_aubio_filterbank (mf->fb);
     107  if (mf->in_dct)
     108    del_fvec (mf->in_dct);
     109  if (mf->dct)
     110    del_aubio_dct (mf->dct);
     111  if (mf->output)
     112    del_fvec (mf->output);
    127113  AUBIO_FREE (mf);
    128114}
     
    132118aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out)
    133119{
    134 #ifndef HAVE_SLOW_DCT
    135120  fvec_t tmp;
    136 #endif
    137121
    138122  /* compute filterbank */
     
    145129
    146130  /* compute mfccs */
    147 #if defined(HAVE_SLOW_DCT)
    148   fmat_vecmul(mf->dct_coeffs, mf->in_dct, out);
    149 #else
    150131  aubio_dct_do(mf->dct, mf->in_dct, mf->output);
    151132  // copy only first n_coeffs elements
     
    154135  tmp.length = out->length;
    155136  fvec_copy(&tmp, out);
    156 #endif
    157137
    158138  return;
  • tests/src/spectral/test-mfcc.c

    re680926 rdb3eb5c  
    55  uint_t win_s = 512; // fft size
    66  uint_t n_filters = 40; // number of filters
    7   uint_t n_coefs = 13; // number of coefficients
     7  uint_t n_coeffs = 13; // number of coefficients
    88  smpl_t samplerate = 16000.; // samplerate
    99  cvec_t *in = new_cvec (win_s); // input buffer
    10   fvec_t *out = new_fvec (n_coefs); // output coefficients
     10  fvec_t *out = new_fvec (n_coeffs); // output coefficients
     11
     12  if (new_aubio_mfcc(    0, n_filters, n_coeffs, samplerate)) return 1;
     13  if (new_aubio_mfcc(win_s,         0, n_coeffs, samplerate)) return 1;
     14  if (new_aubio_mfcc(win_s, n_filters,        0, samplerate)) return 1;
     15  if (new_aubio_mfcc(win_s, n_filters, n_coeffs,          0)) return 1;
    1116
    1217  // create mfcc object
    13   aubio_mfcc_t *o = new_aubio_mfcc (win_s, n_filters, n_coefs, samplerate);
     18  aubio_mfcc_t *o = new_aubio_mfcc (win_s, n_filters, n_coeffs, samplerate);
    1419
    1520  cvec_norm_set_all (in, 1.);
Note: See TracChangeset for help on using the changeset viewer.