Changes in / [0d07a2a:992e0ff]


Ignore:
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • python/ext/py-filterbank.c

    r0d07a2a r992e0ff  
    139139      &(self->freqs), samplerate);
    140140  if (err > 0) {
    141     PyErr_SetString (PyExc_ValueError,
    142         "error when running set_triangle_bands");
     141    if (PyErr_Occurred() == NULL) {
     142      PyErr_SetString (PyExc_ValueError, "error running set_triangle_bands");
     143    } else {
     144      // change the RuntimeError into ValueError
     145      PyObject *type, *value, *traceback;
     146      PyErr_Fetch(&type, &value, &traceback);
     147      PyErr_Restore(PyExc_ValueError, value, traceback);
     148    }
    143149    return NULL;
    144150  }
     
    158164  err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate);
    159165  if (err > 0) {
    160     PyErr_SetString (PyExc_ValueError,
    161         "error when running set_mel_coeffs_slaney");
     166    if (PyErr_Occurred() == NULL) {
     167      PyErr_SetString (PyExc_ValueError, "error running set_mel_coeffs_slaney");
     168    } else {
     169      // change the RuntimeError into ValueError
     170      PyObject *type, *value, *traceback;
     171      PyErr_Fetch(&type, &value, &traceback);
     172      PyErr_Restore(PyExc_ValueError, value, traceback);
     173    }
     174    return NULL;
     175  }
     176  Py_RETURN_NONE;
     177}
     178
     179static PyObject *
     180Py_filterbank_set_mel_coeffs (Py_filterbank * self, PyObject *args)
     181{
     182  uint_t err = 0;
     183
     184  uint_t samplerate;
     185  smpl_t freq_min;
     186  smpl_t freq_max;
     187  if (!PyArg_ParseTuple (args, "I" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR,
     188        &samplerate, &freq_min, &freq_max)) {
     189    return NULL;
     190  }
     191
     192  err = aubio_filterbank_set_mel_coeffs (self->o, samplerate,
     193      freq_min, freq_max);
     194  if (err > 0) {
     195    if (PyErr_Occurred() == NULL) {
     196      PyErr_SetString (PyExc_ValueError, "error running set_mel_coeffs");
     197    } else {
     198      // change the RuntimeError into ValueError
     199      PyObject *type, *value, *traceback;
     200      PyErr_Fetch(&type, &value, &traceback);
     201      PyErr_Restore(PyExc_ValueError, value, traceback);
     202    }
     203    return NULL;
     204  }
     205  Py_RETURN_NONE;
     206}
     207
     208static PyObject *
     209Py_filterbank_set_mel_coeffs_htk (Py_filterbank * self, PyObject *args)
     210{
     211  uint_t err = 0;
     212
     213  uint_t samplerate;
     214  smpl_t freq_min;
     215  smpl_t freq_max;
     216  if (!PyArg_ParseTuple (args, "I" AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR,
     217        &samplerate, &freq_min, &freq_max)) {
     218    return NULL;
     219  }
     220
     221  err = aubio_filterbank_set_mel_coeffs_htk (self->o, samplerate,
     222      freq_min, freq_max);
     223  if (err > 0) {
     224    if (PyErr_Occurred() == NULL) {
     225      PyErr_SetString (PyExc_ValueError, "error running set_mel_coeffs_htk");
     226    } else {
     227      // change the RuntimeError into ValueError
     228      PyObject *type, *value, *traceback;
     229      PyErr_Fetch(&type, &value, &traceback);
     230      PyErr_Restore(PyExc_ValueError, value, traceback);
     231    }
    162232    return NULL;
    163233  }
     
    194264  return (PyObject *)PyAubio_CFmatToArray(
    195265      aubio_filterbank_get_coeffs (self->o) );
     266}
     267
     268static PyObject *
     269Py_filterbank_set_power(Py_filterbank *self, PyObject *args)
     270{
     271  uint_t playing;
     272
     273  if (!PyArg_ParseTuple (args, "I", &playing)) {
     274    return NULL;
     275  }
     276  if(aubio_filterbank_set_power (self->o, playing)) {
     277    if (PyErr_Occurred() == NULL) {
     278      PyErr_SetString (PyExc_ValueError,
     279          "error running filterbank.set_power");
     280    } else {
     281      // change the RuntimeError into ValueError
     282      PyObject *type, *value, *traceback;
     283      PyErr_Fetch(&type, &value, &traceback);
     284      PyErr_Restore(PyExc_ValueError, value, traceback);
     285    }
     286    return NULL;
     287  }
     288  Py_RETURN_NONE;
     289}
     290
     291static PyObject *
     292Py_filterbank_set_norm(Py_filterbank *self, PyObject *args)
     293{
     294  uint_t playing;
     295
     296  if (!PyArg_ParseTuple (args, "I", &playing)) {
     297    return NULL;
     298  }
     299  if(aubio_filterbank_set_norm (self->o, playing)) {
     300    if (PyErr_Occurred() == NULL) {
     301      PyErr_SetString (PyExc_ValueError,
     302          "error running filterbank.set_power");
     303    } else {
     304      // change the RuntimeError into ValueError
     305      PyObject *type, *value, *traceback;
     306      PyErr_Fetch(&type, &value, &traceback);
     307      PyErr_Restore(PyExc_ValueError, value, traceback);
     308    }
     309    return NULL;
     310  }
     311  Py_RETURN_NONE;
    196312}
    197313
     
    201317  {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney,
    202318    METH_VARARGS, "set coefficients of filterbank as in Auditory Toolbox"},
     319  {"set_mel_coeffs", (PyCFunction) Py_filterbank_set_mel_coeffs,
     320    METH_VARARGS, "set coefficients of filterbank to linearly spaced mel scale"},
     321  {"set_mel_coeffs_htk", (PyCFunction) Py_filterbank_set_mel_coeffs_htk,
     322    METH_VARARGS, "set coefficients of filterbank to linearly spaced mel scale"},
    203323  {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs,
    204324    METH_NOARGS, "get coefficients of filterbank"},
    205325  {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs,
    206326    METH_VARARGS, "set coefficients of filterbank"},
     327  {"set_power", (PyCFunction) Py_filterbank_set_power,
     328    METH_VARARGS, "set power applied to filterbank input spectrum"},
     329  {"set_norm", (PyCFunction) Py_filterbank_set_norm,
     330    METH_VARARGS, "set norm applied to filterbank input spectrum"},
    207331  {NULL}
    208332};
  • src/spectral/filterbank.c

    r0d07a2a r992e0ff  
    2424#include "fmat.h"
    2525#include "cvec.h"
     26#include "vecutils.h"
    2627#include "spectral/filterbank.h"
    2728#include "mathutils.h"
     
    3334  uint_t n_filters;
    3435  fmat_t *filters;
     36  smpl_t norm;
     37  smpl_t power;
    3538};
    3639
     
    4548  /* allocate filter tables, a matrix of length win_s and of height n_filters */
    4649  fb->filters = new_fmat (n_filters, win_s / 2 + 1);
     50
     51  fb->norm = 1;
     52
     53  fb->power = 1;
    4754
    4855  return fb;
     
    6875  tmp.data = in->norm;
    6976
     77  if (f->power != 1.) fvec_pow(&tmp, f->power);
     78
    7079  fmat_vecmul(f->filters, &tmp, out);
    7180
     
    8594  return 0;
    8695}
     96
     97uint_t
     98aubio_filterbank_set_norm (aubio_filterbank_t *f, smpl_t norm)
     99{
     100  if (norm != 0 && norm != 1) return AUBIO_FAIL;
     101  f->norm = norm;
     102  return AUBIO_OK;
     103}
     104
     105smpl_t
     106aubio_filterbank_get_norm (aubio_filterbank_t *f)
     107{
     108  return f->norm;
     109}
     110
     111uint_t
     112aubio_filterbank_set_power (aubio_filterbank_t *f, smpl_t power)
     113{
     114  f->power = power;
     115  return AUBIO_OK;
     116}
     117
     118smpl_t
     119aubio_filterbank_get_power (aubio_filterbank_t *f)
     120{
     121  return f->norm;
     122}
  • src/spectral/filterbank.h

    r0d07a2a r992e0ff  
    8484uint_t aubio_filterbank_set_coeffs (aubio_filterbank_t * f, const fmat_t * filters);
    8585
     86/** set norm parameter
     87
     88  \param f filterbank object, as returned by new_aubio_filterbank()
     89  \param norm `1` to norm the filters, `0` otherwise.
     90
     91  If set to `0`, the filters will not be normalized. If set to `1`,
     92  each filter will be normalized to one. Defaults to `1`.
     93
     94  This function should be called *before* setting the filters with one of
     95  aubio_filterbank_set_triangle_bands(), aubio_filterbank_set_mel_coeffs(),
     96  aubio_filterbank_set_mel_coeffs_htk(), or
     97  aubio_filterbank_set_mel_coeffs_slaney().
     98
     99 */
     100uint_t aubio_filterbank_set_norm (aubio_filterbank_t *f, smpl_t norm);
     101
     102/** get norm parameter
     103
     104  \param f filterbank object, as returned by new_aubio_filterbank()
     105  \returns `1` if norm is set, `0` otherwise. Defaults to `1`.
     106
     107 */
     108smpl_t aubio_filterbank_get_norm (aubio_filterbank_t *f);
     109
     110/** set power parameter
     111
     112  \param f filterbank object, as returned by new_aubio_filterbank()
     113  \param power Raise norm of the input spectrum norm to this power before
     114  computing filterbank.  Defaults to `1`.
     115
     116 */
     117uint_t aubio_filterbank_set_power (aubio_filterbank_t *f, smpl_t power);
     118
     119/** get power parameter
     120
     121  \param f filterbank object, as returned by new_aubio_filterbank()
     122  \return current power parameter. Defaults to `1`.
     123
     124 */
     125smpl_t aubio_filterbank_get_power (aubio_filterbank_t *f);
     126
    86127#ifdef __cplusplus
    87128}
  • src/spectral/filterbank_mel.c

    r0d07a2a r992e0ff  
    9191
    9292  /* compute triangle heights so that each triangle has unit area */
    93   for (fn = 0; fn < n_filters; fn++) {
    94     triangle_heights->data[fn] =
    95         2. / (upper_freqs->data[fn] - lower_freqs->data[fn]);
     93  if (aubio_filterbank_get_norm(fb)) {
     94    for (fn = 0; fn < n_filters; fn++) {
     95      triangle_heights->data[fn] =
     96          2. / (upper_freqs->data[fn] - lower_freqs->data[fn]);
     97    }
     98  } else {
     99    fvec_ones (triangle_heights);
    96100  }
    97101
     
    118122
    119123    /* compute positive slope step size */
    120     riseInc =
    121         triangle_heights->data[fn] /
    122         (center_freqs->data[fn] - lower_freqs->data[fn]);
     124    riseInc = triangle_heights->data[fn]
     125      / (center_freqs->data[fn] - lower_freqs->data[fn]);
    123126
    124127    /* compute coefficients in positive slope */
     
    134137
    135138    /* compute negative slope step size */
    136     downInc =
    137         triangle_heights->data[fn] /
    138         (upper_freqs->data[fn] - center_freqs->data[fn]);
     139    downInc = triangle_heights->data[fn]
     140      / (upper_freqs->data[fn] - center_freqs->data[fn]);
    139141
    140142    /* compute coefficents in negative slope */
     
    207209  return retval;
    208210}
     211
     212uint_t
     213aubio_filterbank_set_mel_coeffs (aubio_filterbank_t * fb, smpl_t samplerate,
     214    smpl_t freq_min, smpl_t freq_max)
     215{
     216  uint_t m, retval;
     217  smpl_t start, end, step;
     218  fvec_t *freqs;
     219  fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
     220  uint_t n_bands = coeffs->height;
     221
     222  if (freq_max < 0) {
     223    AUBIO_ERR("filterbank: set_mel_coeffs freq_max should be > 0\n");
     224    return AUBIO_FAIL;
     225  } else if (freq_max == 0) {
     226    end = aubio_hztomel(samplerate / 2.);
     227  } else {
     228    end = aubio_hztomel(freq_max);
     229  }
     230  if (freq_min < 0) {
     231    AUBIO_ERR("filterbank: set_mel_coeffs freq_min should be > 0\n");
     232    return AUBIO_FAIL;
     233  } else {
     234    start = aubio_hztomel(freq_min);
     235  }
     236  if (n_bands <= 0) {
     237    AUBIO_ERR("filterbank: set_mel_coeffs n_bands should be > 0\n");
     238    return AUBIO_FAIL;
     239  }
     240
     241  freqs = new_fvec(n_bands + 2);
     242  step = (end - start) / (n_bands + 1);
     243
     244  for (m = 0; m < n_bands + 2; m++)
     245  {
     246    freqs->data[m] = MIN(aubio_meltohz(start + step * m), samplerate/2.);
     247  }
     248
     249  retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
     250
     251  /* destroy vector used to store frequency limits */
     252  del_fvec (freqs);
     253  return retval;
     254}
     255
     256uint_t
     257aubio_filterbank_set_mel_coeffs_htk (aubio_filterbank_t * fb, smpl_t samplerate,
     258    smpl_t freq_min, smpl_t freq_max)
     259{
     260  uint_t m, retval;
     261  smpl_t start, end, step;
     262  fvec_t *freqs;
     263  fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
     264  uint_t n_bands = coeffs->height;
     265
     266  if (freq_max < 0) {
     267    AUBIO_ERR("filterbank: set_mel_coeffs freq_max should be > 0\n");
     268    return AUBIO_FAIL;
     269  } else if (freq_max == 0) {
     270    end = aubio_hztomel_htk(samplerate / 2.);
     271  } else {
     272    end = aubio_hztomel_htk(freq_max);
     273  }
     274  if (freq_min < 0) {
     275    AUBIO_ERR("filterbank: set_mel_coeffs freq_min should be > 0\n");
     276    return AUBIO_FAIL;
     277  } else {
     278    start = aubio_hztomel_htk(freq_min);
     279  }
     280  if (n_bands <= 0) {
     281    AUBIO_ERR("filterbank: set_mel_coeffs n_bands should be > 0\n");
     282    return AUBIO_FAIL;
     283  }
     284
     285  freqs = new_fvec (n_bands + 2);
     286  step = (end - start) / (n_bands + 1);
     287
     288  for (m = 0; m < n_bands + 2; m++)
     289  {
     290    freqs->data[m] = MIN(aubio_meltohz_htk(start + step * m), samplerate/2.);
     291  }
     292
     293  retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
     294
     295  /* destroy vector used to store frequency limits */
     296  del_fvec (freqs);
     297  return retval;
     298}
  • src/spectral/filterbank_mel.h

    r0d07a2a r992e0ff  
    5858  \param samplerate audio sampling rate
    5959
    60   The filter coefficients are built according to Malcolm Slaney's Auditory
    61   Toolbox, available online at the following address (see file mfcc.m):
     60  The filter coefficients are built to match exactly Malcolm Slaney's Auditory
     61  Toolbox implementation (see file mfcc.m). The number of filters should be 40.
    6262
     63  References
     64  ----------
     65
     66  Malcolm Slaney, *Auditory Toolbox Version 2, Technical Report #1998-010*
    6367  https://engineering.purdue.edu/~malcolm/interval/1998-010/
    6468
     
    6771    smpl_t samplerate);
    6872
     73/** Mel filterbank initialization
     74
     75  \param fb filterbank object
     76  \param samplerate audio sampling rate
     77  \param fmin start frequency, in Hz
     78  \param fmax end frequency, in Hz
     79
     80  The filterbank will be initialized with bands linearly spaced in the mel
     81  scale, from `fmin` to `fmax`.
     82
     83  References
     84  ----------
     85
     86  Malcolm Slaney, *Auditory Toolbox Version 2, Technical Report #1998-010*
     87  https://engineering.purdue.edu/~malcolm/interval/1998-010/
     88
     89*/
     90uint_t aubio_filterbank_set_mel_coeffs(aubio_filterbank_t * fb,
     91    smpl_t samplerate, smpl_t fmin, smpl_t fmax);
     92
     93/** Mel filterbank initialization
     94
     95  \param fb filterbank object
     96  \param samplerate audio sampling rate
     97  \param fmin start frequency, in Hz
     98  \param fmax end frequency, in Hz
     99
     100  The bank of filters will be initalized to to cover linearly spaced bands in
     101  the Htk mel scale, from `fmin` to `fmax`.
     102
     103  References
     104  ----------
     105
     106  Douglas O'Shaughnessy (1987). *Speech communication: human and machine*.
     107  Addison-Wesley. p. 150. ISBN 978-0-201-16520-3.
     108
     109  HTK Speech Recognition Toolkit: http://htk.eng.cam.ac.uk/
     110
     111*/
     112uint_t aubio_filterbank_set_mel_coeffs_htk(aubio_filterbank_t * fb,
     113    smpl_t samplerate, smpl_t fmin, smpl_t fmax);
     114
    69115#ifdef __cplusplus
    70116}
Note: See TracChangeset for help on using the changeset viewer.