source: python/tests/test_phasevoc.py @ 4093c0c

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampleryinfft+
Last change on this file since 4093c0c was 0b6d23d, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/tests: fix most prospect warnings

  • Property mode set to 100755
File size: 5.4 KB
Line 
1#! /usr/bin/env python
2
3from numpy.testing import TestCase, assert_equal, assert_array_less
4from aubio import fvec, cvec, pvoc, float_type
5from nose2 import main
6from nose2.tools import params
7import numpy as np
8
9if float_type == 'float32':
10    max_sq_error = 1.e-12
11else:
12    max_sq_error = 1.e-29
13
14def create_sine(hop_s, freq, samplerate):
15    t = np.arange(hop_s).astype(float_type)
16    return np.sin( 2. * np.pi * freq * t / float(samplerate))
17
18def create_noise(hop_s):
19    return np.random.rand(hop_s).astype(float_type) * 2. - 1.
20
21class aubio_pvoc_test_case(TestCase):
22    """ pvoc object test case """
23
24    def test_members_automatic_sizes_default(self):
25        """ check object creation with default parameters """
26        f = pvoc()
27        assert_equal ([f.win_s, f.hop_s], [1024, 512])
28
29    def test_members_unnamed_params(self):
30        """ check object creation with unnamed parameters """
31        f = pvoc(2048, 128)
32        assert_equal ([f.win_s, f.hop_s], [2048, 128])
33
34    def test_members_named_params(self):
35        """ check object creation with named parameters """
36        f = pvoc(hop_s = 128, win_s = 2048)
37        assert_equal ([f.win_s, f.hop_s], [2048, 128])
38
39    def test_zeros(self):
40        """ check the resynthesis of zeros gives zeros """
41        win_s, hop_s = 1024, 256
42        f = pvoc (win_s, hop_s)
43        t = fvec (hop_s)
44        for _ in range( int ( 4 * win_s / hop_s ) ):
45            s = f(t)
46            r = f.rdo(s)
47            assert_equal ( t, 0.)
48            assert_equal ( s.norm, 0.)
49            assert_equal ( s.phas, 0.)
50            assert_equal ( r, 0.)
51
52    @params(
53            ( 256, 8),
54            ( 256, 4),
55            ( 256, 2),
56            ( 512, 8),
57            ( 512, 4),
58            ( 512, 2),
59            #( 129, 2),
60            #( 320, 4),
61            #(  13, 8),
62            (1024, 8),
63            (1024, 4),
64            (1024, 2),
65            (2048, 8),
66            (2048, 4),
67            (2048, 2),
68            (4096, 8),
69            (4096, 4),
70            (4096, 2),
71            (8192, 8),
72            (8192, 4),
73            (8192, 2),
74            )
75    def test_resynth_steps_noise(self, hop_s, ratio):
76        """ check the resynthesis of a random signal is correct """
77        sigin = create_noise(hop_s)
78        self.reconstruction(sigin, hop_s, ratio)
79
80    @params(
81            (44100,  256, 8,   441),
82            (44100,  256, 4,  1203),
83            (44100,  256, 2,  3045),
84            (44100,  512, 8,   445),
85            (44100,  512, 4,   445),
86            (44100,  512, 2,   445),
87            (44100, 1024, 8,   445),
88            (44100, 1024, 4,   445),
89            (44100, 1024, 2,   445),
90            ( 8000, 1024, 2,   445),
91            (22050, 1024, 2,   445),
92            (22050,  256, 8,   445),
93            (96000, 1024, 8, 47000),
94            (96000, 1024, 8,    20),
95            )
96    def test_resynth_steps_sine(self, samplerate, hop_s, ratio, freq):
97        """ check the resynthesis of a sine is correct """
98        sigin = create_sine(hop_s, freq, samplerate)
99        self.reconstruction(sigin, hop_s, ratio)
100
101    def reconstruction(self, sigin, hop_s, ratio):
102        buf_s = hop_s * ratio
103        f = pvoc(buf_s, hop_s)
104        zeros = fvec(hop_s)
105        r2 = f.rdo( f(sigin) )
106        for _ in range(1, ratio):
107            r2 = f.rdo( f(zeros) )
108        # compute square errors
109        sq_error = (r2 - sigin)**2
110        # make sure all square errors are less than desired precision
111        assert_array_less(sq_error, max_sq_error)
112
113class aubio_pvoc_strange_params(TestCase):
114
115    def test_win_size_short(self):
116        with self.assertRaises(RuntimeError):
117            pvoc(1, 1)
118
119    def test_hop_size_long(self):
120        with self.assertRaises(RuntimeError):
121            pvoc(1024, 1025)
122
123    def test_large_input_timegrain(self):
124        win_s = 1024
125        f = pvoc(win_s)
126        t = fvec(win_s + 1)
127        with self.assertRaises(ValueError):
128            f(t)
129
130    def test_small_input_timegrain(self):
131        win_s = 1024
132        f = pvoc(win_s)
133        t = fvec(1)
134        with self.assertRaises(ValueError):
135            f(t)
136
137    def test_large_input_fftgrain(self):
138        win_s = 1024
139        f = pvoc(win_s)
140        s = cvec(win_s + 5)
141        with self.assertRaises(ValueError):
142            f.rdo(s)
143
144    def test_small_input_fftgrain(self):
145        win_s = 1024
146        f = pvoc(win_s)
147        s = cvec(16)
148        with self.assertRaises(ValueError):
149            f.rdo(s)
150
151class aubio_pvoc_wrong_params(TestCase):
152
153    def test_wrong_buf_size(self):
154        win_s = -1
155        with self.assertRaises(ValueError):
156            pvoc(win_s)
157
158    def test_buf_size_too_small(self):
159        win_s = 1
160        with self.assertRaises(RuntimeError):
161            pvoc(win_s)
162
163    def test_hop_size_negative(self):
164        win_s = 512
165        hop_s = -2
166        with self.assertRaises(ValueError):
167            pvoc(win_s, hop_s)
168
169    def test_hop_size_too_small(self):
170        win_s = 1
171        hop_s = 1
172        with self.assertRaises(RuntimeError):
173            pvoc(win_s, hop_s)
174
175    def test_buf_size_not_power_of_two(self):
176        win_s = 320
177        hop_s = win_s // 2
178        try:
179            with self.assertRaises(RuntimeError):
180                pvoc(win_s, hop_s)
181        except AssertionError:
182            # when compiled with fftw3, aubio supports non power of two fft sizes
183            self.skipTest('creating aubio.pvoc with size %d did not fail' % win_s)
184
185if __name__ == '__main__':
186    main()
187
Note: See TracBrowser for help on using the repository browser.