[75e715f] | 1 | #! /usr/bin/env python |
---|
[1ebf8770] | 2 | |
---|
[215b33c] | 3 | from numpy.testing import TestCase, assert_equal, assert_array_less |
---|
[ac65a2f] | 4 | from aubio import fvec, cvec, pvoc, float_type |
---|
[0b6d23d] | 5 | from nose2 import main |
---|
[fb434c4] | 6 | from nose2.tools import params |
---|
[ac65a2f] | 7 | import numpy as np |
---|
[467122d] | 8 | |
---|
[fb434c4] | 9 | if float_type == 'float32': |
---|
| 10 | max_sq_error = 1.e-12 |
---|
| 11 | else: |
---|
| 12 | max_sq_error = 1.e-29 |
---|
[215b33c] | 13 | |
---|
| 14 | def create_sine(hop_s, freq, samplerate): |
---|
| 15 | t = np.arange(hop_s).astype(float_type) |
---|
[fb434c4] | 16 | return np.sin( 2. * np.pi * freq * t / float(samplerate)) |
---|
[215b33c] | 17 | |
---|
| 18 | def create_noise(hop_s): |
---|
| 19 | return np.random.rand(hop_s).astype(float_type) * 2. - 1. |
---|
[0536612] | 20 | |
---|
| 21 | class aubio_pvoc_test_case(TestCase): |
---|
[467122d] | 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]) |
---|
[0536612] | 28 | |
---|
[467122d] | 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]) |
---|
[86ad546] | 33 | |
---|
[467122d] | 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]) |
---|
[0536612] | 38 | |
---|
[467122d] | 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) |
---|
[0b6d23d] | 44 | for _ in range( int ( 4 * win_s / hop_s ) ): |
---|
[467122d] | 45 | s = f(t) |
---|
| 46 | r = f.rdo(s) |
---|
[fb434c4] | 47 | assert_equal ( t, 0.) |
---|
| 48 | assert_equal ( s.norm, 0.) |
---|
| 49 | assert_equal ( s.phas, 0.) |
---|
| 50 | assert_equal ( r, 0.) |
---|
[0536612] | 51 | |
---|
[fb434c4] | 52 | @params( |
---|
| 53 | ( 256, 8), |
---|
| 54 | ( 256, 4), |
---|
| 55 | ( 256, 2), |
---|
| 56 | ( 512, 8), |
---|
| 57 | ( 512, 4), |
---|
| 58 | ( 512, 2), |
---|
[168a154] | 59 | #( 129, 2), |
---|
| 60 | #( 320, 4), |
---|
| 61 | #( 13, 8), |
---|
[fb434c4] | 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 | |
---|
[6937842] | 101 | def reconstruction(self, sigin, hop_s, ratio): |
---|
[ac65a2f] | 102 | buf_s = hop_s * ratio |
---|
[467122d] | 103 | f = pvoc(buf_s, hop_s) |
---|
[ac65a2f] | 104 | zeros = fvec(hop_s) |
---|
| 105 | r2 = f.rdo( f(sigin) ) |
---|
[0b6d23d] | 106 | for _ in range(1, ratio): |
---|
[ac65a2f] | 107 | r2 = f.rdo( f(zeros) ) |
---|
[215b33c] | 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) |
---|
[0536612] | 112 | |
---|
[168a154] | 113 | class 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 | |
---|
[dfada33] | 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 | |
---|
[168a154] | 151 | class 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) |
---|
[0b6d23d] | 181 | except AssertionError: |
---|
[168a154] | 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 | |
---|
[0536612] | 185 | if __name__ == '__main__': |
---|
[fb434c4] | 186 | main() |
---|
[0536612] | 187 | |
---|