Ignore:
Timestamp:
Dec 5, 2018, 10:34:39 PM (5 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/cnn, feature/crepe, feature/pitchshift, feature/timestretch, fix/ffmpeg5, master
Children:
283a619a
Parents:
5b46bc3 (diff), f19db54 (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 branch 'master' into feature/pitchshift

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/spectral/mfcc.c

    r5b46bc3 r633400d  
    2929#include "spectral/filterbank.h"
    3030#include "spectral/filterbank_mel.h"
     31#include "spectral/dct.h"
    3132#include "spectral/mfcc.h"
    3233
     
    3738  uint_t win_s;             /** grain length */
    3839  uint_t samplerate;        /** sample rate (needed?) */
    39   uint_t n_filters;         /** number of  *filters */
     40  uint_t n_filters;         /** number of filters */
    4041  uint_t n_coefs;           /** number of coefficients (<= n_filters/2 +1) */
    4142  aubio_filterbank_t *fb;   /** filter bank */
    4243  fvec_t *in_dct;           /** input buffer for dct * [fb->n_filters] */
    43   fmat_t *dct_coeffs;       /** DCT transform n_filters * n_coeffs */
     44  aubio_dct_t *dct;         /** dct object */
     45  fvec_t *output;           /** dct output */
     46  smpl_t scale;
    4447};
    4548
     
    5255  /* allocate space for mfcc object */
    5356  aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t);
    54   smpl_t scaling;
    5557
    56   uint_t i, j;
     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  }
    5766
    5867  mfcc->win_s = win_s;
     
    6372  /* filterbank allocation */
    6473  mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s);
    65   aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate);
     74
     75  if (!mfcc->fb)
     76    goto failure;
     77
     78  if (n_filters == 40)
     79    aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate);
     80  else
     81    aubio_filterbank_set_mel_coeffs(mfcc->fb, samplerate,
     82        0, samplerate/2.);
    6683
    6784  /* allocating buffers */
    6885  mfcc->in_dct = new_fvec (n_filters);
    6986
    70   mfcc->dct_coeffs = new_fmat (n_coefs, n_filters);
     87  mfcc->dct = new_aubio_dct (n_filters);
     88  mfcc->output = new_fvec (n_filters);
    7189
    72   /* compute DCT transform dct_coeffs[j][i] as
    73      cos ( j * (i+.5) * PI / n_filters ) */
    74   scaling = 1. / SQRT (n_filters / 2.);
    75   for (i = 0; i < n_filters; i++) {
    76     for (j = 0; j < n_coefs; j++) {
    77       mfcc->dct_coeffs->data[j][i] =
    78           scaling * COS (j * (i + 0.5) * PI / n_filters);
    79     }
    80     mfcc->dct_coeffs->data[0][i] *= SQRT (2.) / 2.;
    81   }
     90  if (!mfcc->in_dct || !mfcc->dct || !mfcc->output)
     91    goto failure;
     92
     93  mfcc->scale = 1.;
    8294
    8395  return mfcc;
     96
     97failure:
     98  del_aubio_mfcc(mfcc);
     99  return NULL;
    84100}
    85101
     
    87103del_aubio_mfcc (aubio_mfcc_t * mf)
    88104{
    89 
    90   /* delete filterbank */
    91   del_aubio_filterbank (mf->fb);
    92 
    93   /* delete buffers */
    94   del_fvec (mf->in_dct);
    95   del_fmat (mf->dct_coeffs);
    96 
    97   /* 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);
    98113  AUBIO_FREE (mf);
    99114}
     
    103118aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out)
    104119{
     120  fvec_t tmp;
     121
    105122  /* compute filterbank */
    106123  aubio_filterbank_do (mf->fb, in, mf->in_dct);
     
    109126  fvec_log10 (mf->in_dct);
    110127
    111   /* raise power */
    112   //fvec_pow (mf->in_dct, 3.);
     128  if (mf->scale != 1) fvec_mul (mf->in_dct, mf->scale);
    113129
    114130  /* compute mfccs */
    115   fmat_vecmul(mf->dct_coeffs, mf->in_dct, out);
     131  aubio_dct_do(mf->dct, mf->in_dct, mf->output);
     132  // copy only first n_coeffs elements
     133  // TODO assert mf->output->length == n_coeffs
     134  tmp.data = mf->output->data;
     135  tmp.length = out->length;
     136  fvec_copy(&tmp, out);
    116137
    117138  return;
    118139}
     140
     141uint_t aubio_mfcc_set_power (aubio_mfcc_t *mf, smpl_t power)
     142{
     143  return aubio_filterbank_set_power(mf->fb, power);
     144}
     145
     146smpl_t aubio_mfcc_get_power (aubio_mfcc_t *mf)
     147{
     148  return aubio_filterbank_get_power(mf->fb);
     149}
     150
     151uint_t aubio_mfcc_set_scale (aubio_mfcc_t *mf, smpl_t scale)
     152{
     153  mf->scale = scale;
     154  return AUBIO_OK;
     155}
     156
     157smpl_t aubio_mfcc_get_scale (aubio_mfcc_t *mf)
     158{
     159  return mf->scale;
     160}
     161
     162uint_t aubio_mfcc_set_mel_coeffs (aubio_mfcc_t *mf, smpl_t freq_min,
     163    smpl_t freq_max)
     164{
     165  return aubio_filterbank_set_mel_coeffs(mf->fb, mf->samplerate,
     166      freq_min, freq_max);
     167}
     168
     169uint_t aubio_mfcc_set_mel_coeffs_htk (aubio_mfcc_t *mf, smpl_t freq_min,
     170    smpl_t freq_max)
     171{
     172  return aubio_filterbank_set_mel_coeffs_htk(mf->fb, mf->samplerate,
     173      freq_min, freq_max);
     174}
     175
     176uint_t aubio_mfcc_set_mel_coeffs_slaney (aubio_mfcc_t *mf)
     177{
     178  return aubio_filterbank_set_mel_coeffs_slaney (mf->fb, mf->samplerate);
     179}
Note: See TracChangeset for help on using the changeset viewer.