Changes in / [0d07a2a:992e0ff]
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
python/ext/py-filterbank.c
r0d07a2a r992e0ff 139 139 &(self->freqs), samplerate); 140 140 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 } 143 149 return NULL; 144 150 } … … 158 164 err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate); 159 165 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 179 static PyObject * 180 Py_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 208 static PyObject * 209 Py_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 } 162 232 return NULL; 163 233 } … … 194 264 return (PyObject *)PyAubio_CFmatToArray( 195 265 aubio_filterbank_get_coeffs (self->o) ); 266 } 267 268 static PyObject * 269 Py_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 291 static PyObject * 292 Py_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; 196 312 } 197 313 … … 201 317 {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney, 202 318 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"}, 203 323 {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs, 204 324 METH_NOARGS, "get coefficients of filterbank"}, 205 325 {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs, 206 326 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"}, 207 331 {NULL} 208 332 }; -
src/spectral/filterbank.c
r0d07a2a r992e0ff 24 24 #include "fmat.h" 25 25 #include "cvec.h" 26 #include "vecutils.h" 26 27 #include "spectral/filterbank.h" 27 28 #include "mathutils.h" … … 33 34 uint_t n_filters; 34 35 fmat_t *filters; 36 smpl_t norm; 37 smpl_t power; 35 38 }; 36 39 … … 45 48 /* allocate filter tables, a matrix of length win_s and of height n_filters */ 46 49 fb->filters = new_fmat (n_filters, win_s / 2 + 1); 50 51 fb->norm = 1; 52 53 fb->power = 1; 47 54 48 55 return fb; … … 68 75 tmp.data = in->norm; 69 76 77 if (f->power != 1.) fvec_pow(&tmp, f->power); 78 70 79 fmat_vecmul(f->filters, &tmp, out); 71 80 … … 85 94 return 0; 86 95 } 96 97 uint_t 98 aubio_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 105 smpl_t 106 aubio_filterbank_get_norm (aubio_filterbank_t *f) 107 { 108 return f->norm; 109 } 110 111 uint_t 112 aubio_filterbank_set_power (aubio_filterbank_t *f, smpl_t power) 113 { 114 f->power = power; 115 return AUBIO_OK; 116 } 117 118 smpl_t 119 aubio_filterbank_get_power (aubio_filterbank_t *f) 120 { 121 return f->norm; 122 } -
src/spectral/filterbank.h
r0d07a2a r992e0ff 84 84 uint_t aubio_filterbank_set_coeffs (aubio_filterbank_t * f, const fmat_t * filters); 85 85 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 */ 100 uint_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 */ 108 smpl_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 */ 117 uint_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 */ 125 smpl_t aubio_filterbank_get_power (aubio_filterbank_t *f); 126 86 127 #ifdef __cplusplus 87 128 } -
src/spectral/filterbank_mel.c
r0d07a2a r992e0ff 91 91 92 92 /* 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); 96 100 } 97 101 … … 118 122 119 123 /* 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]); 123 126 124 127 /* compute coefficients in positive slope */ … … 134 137 135 138 /* 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]); 139 141 140 142 /* compute coefficents in negative slope */ … … 207 209 return retval; 208 210 } 211 212 uint_t 213 aubio_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 256 uint_t 257 aubio_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 58 58 \param samplerate audio sampling rate 59 59 60 The filter coefficients are built according toMalcolm Slaney's Auditory61 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. 62 62 63 References 64 ---------- 65 66 Malcolm Slaney, *Auditory Toolbox Version 2, Technical Report #1998-010* 63 67 https://engineering.purdue.edu/~malcolm/interval/1998-010/ 64 68 … … 67 71 smpl_t samplerate); 68 72 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 */ 90 uint_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 */ 112 uint_t aubio_filterbank_set_mel_coeffs_htk(aubio_filterbank_t * fb, 113 smpl_t samplerate, smpl_t fmin, smpl_t fmax); 114 69 115 #ifdef __cplusplus 70 116 }
Note: See TracChangeset
for help on using the changeset viewer.