source: python/tests/test_dct.py @ 2bfbf33

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since 2bfbf33 was fc144a5, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[tests] avoid some imports, add missing main

  • Property mode set to 100755
File size: 2.4 KB
RevLine 
[2bf59042]1#! /usr/bin/env python
2
3
4import numpy as np
5from numpy.testing import TestCase, assert_almost_equal
6import aubio
7
[e7556a1]8precomputed_arange = [ 9.89949512, -6.44232273,  0., -0.67345482, 0.,
9        -0.20090288, 0., -0.05070186]
10
11precomputed_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
[2bf59042]16class aubio_dct(TestCase):
17
18    def test_init(self):
[e7556a1]19        """ test that aubio.dct() is created with expected size """
[2bf59042]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
[95e3cba]27        >>> a_in = np.arange(8).astype(aubio.float_type)
[2bf59042]28        >>> precomputed = dct(a_in, norm='ortho')
29        """
[e7556a1]30        N = len(precomputed_arange)
[2bf59042]31        a_dct = aubio.dct(8)
[95e3cba]32        a_in = np.arange(8).astype(aubio.float_type)
[e7556a1]33        a_expected = aubio.fvec(precomputed_arange)
[3cb2a52]34        assert_almost_equal(a_dct(a_in), a_expected, decimal=5)
[2bf59042]35
36    def test_some_ones(self):
37        """ test that dct(somevector) is computed correctly """
38        a_dct = aubio.dct(16)
[95e3cba]39        a_in = np.ones(16).astype(aubio.float_type)
[2bf59042]40        a_in[1] = 0
41        a_in[3] = np.pi
[e7556a1]42        a_expected = aubio.fvec(precomputed_some_ones)
[b99e2a5]43        assert_almost_equal(a_dct(a_in), a_expected, decimal=6)
[2bf59042]44
45    def test_reconstruction(self):
[e7556a1]46        """ test that some_ones vector can be recontructed """
[2bf59042]47        a_dct = aubio.dct(16)
[95e3cba]48        a_in = np.ones(16).astype(aubio.float_type)
[2bf59042]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):
[e7556a1]56        """ test that creation fails with a negative size """
[2bf59042]57        with self.assertRaises(ValueError):
58            aubio.dct(-1)
59
60    def test_wrong_size(self):
[e7556a1]61        """ test that creation fails with a non power-of-two size """
[2bf59042]62        # supports for non 2** fft sizes only when compiled with fftw3
[b99e2a5]63        size = 13
[2bf59042]64        try:
65            with self.assertRaises(RuntimeError):
[b99e2a5]66                aubio.dct(size)
[2bf59042]67        except AssertionError:
[b99e2a5]68            self.skipTest('creating aubio.dct with size %d did not fail' % size)
[fc144a5]69
70if __name__ == '__main__':
71    from unittest import main
72    main()
Note: See TracBrowser for help on using the repository browser.