File: py-filterbank.c

package info (click to toggle)
aubio 0.4.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,720 kB
  • sloc: python: 20,447; ansic: 20,127; makefile: 348; sh: 232
file content (582 lines) | stat: -rw-r--r-- 14,587 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#include "aubio-types.h"

static char Py_filterbank_doc[] = ""
"filterbank(n_filters=40, win_s=1024)\n"
"\n"
"Create a bank of spectral filters. Each instance is a callable\n"
"that holds a matrix of coefficients.\n"
"\n"
"See also :meth:`set_mel_coeffs`, :meth:`set_mel_coeffs_htk`,\n"
":meth:`set_mel_coeffs_slaney`, :meth:`set_triangle_bands`, and\n"
":meth:`set_coeffs`.\n"
"\n"
"Parameters\n"
"----------\n"
"n_filters : int\n"
"    Number of filters to create.\n"
"win_s : int\n"
"    Size of the input spectrum to process.\n"
"\n"
"Examples\n"
"--------\n"
">>> f = aubio.filterbank(128, 1024)\n"
">>> f.set_mel_coeffs(44100, 0, 10000)\n"
">>> c = aubio.cvec(1024)\n"
">>> f(c).shape\n"
"(128, )\n"
"";

static char Py_filterbank_set_triangle_bands_doc[] =""
"set_triangle_bands(freqs, samplerate)\n"
"\n"
"Set triangular bands. The coefficients will be set to triangular\n"
"overlapping windows using the boundaries specified by `freqs`.\n"
"\n"
"`freqs` should contain `n_filters + 2` frequencies in Hz, ordered\n"
"by value, from smallest to largest. The first element should be greater\n"
"or equal to zero; the last element should be smaller or equal to\n"
"`samplerate / 2`.\n"
"\n"
"Parameters\n"
"----------\n"
"freqs: fvec\n"
"    List of frequencies, in Hz.\n"
"samplerate : float\n"
"    Sampling-rate of the expected input.\n"
"\n"
"Example\n"
"-------\n"
">>> fb = aubio.filterbank(n_filters=100, win_s=2048)\n"
">>> samplerate = 44100; freqs = np.linspace(0, 20200, 102)\n"
">>> fb.set_triangle_bands(aubio.fvec(freqs), samplerate)\n"
"";

static char Py_filterbank_set_mel_coeffs_slaney_doc[] = ""
"set_mel_coeffs_slaney(samplerate)\n"
"\n"
"Set coefficients of filterbank to match Slaney's Auditory Toolbox.\n"
"\n"
"The filter coefficients will be set as in Malcolm Slaney's\n"
"implementation. The filterbank should have been created with\n"
"`n_filters = 40`.\n"
"\n"
"This is approximately equivalent to using :meth:`set_mel_coeffs` with\n"
"`fmin = 400./3., fmax = 6853.84`.\n"
"\n"
"Parameters\n"
"----------\n"
"samplerate : float\n"
"    Sampling-rate of the expected input.\n"
"\n"
"References\n"
"----------\n"
"\n"
"Malcolm Slaney, `Auditory Toolbox Version 2, Technical Report #1998-010\n"
"<https://engineering.purdue.edu/~malcolm/interval/1998-010/>`_\n"
"";

static char Py_filterbank_set_mel_coeffs_doc[] = ""
"set_mel_coeffs(samplerate, fmin, fmax)\n"
"\n"
"Set coefficients of filterbank to linearly spaced mel scale.\n"
"\n"
"Parameters\n"
"----------\n"
"samplerate : float\n"
"    Sampling-rate of the expected input.\n"
"fmin : float\n"
"    Lower frequency boundary of the first filter.\n"
"fmax : float\n"
"    Upper frequency boundary of the last filter.\n"
"\n"
"See also\n"
"--------\n"
"hztomel\n"
"";

static char Py_filterbank_set_mel_coeffs_htk_doc[] = ""
"set_mel_coeffs_htk(samplerate, fmin, fmax)\n"
"\n"
"Set coefficients of the filters to be linearly spaced in the HTK mel scale.\n"
"\n"
"Parameters\n"
"----------\n"
"samplerate : float\n"
"    Sampling-rate of the expected input.\n"
"fmin : float\n"
"    Lower frequency boundary of the first filter.\n"
"fmax : float\n"
"    Upper frequency boundary of the last filter.\n"
"\n"
"See also\n"
"--------\n"
"hztomel with `htk=True`\n"
"";

static char Py_filterbank_get_coeffs_doc[] = ""
"get_coeffs()\n"
"\n"
"Get coefficients matrix of filterbank.\n"
"\n"
"Returns\n"
"-------\n"
"array_like\n"
"    Array of shape (n_filters, win_s/2+1) containing the coefficients.\n"
"";

static char Py_filterbank_set_coeffs_doc[] = ""
"set_coeffs(coeffs)\n"
"\n"
"Set coefficients of filterbank.\n"
"\n"
"Parameters\n"
"----------\n"
"coeffs : fmat\n"
"    Array of shape (n_filters, win_s/2+1) containing the coefficients.\n"
"";

static char Py_filterbank_set_power_doc[] = ""
"set_power(power)\n"
"\n"
"Set power applied to input spectrum of filterbank.\n"
"\n"
"Parameters\n"
"----------\n"
"power : float\n"
"    Power to raise input spectrum to before computing the filters.\n"
"";

static char Py_filterbank_get_power_doc[] = ""
"get_power()\n"
"\n"
"Get power applied to filterbank.\n"
"\n"
"Returns\n"
"-------\n"
"float\n"
"    Power parameter.\n"
"";

static char Py_filterbank_set_norm_doc[] = ""
"set_norm(norm)\n"
"\n"
"Set norm parameter. If set to `0`, the filters will not be normalized.\n"
"If set to `1`, the filters will be normalized to one. Default to `1`.\n"
"\n"
"This function should be called *before* :meth:`set_triangle_bands`,\n"
":meth:`set_mel_coeffs`, :meth:`set_mel_coeffs_htk`, or\n"
":meth:`set_mel_coeffs_slaney`.\n"
"\n"
"Parameters\n"
"----------\n"
"norm : int\n"
"   `0` to disable, `1` to enable\n"
"";

static char Py_filterbank_get_norm_doc[] = ""
"get_norm()\n"
"\n"
"Get norm parameter of filterbank.\n"
"\n"
"Returns\n"
"-------\n"
"float\n"
"    Norm parameter.\n"
"";

typedef struct
{
  PyObject_HEAD
  aubio_filterbank_t * o;
  uint_t n_filters;
  uint_t win_s;
  cvec_t vec;
  fvec_t freqs;
  fmat_t coeffs;
  PyObject *out;
  fvec_t c_out;
} Py_filterbank;

static PyObject *
Py_filterbank_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
{
  int win_s = 0, n_filters = 0;
  Py_filterbank *self;
  static char *kwlist[] = { "n_filters", "win_s", NULL };

  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|II", kwlist,
          &n_filters, &win_s)) {
    return NULL;
  }

  self = (Py_filterbank *) type->tp_alloc (type, 0);

  if (self == NULL) {
    return NULL;
  }

  self->win_s = Py_default_vector_length;
  if (win_s > 0) {
    self->win_s = win_s;
  } else if (win_s < 0) {
    PyErr_SetString (PyExc_ValueError,
        "can not use negative window size");
    return NULL;
  }

  self->n_filters = 40;
  if (n_filters > 0) {
    self->n_filters = n_filters;
  } else if (n_filters < 0) {
    PyErr_SetString (PyExc_ValueError,
        "can not use negative number of filters");
    return NULL;
  }

  return (PyObject *) self;
}

static int
Py_filterbank_init (Py_filterbank * self, PyObject * args, PyObject * kwds)
{
  self->o = new_aubio_filterbank (self->n_filters, self->win_s);
  if (self->o == NULL) {
    PyErr_Format(PyExc_RuntimeError, "error creating filterbank with"
        " n_filters=%d, win_s=%d", self->n_filters, self->win_s);
    return -1;
  }
  self->out = new_py_fvec(self->n_filters);

  return 0;
}

static void
Py_filterbank_del (Py_filterbank *self, PyObject *unused)
{
  if (self->o) {
    free(self->coeffs.data);
    del_aubio_filterbank(self->o);
  }
  Py_XDECREF(self->out);
  Py_TYPE(self)->tp_free((PyObject *) self);
}

static PyObject *
Py_filterbank_do(Py_filterbank * self, PyObject * args)
{
  PyObject *input;

  if (!PyArg_ParseTuple (args, "O", &input)) {
    return NULL;
  }

  if (!PyAubio_PyCvecToCCvec(input, &(self->vec) )) {
    return NULL;
  }

  if (self->vec.length != self->win_s / 2 + 1) {
    PyErr_Format(PyExc_ValueError,
                 "input cvec has length %d, but filterbank expects length %d",
                 self->vec.length, self->win_s / 2 + 1);
    return NULL;
  }

  Py_INCREF(self->out);
  if (!PyAubio_ArrayToCFvec(self->out, &(self->c_out))) {
    return NULL;
  }
  // compute the function
  aubio_filterbank_do (self->o, &(self->vec), &(self->c_out));
  return self->out;
}

static PyMemberDef Py_filterbank_members[] = {
  {"win_s", T_INT, offsetof (Py_filterbank, win_s), READONLY,
    "size of the window"},
  {"n_filters", T_INT, offsetof (Py_filterbank, n_filters), READONLY,
    "number of filters"},
  {NULL} /* sentinel */
};

static PyObject *
Py_filterbank_set_triangle_bands (Py_filterbank * self, PyObject *args)
{
  uint_t err = 0;

  PyObject *input;
  smpl_t samplerate;
  if (!PyArg_ParseTuple (args, "O" AUBIO_NPY_SMPL_CHR, &input, &samplerate)) {
    return NULL;
  }

  if (input == NULL) {
    return NULL;
  }

  if (!PyAubio_ArrayToCFvec(input, &(self->freqs) )) {
    return NULL;
  }

  err = aubio_filterbank_set_triangle_bands (self->o,
      &(self->freqs), samplerate);
  if (err > 0) {
    if (PyErr_Occurred() == NULL) {
      PyErr_SetString (PyExc_ValueError, "error running set_triangle_bands");
    } else {
      // change the RuntimeError into ValueError
      PyObject *type, *value, *traceback;
      PyErr_Fetch(&type, &value, &traceback);
      PyErr_Restore(PyExc_ValueError, value, traceback);
    }
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyObject *
Py_filterbank_set_mel_coeffs_slaney (Py_filterbank * self, PyObject *args)
{
  uint_t err = 0;

  smpl_t samplerate;
  if (!PyArg_ParseTuple (args, AUBIO_NPY_SMPL_CHR, &samplerate)) {
    return NULL;
  }

  err = aubio_filterbank_set_mel_coeffs_slaney (self->o, samplerate);
  if (err > 0) {
    if (PyErr_Occurred() == NULL) {
      PyErr_SetString (PyExc_ValueError, "error running set_mel_coeffs_slaney");
    } else {
      // change the RuntimeError into ValueError
      PyObject *type, *value, *traceback;
      PyErr_Fetch(&type, &value, &traceback);
      PyErr_Restore(PyExc_ValueError, value, traceback);
    }
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyObject *
Py_filterbank_set_mel_coeffs (Py_filterbank * self, PyObject *args)
{
  uint_t err = 0;

  smpl_t samplerate;
  smpl_t freq_min;
  smpl_t freq_max;
  if (!PyArg_ParseTuple (args, AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR
        AUBIO_NPY_SMPL_CHR, &samplerate, &freq_min, &freq_max)) {
    return NULL;
  }

  err = aubio_filterbank_set_mel_coeffs (self->o, samplerate,
      freq_min, freq_max);
  if (err > 0) {
    if (PyErr_Occurred() == NULL) {
      PyErr_SetString (PyExc_ValueError, "error running set_mel_coeffs");
    } else {
      // change the RuntimeError into ValueError
      PyObject *type, *value, *traceback;
      PyErr_Fetch(&type, &value, &traceback);
      PyErr_Restore(PyExc_ValueError, value, traceback);
    }
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyObject *
Py_filterbank_set_mel_coeffs_htk (Py_filterbank * self, PyObject *args)
{
  uint_t err = 0;

  smpl_t samplerate;
  smpl_t freq_min;
  smpl_t freq_max;
  if (!PyArg_ParseTuple (args, AUBIO_NPY_SMPL_CHR AUBIO_NPY_SMPL_CHR
        AUBIO_NPY_SMPL_CHR, &samplerate, &freq_min, &freq_max)) {
    return NULL;
  }

  err = aubio_filterbank_set_mel_coeffs_htk (self->o, samplerate,
      freq_min, freq_max);
  if (err > 0) {
    if (PyErr_Occurred() == NULL) {
      PyErr_SetString (PyExc_ValueError, "error running set_mel_coeffs_htk");
    } else {
      // change the RuntimeError into ValueError
      PyObject *type, *value, *traceback;
      PyErr_Fetch(&type, &value, &traceback);
      PyErr_Restore(PyExc_ValueError, value, traceback);
    }
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyObject *
Py_filterbank_set_coeffs (Py_filterbank * self, PyObject *args)
{
  uint_t err = 0;

  PyObject *input;
  if (!PyArg_ParseTuple (args, "O", &input)) {
    return NULL;
  }

  if (!PyAubio_ArrayToCFmat(input, &(self->coeffs))) {
    return NULL;
  }

  err = aubio_filterbank_set_coeffs (self->o, &(self->coeffs));

  if (err > 0) {
    PyErr_SetString (PyExc_ValueError,
        "error when setting filter coefficients");
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyObject *
Py_filterbank_get_coeffs (Py_filterbank * self, PyObject *unused)
{
  return (PyObject *)PyAubio_CFmatToArray(
      aubio_filterbank_get_coeffs (self->o) );
}

static PyObject *
Py_filterbank_set_power(Py_filterbank *self, PyObject *args)
{
  smpl_t power;

  if (!PyArg_ParseTuple (args, AUBIO_NPY_SMPL_CHR, &power)) {
    return NULL;
  }
  if(aubio_filterbank_set_power (self->o, power)) {
    if (PyErr_Occurred() == NULL) {
      PyErr_SetString (PyExc_ValueError,
          "error running filterbank.set_power");
    } else {
      // change the RuntimeError into ValueError
      PyObject *type, *value, *traceback;
      PyErr_Fetch(&type, &value, &traceback);
      PyErr_Restore(PyExc_ValueError, value, traceback);
    }
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyObject *
Py_filterbank_get_power (Py_filterbank * self, PyObject *unused)
{
  smpl_t power = aubio_filterbank_get_power(self->o);
  return (PyObject *)PyFloat_FromDouble (power);
}

static PyObject *
Py_filterbank_set_norm(Py_filterbank *self, PyObject *args)
{
  smpl_t norm;

  if (!PyArg_ParseTuple (args, AUBIO_NPY_SMPL_CHR, &norm)) {
    return NULL;
  }
  if(aubio_filterbank_set_norm (self->o, norm)) {
    if (PyErr_Occurred() == NULL) {
      PyErr_SetString (PyExc_ValueError,
          "error running filterbank.set_power");
    } else {
      // change the RuntimeError into ValueError
      PyObject *type, *value, *traceback;
      PyErr_Fetch(&type, &value, &traceback);
      PyErr_Restore(PyExc_ValueError, value, traceback);
    }
    return NULL;
  }
  Py_RETURN_NONE;
}

static PyObject *
Py_filterbank_get_norm (Py_filterbank * self, PyObject *unused)
{
  smpl_t norm = aubio_filterbank_get_norm(self->o);
  return (PyObject *)PyFloat_FromDouble (norm);
}

static PyMethodDef Py_filterbank_methods[] = {
  {"set_triangle_bands", (PyCFunction) Py_filterbank_set_triangle_bands,
    METH_VARARGS, Py_filterbank_set_triangle_bands_doc},
  {"set_mel_coeffs_slaney", (PyCFunction) Py_filterbank_set_mel_coeffs_slaney,
    METH_VARARGS, Py_filterbank_set_mel_coeffs_slaney_doc},
  {"set_mel_coeffs", (PyCFunction) Py_filterbank_set_mel_coeffs,
    METH_VARARGS, Py_filterbank_set_mel_coeffs_doc},
  {"set_mel_coeffs_htk", (PyCFunction) Py_filterbank_set_mel_coeffs_htk,
    METH_VARARGS, Py_filterbank_set_mel_coeffs_htk_doc},
  {"get_coeffs", (PyCFunction) Py_filterbank_get_coeffs,
    METH_NOARGS, Py_filterbank_get_coeffs_doc},
  {"set_coeffs", (PyCFunction) Py_filterbank_set_coeffs,
    METH_VARARGS, Py_filterbank_set_coeffs_doc},
  {"set_power", (PyCFunction) Py_filterbank_set_power,
    METH_VARARGS, Py_filterbank_set_power_doc},
  {"get_power", (PyCFunction) Py_filterbank_get_power,
    METH_NOARGS, Py_filterbank_get_power_doc},
  {"set_norm", (PyCFunction) Py_filterbank_set_norm,
    METH_VARARGS, Py_filterbank_set_norm_doc},
  {"get_norm", (PyCFunction) Py_filterbank_get_norm,
    METH_NOARGS, Py_filterbank_get_norm_doc},
  {NULL}
};

PyTypeObject Py_filterbankType = {
  PyVarObject_HEAD_INIT (NULL, 0)
  "aubio.filterbank",
  sizeof (Py_filterbank),
  0,
  (destructor) Py_filterbank_del,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  (ternaryfunc)Py_filterbank_do,
  0,
  0,
  0,
  0,
  Py_TPFLAGS_DEFAULT,
  Py_filterbank_doc,
  0,
  0,
  0,
  0,
  0,
  0,
  Py_filterbank_methods,
  Py_filterbank_members,
  0,
  0,
  0,
  0,
  0,
  0,
  (initproc) Py_filterbank_init,
  0,
  Py_filterbank_new,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
};