def test_resampled(self): original_frequency = Milliseconds(500) original_duration = Seconds(1) ratio = 0.02 orig_sr = SampleRate(original_frequency, original_duration) new_sr = orig_sr.resample(ratio) self.assertEqual(Milliseconds(10), new_sr.frequency) self.assertEqual(Milliseconds(20), new_sr.duration)
def unpackPartition(unpacker): partition = {} partition['start_time'] = unpacker.unpack_uhyper() partition['end_time'] = unpacker.unpack_uhyper() unpacker.unpack_int() unpacker.unpack_int() sampleRateType = unpacker.unpack_uint() partition['sample_rate'] = SampleRate.hertz(unpacker.unpack_uint()) if sampleRateType == 1 else \ SampleRate.seconds(unpacker.unpack_uint()) for _ in range(unpacker.unpack_uint()): popSize = unpacker.unpack_uint() * 12 unpacker.unpack_fopaque(popSize) return timeseries.descriptor(partition['sample_rate']), partition
def unpackPartition(unpacker): partition = {} partition['start_time'] = unpacker.unpack_uhyper() partition['end_time'] = unpacker.unpack_uhyper() unpacker.unpack_int() unpacker.unpack_int() sampleRateType = unpacker.unpack_uint() partition['sample_rate'] = SampleRate.hertz(unpacker.unpack_uint()) if sampleRateType == 1 else \ SampleRate.seconds(unpacker.unpack_uint()) partition['num_bins'] = unpacker.unpack_uint() partition['bin_start'] = unpacker.unpack_float() partition['bin_size'] = unpacker.unpack_float() return histogram.descriptor(partition['sample_rate'], partition['bin_start'], partition['bin_size'], partition['num_bins']), partition
def test_sliding_window_dimensions(self): samplerate = SR11025() samples = AudioSamples.silence(SR11025(), Seconds(10)) window_sr = SampleRate( duration=samplerate.frequency * 512, frequency=samplerate.frequency * (512 - 25)) windowed = samples.sliding_window(window_sr) self.assertEqual(window_sr, windowed.dimensions[0].samplerate)
def test_sliding_window(self): samples = AudioSamples.silence(SR11025(), Seconds(30)) sr = samples.samplerate * Stride(frequency=16, duration=512) windowed = samples.sliding_window(sr) self.assertEqual((512,), windowed.shape[1:]) long_sr = SampleRate( frequency=sr.frequency * 2, duration=sr.frequency * 32) frequency = TimeSlice(duration=long_sr.frequency) duration = TimeSlice(duration=long_sr.duration) _, long_windowed = windowed.sliding_window_with_leftovers( windowsize=duration, stepsize=frequency, dopad=True) self.assertEqual((32, 512), long_windowed.shape[1:]) self.assertEqual(3, long_windowed.ndim)
def test_overlap_ratio_type(self): sr = SampleRate(frequency=Milliseconds(500), duration=Seconds(1)) self.assertIsInstance(sr.overlap_ratio, float)
def test_overlap_ratio_half(self): sr = SampleRate(frequency=Milliseconds(500), duration=Seconds(1)) self.assertEqual(0.5, sr.overlap_ratio)
def test_overlap_ratio_zero(self): sr = SampleRate(frequency=Seconds(1), duration=Seconds(1)) self.assertEqual(0, sr.overlap_ratio)
def test_multiply_some_overlap_two_values(self): sr = SampleRate(Seconds(1), Seconds(2)) * (2, 4) self.assertEqual(Seconds(2), sr.frequency) self.assertEqual(Seconds(5), sr.duration)
def test_multiply_some_overlap_single_value(self): sr = SampleRate(Seconds(1), Seconds(2)) * (2, ) self.assertEqual(Seconds(2), sr.frequency) self.assertEqual(Seconds(3), sr.duration)
def test_raises_value_error_for_zero_frequency(self): self.assertRaises(ValueError, lambda: SampleRate(Seconds(0), Seconds(1)))
def test_some_overlap(self): self.assertEqual(Seconds(1), SampleRate(Seconds(1), Seconds(2)).overlap)
def test_no_overlap(self): self.assertEqual(Seconds(0), SampleRate(Seconds(1), Seconds(1)).overlap)
def samplerate(self): return SampleRate(self.frequency, self.duration)
def test_raises_if_not_audio_samplerate(self): arr = np.zeros(int(44100 * 2.5)) one = Seconds(1) self.assertRaises( TypeError, lambda: AudioSamples(arr, SampleRate(one, one)))
def test_raises_value_error_for_negative_duration(self): self.assertRaises(ValueError, lambda: SampleRate(Seconds(1), Seconds(-1)))
def test_can_unpack_samplerate(self): sr = SampleRate(Seconds(1), Seconds(2)) frequency, duration = sr self.assertEqual(Seconds(1), frequency) self.assertEqual(Seconds(2), duration)
def test_multiply_some_overlap_number(self): sr = SampleRate(Seconds(1), Seconds(2)) * 2 self.assertEqual(Seconds(2), sr.frequency) self.assertEqual(Seconds(3), sr.duration)