def test_stft_raises_for_invalid_dimensions(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666)) wscheme = sr.windowing_scheme(512, 256) tf = stft(samples, wscheme, HanningWindowingFunc()) self.assertRaises(ValueError, lambda: stft(tf, wscheme, HanningWindowingFunc()))
def test_has_correct_shape(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(9999)) wscheme = sr.windowing_scheme(256, 128) scale = GeometricScale(50, sr.nyquist, 0.4, 32) scale.ensure_overlap_ratio() tf = stft(samples, wscheme, HanningWindowingFunc()) geom = apply_scale(tf, scale, window=HanningWindowingFunc()) self.assertEqual(tf.shape[:-1] + (len(scale), ), geom.shape)
def test_apply_scale_to_self_is_identity_function(self): samplerate = SR22050() samples = SineSynthesizer(samplerate).synthesize(Milliseconds(8888)) wscheme = samplerate.windowing_scheme(256, 128) tf = stft(samples, wscheme, HanningWindowingFunc()) scale = tf.dimensions[-1].scale transformed = apply_scale(tf, scale, HanningWindowingFunc()) self.assertEqual(tf.shape, transformed.shape) self.assertEqual(tf.dimensions[0], transformed.dimensions[0]) self.assertEqual(tf.dimensions[1], transformed.dimensions[1])
def test_has_correct_dimensions(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666)) wscheme = sr.windowing_scheme(512, 256) tf = stft(samples, wscheme, HanningWindowingFunc()) self.assertIsInstance(tf, ArrayWithUnits) self.assertEqual(2, len(tf.dimensions)) self.assertIsInstance(tf.dimensions[0], TimeDimension) self.assertEqual(tf.dimensions[0].samplerate, wscheme) self.assertIsInstance(tf.dimensions[1], FrequencyDimension) self.assertIsInstance(tf.dimensions[1].scale, LinearScale)
def test_should_have_correct_shape_and_dimensions(self): samplerate = SR22050() samples = SineSynthesizer(samplerate).synthesize(Milliseconds(8888)) wscheme = samplerate.windowing_scheme(256, 128) tf = stft(samples, wscheme, HanningWindowingFunc()) rg = rainbowgram(tf, cm.rainbow) self.assertEqual(3, rg.ndim) self.assertEqual(tf.shape + (3, ), rg.shape) self.assertEqual(3, len(rg.dimensions)) self.assertEqual(tf.dimensions[0], rg.dimensions[0]) self.assertEqual(tf.dimensions[1], rg.dimensions[1]) self.assertEqual(rg.dimensions[2], IdentityDimension()) self.assertEqual(3, rg.shape[-1])
def test_has_correct_dimensions(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(9999)) wscheme = sr.windowing_scheme(256, 128) scale = GeometricScale(50, sr.nyquist, 0.4, 32) scale.ensure_overlap_ratio() tf = stft(samples, wscheme, HanningWindowingFunc()) geom = apply_scale(tf, scale, window=HanningWindowingFunc()) self.assertIsInstance(geom, ArrayWithUnits) self.assertEqual(2, len(geom.dimensions)) self.assertIsInstance(geom.dimensions[0], TimeDimension) self.assertEqual(geom.dimensions[0].samplerate, wscheme) self.assertIsInstance(geom.dimensions[1], FrequencyDimension) self.assertEqual(scale, geom.dimensions[1].scale)
def test_preserves_time_dimension(self): sr = SR22050() samples = TickSynthesizer(sr).synthesize(Milliseconds(10000), Milliseconds(1000)) wscheme = sr.windowing_scheme(256, 128) scale = GeometricScale(50, sr.nyquist, 0.4, 32) scale.ensure_overlap_ratio() tf = stft(samples, wscheme, HanningWindowingFunc()) geom = apply_scale(tf, scale, window=HanningWindowingFunc()) # get the loudness envelope of each tf_envelope = np.abs(tf.real).sum(axis=1) geom_envelope = geom.sum(axis=1) tf_zeros = np.where(tf_envelope == 0) geom_zeros = np.where(geom_envelope == 0) np.testing.assert_allclose(tf_zeros, geom_zeros)
def test_can_take_stft_of_batch(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666)) stacked = ArrayWithUnits( np.zeros((10, ) + samples.shape, dtype=samples.dtype), (IdentityDimension(), ) + samples.dimensions) stacked[:] = samples wscheme = sr.windowing_scheme(512, 256) tf = stft(stacked, wscheme, HanningWindowingFunc()) self.assertEqual(10, len(tf)) self.assertIsInstance(tf, ArrayWithUnits) self.assertEqual(3, len(tf.dimensions)) self.assertIsInstance(tf.dimensions[0], IdentityDimension) self.assertIsInstance(tf.dimensions[1], TimeDimension) self.assertEqual(tf.dimensions[1].samplerate, wscheme) self.assertIsInstance(tf.dimensions[2], FrequencyDimension) self.assertIsInstance(tf.dimensions[2].scale, LinearScale)
def test_has_correct_number_of_bins(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666)) wscheme = sr.windowing_scheme(512, 256) tf = stft(samples, wscheme, HanningWindowingFunc()) self.assertEqual(tf.shape[1], 257)