def upsample2_test(self): """ Here testing upsampling with the following configurations (+ testing stereo): IN: | | | | OUT: | | | | | """ def gen(): """ [[0, 0], [-2, 2], [-4, 4]] [[-6, 6], [-8, 8], [-10, 10]] ... """ for i in range(0, 2): block_in = numpy.vstack([ numpy.arange(-i * 2 * 3, -(i + 1) * 2 * 3, -2), numpy.arange(i * 2 * 3, (i + 1) * 2 * 3, 2) ]).transpose() yield block_in resampler = stream_functions.resample(gen()) ratio = 3.0 / 2 resampler.set_ratio(ratio) # IN: 0 1 2 3 4 5 # OUT: 0 1 2 3 4 5 6 7 numpy.testing.assert_array_equal( stream_functions.concatenate(resampler).round(8), (numpy.array([ [0, 0], [-1, 1], [-2, 2], [-3, 3], [-4, 4], [-5, 5], [-6, 6], [-7, 7] ]) * 2 / ratio).round(8) )
def upsample1_test(self): """ Testing upsampling with the following configurations : IN: | | | OUT: | | | | | | | """ def gen(): """ [[0], [2], [4]] [[6], [8], [10]] ... """ for i in range(0, 2): yield numpy.arange(i * 2 * 3, (i + 1) * 2 * 3, 2).reshape(3, 1) resampler = stream_functions.resample(gen()) resampler.set_ratio(3.0) # IN: 0 1 2 3 4 5 # OUT: 0 1 2 3 4 5 6 7 8 9 a b c d e f numpy.testing.assert_array_equal( stream_functions.concatenate(resampler).round(8), (numpy.array([ [0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15] ]) * 2.0 / 3).round(8) )
def sanity_check_test(self): """ Test that something's not fundamentally wrong. """ def zcr_f0(samples): """ Calculate frequency using zero-crossings method. """ frame_rate = config.frame_rate frame_count = len(samples) crossings = (numpy.diff(numpy.sign(samples)) != 0) time = (numpy.ones(frame_count) / frame_rate).cumsum() - 1 / frame_rate half_oscillation_times = numpy.diff(time[crossings]) self.assertTrue(half_oscillation_times.std() < 0.00005) return 0.5 / half_oscillation_times.mean() frame_count = 44100 * 20 config.block_size = frame_count f0 = 440 ratio = 1/0.99999 time = numpy.arange(0, frame_count) / float(config.frame_rate) samples = numpy.cos(2 * numpy.pi * f0 * time) def gen(): yield samples.reshape(len(samples), 1) resampler = stream_functions.resample(gen()) resampler.set_ratio(ratio) self.assertEqual(round(zcr_f0(samples), 3), round(f0, 3)) samples2 = next(resampler)[:,0] self.assertEqual(round(zcr_f0(samples2), 3), round(f0 / ratio, 3))
def ratio1_test(self): """ Ratio 1 test. """ def gen(): """ [[0], [0.5], [1]] [[1.5], [2], [2.5]] ... """ for i in range(0, 2): yield numpy.arange(i * 3 * 0.5, (i + 1) * 3 * 0.5, 0.5).reshape(3, 1) resampler = stream_functions.resample(gen()) numpy.testing.assert_array_equal( stream_functions.concatenate(resampler).round(8), (numpy.array([[0], [1], [2], [3], [4], [5]]) * 0.5).round(8) )
def downsample3_test(self): """ Testing high downsampling, several blocks of incoming data fetched for one frame out. """ def gen(): """ [[0], [0.5], [1]] [[1.5], [2], [2.5]] ... """ for i in range(0, 6): yield numpy.arange(i * 3 * 0.5, (i + 1) * 3 * 0.5, 0.5).reshape(3, 1) resampler = stream_functions.resample(gen()) ratio = 1.0 / 8 resampler.set_ratio(ratio) # IN: 0 1 2 3 4 5 6 7 8 9 a b c d e f g h # OUT: 0 1 2 numpy.testing.assert_array_equal( stream_functions.concatenate(resampler).round(8), (numpy.array([[0], [1], [2]]) * (0.5 / ratio)).round(8) )
def downsample2_test(self): """ Testing downsampling with the following configurations: # IN: | | | | | | | | | # OUT: | | | """ def gen(): """ [[0], [0.5], [1]] [[1.5], [2], [2.5]] ... """ for i in range(0, 3): yield numpy.arange(i * 3 * 0.5, (i + 1) * 3 * 0.5, 0.5).reshape(3, 1) resampler = stream_functions.resample(gen()) ratio = 1.0 / 4 resampler.set_ratio(ratio) # IN: 0 1 2 3 4 5 6 7 8 # OUT: 0 1 2 numpy.testing.assert_array_equal( stream_functions.concatenate(resampler).round(8), (numpy.array([[0], [1], [2]]) * 0.5 / ratio).round(8) )