Showing the changes to python (3 of 13 files). Show all
Unified · Split
| @@ -2,6 +2,7 @@ aubiodefvalue = { · 1 unmodified line | ||
| 2 | 2 | # we have some clean up to do |
| 3 | 3 | 'buf_size': 'Py_default_vector_length', |
| 4 | 4 | 'win_s': 'Py_default_vector_length', |
| 5 | + 'size': 'Py_default_vector_length', | |
| 5 | 6 | # and here too |
| 6 | 7 | 'hop_size': 'Py_default_vector_length / 2', |
| 7 | 8 | 'hop_s': 'Py_default_vector_length / 2', |
| @@ -82,6 +83,7 @@ objoutsize = { · 74 unmodified lines | ||
| 82 | 83 | 'tempo': '1', |
| 83 | 84 | 'filterbank': 'self->n_filters', |
| 84 | 85 | 'tss': 'self->buf_size', |
| 86 | + 'dct': 'self->size', | |
| 85 | 87 | } |
| 86 | 88 | |
| 87 | 89 | objinputsize = { |
| @@ -176,6 +178,10 @@ class MappedObject(object): · 88 unmodified lines | ||
| 176 | 178 | self.do_inputs = [get_params_types_names(self.do_proto)[1]] |
| 177 | 179 | self.do_outputs = get_params_types_names(self.do_proto)[2:] |
| 178 | 180 | struct_output_str = ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in self.do_outputs] |
| 181 | + if len(self.prototypes['rdo']): | |
| 182 | + rdo_outputs = get_params_types_names(prototypes['rdo'][0])[2:] | |
| 183 | + struct_output_str += ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in rdo_outputs] | |
| 184 | + self.outputs += rdo_outputs | |
| 179 | 185 | self.struct_outputs = ";\n ".join(struct_output_str) |
| 180 | 186 | |
| 181 | 187 | #print ("input_params: ", map(split_type, get_input_params(self.do_proto))) |
| @@ -190,6 +196,11 @@ class MappedObject(object): · 8 unmodified lines | ||
| 190 | 196 | out += self.gen_init() |
| 191 | 197 | out += self.gen_del() |
| 192 | 198 | out += self.gen_do() |
| 199 | + if len(self.prototypes['rdo']): | |
| 200 | + self.do_proto = self.prototypes['rdo'][0] | |
| 201 | + self.do_inputs = [get_params_types_names(self.do_proto)[1]] | |
| 202 | + self.do_outputs = get_params_types_names(self.do_proto)[2:] | |
| 203 | + out += self.gen_do(method='rdo') | |
| 193 | 204 | out += self.gen_memberdef() |
| 194 | 205 | out += self.gen_set() |
| 195 | 206 | out += self.gen_get() |
| @@ -370,12 +381,12 @@ Py_{shortname}_del (Py_{shortname} * self, PyObject * unused) · 174 unmodified lines | ||
| 370 | 381 | """.format(del_fn = del_fn) |
| 371 | 382 | return out |
| 372 | 383 | |
| 373 | − def gen_do(self): | |
| 384 | + def gen_do(self, method = 'do'): | |
| 374 | 385 | out = """ |
| 375 | 386 | // do {shortname} |
| 376 | 387 | static PyObject* |
| 377 | −Py_{shortname}_do (Py_{shortname} * self, PyObject * args) | |
| 378 | −{{""".format(**self.__dict__) | |
| 388 | +Pyaubio_{shortname}_{method} (Py_{shortname} * self, PyObject * args) | |
| 389 | +{{""".format(method = method, **self.__dict__) | |
| 379 | 390 | input_params = self.do_inputs |
| 380 | 391 | output_params = self.do_outputs |
| 381 | 392 | #print input_params |
| @@ -515,6 +526,12 @@ static PyMethodDef Py_{shortname}_methods[] = {{""".format(**self.__dict__) · 133 unmodified lines | ||
| 515 | 526 | out += """ |
| 516 | 527 | {{"{shortname}", (PyCFunction) Py{name}, |
| 517 | 528 | METH_NOARGS, ""}},""".format(name = name, shortname = shortname) |
| 529 | + for m in self.prototypes['rdo']: | |
| 530 | + name = get_name(m) | |
| 531 | + shortname = name.replace('aubio_%s_' % self.shortname, '') | |
| 532 | + out += """ | |
| 533 | + {{"{shortname}", (PyCFunction) Py{name}, | |
| 534 | + METH_VARARGS, ""}},""".format(name = name, shortname = shortname) | |
| 518 | 535 | out += """ |
| 519 | 536 | {NULL} /* sentinel */ |
| 520 | 537 | }; |
| @@ -540,7 +557,7 @@ PyTypeObject Py_{shortname}Type = {{ · 19 unmodified lines | ||
| 540 | 557 | 0, |
| 541 | 558 | 0, |
| 542 | 559 | 0, |
| 543 | − (ternaryfunc)Py_{shortname}_do, | |
| 560 | + (ternaryfunc)Pyaubio_{shortname}_do, | |
| 544 | 561 | 0, |
| 545 | 562 | 0, |
| 546 | 563 | 0, |
| @@ -181,7 +181,7 @@ def generate_lib_from_c_declarations(cpp_objects, c_declarations): · 180 unmodified lines | ||
| 181 | 181 | if o[:6] == 'aubio_': |
| 182 | 182 | shortname = o[6:-2] # without aubio_ prefix and _t suffix |
| 183 | 183 | |
| 184 | − lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []} | |
| 184 | + lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'rdo': [], 'get': [], 'set': [], 'other': []} | |
| 185 | 185 | lib[shortname]['longname'] = o |
| 186 | 186 | lib[shortname]['shortname'] = shortname |
| 187 | 187 | |
| @@ -195,6 +195,8 @@ def generate_lib_from_c_declarations(cpp_objects, c_declarations): · 7 unmodified lines | ||
| 195 | 195 | lib[shortname]['struct'].append(fn) |
| 196 | 196 | elif '_do' in fn: |
| 197 | 197 | lib[shortname]['do'].append(fn) |
| 198 | + elif '_rdo' in fn: | |
| 199 | + lib[shortname]['rdo'].append(fn) | |
| 198 | 200 | elif 'new_' in fn: |
| 199 | 201 | lib[shortname]['new'].append(fn) |
| 200 | 202 | elif 'del_' in fn: |
| @@ -0,0 +1,68 @@ | ||
| 1 | +#! /usr/bin/env python | |
| 2 | + | |
| 3 | + | |
| 4 | +import numpy as np | |
| 5 | +from numpy.testing import TestCase, assert_almost_equal | |
| 6 | +import aubio | |
| 7 | + | |
| 8 | +precomputed_arange = [ 9.89949512, -6.44232273, 0., -0.67345482, 0., | |
| 9 | + -0.20090288, 0., -0.05070186] | |
| 10 | + | |
| 11 | +precomputed_some_ones = [ 4.28539848, 0.2469689, -0.14625292, -0.58121818, | |
| 12 | + -0.83483052, -0.75921834, -0.35168475, 0.24087936, | |
| 13 | + 0.78539824, 1.06532764, 0.97632152, 0.57164496, 0.03688532, | |
| 14 | + -0.39446154, -0.54619485, -0.37771079] | |
| 15 | + | |
| 16 | +class aubio_dct(TestCase): | |
| 17 | + | |
| 18 | + def test_init(self): | |
| 19 | + """ test that aubio.dct() is created with expected size """ | |
| 20 | + a_dct = aubio.dct() | |
| 21 | + self.assertEqual(a_dct.size, 1024) | |
| 22 | + | |
| 23 | + def test_arange(self): | |
| 24 | + """ test that dct(arange(8)) is computed correctly | |
| 25 | + | |
| 26 | + >>> from scipy.fftpack import dct | |
| 27 | + >>> a_in = np.arange(8).astype(aubio.float_type) | |
| 28 | + >>> precomputed = dct(a_in, norm='ortho') | |
| 29 | + """ | |
| 30 | + N = len(precomputed_arange) | |
| 31 | + a_dct = aubio.dct(8) | |
| 32 | + a_in = np.arange(8).astype(aubio.float_type) | |
| 33 | + a_expected = aubio.fvec(precomputed_arange) | |
| 34 | + assert_almost_equal(a_dct(a_in), a_expected, decimal=5) | |
| 35 | + | |
| 36 | + def test_some_ones(self): | |
| 37 | + """ test that dct(somevector) is computed correctly """ | |
| 38 | + a_dct = aubio.dct(16) | |
| 39 | + a_in = np.ones(16).astype(aubio.float_type) | |
| 40 | + a_in[1] = 0 | |
| 41 | + a_in[3] = np.pi | |
| 42 | + a_expected = aubio.fvec(precomputed_some_ones) | |
| 43 | + assert_almost_equal(a_dct(a_in), a_expected, decimal=6) | |
| 44 | + | |
| 45 | + def test_reconstruction(self): | |
| 46 | + """ test that some_ones vector can be recontructed """ | |
| 47 | + a_dct = aubio.dct(16) | |
| 48 | + a_in = np.ones(16).astype(aubio.float_type) | |
| 49 | + a_in[1] = 0 | |
| 50 | + a_in[3] = np.pi | |
| 51 | + a_dct_in = a_dct(a_in) | |
| 52 | + a_dct_reconstructed = a_dct.rdo(a_dct_in) | |
| 53 | + assert_almost_equal(a_dct_reconstructed, a_in, decimal=6) | |
| 54 | + | |
| 55 | + def test_negative_size(self): | |
| 56 | + """ test that creation fails with a negative size """ | |
| 57 | + with self.assertRaises(ValueError): | |
| 58 | + aubio.dct(-1) | |
| 59 | + | |
| 60 | + def test_wrong_size(self): | |
| 61 | + """ test that creation fails with a non power-of-two size """ | |
| 62 | + # supports for non 2** fft sizes only when compiled with fftw3 | |
| 63 | + size = 13 | |
| 64 | + try: | |
| 65 | + with self.assertRaises(RuntimeError): | |
| 66 | + aubio.dct(size) | |
| 67 | + except AssertionError: | |
| 68 | + self.skipTest('creating aubio.dct with size %d did not fail' % size) | |