def test_resampy(self): old_sr = 3 new_sr = 2 x = pure_tone(7, sr=128, dur=3) + pure_tone(24, sr=128, dur=3) y_re = th.from_numpy(resampy.resample(x.numpy(), old_sr, new_sr)).float() y = resample.resample_frac(x, old_sr, new_sr) self.assertSimilar(y, y_re, x, f"{old_sr} to {new_sr}") old_sr = 2 new_sr = 5 x = pure_tone(7, sr=128) + pure_tone(48, sr=128) y_re = th.from_numpy(resampy.resample(x.numpy(), old_sr, new_sr)).float() y = resample.resample_frac(x, old_sr, new_sr) self.assertSimilar(y, y_re, x, f"{old_sr} to {new_sr}") random.seed(1234) th.manual_seed(1234) for _ in range(10): old_sr = random.randrange(8, 128) new_sr = random.randrange(8, 128) x = th.randn(1024) y_re = th.from_numpy(resampy.resample(x.numpy(), old_sr, new_sr)).float() y = resample.resample_frac(x, old_sr, new_sr, zeros=56) # We allow some relatively high tolerance as we are not using the same window. self.assertSimilar(y, y_re, x, f"{old_sr} to {new_sr}", tol=2)
def test_full(self): x = th.randn(19) y = resample.resample_frac(x, 7, 1, full=True) self.assertEqual(len(y), 3) z = resample.resample_frac(y, 5, 1, full=True) y2 = resample.resample_frac(z, 1, 5, full=True) x2 = resample.resample_frac(y2, 1, 7, output_length=len(x)) self.assertEqual(x.shape, x2.shape)
def test_custom_output_length_out_of_range(self): x = th.ones(1, 32000) with self.assertRaisesRegex( ValueError, "output_length must be between 0 and 48000"): resample.resample_frac(x, old_sr=32000, new_sr=48000, output_length=48002)
def test_ref(self): # Compare to _upsample2 and _downsample2 for freq in [8, 16, 20, 28, 32, 36, 40, 56, 64]: x = pure_tone(freq, sr=128) y_gt_down = resample._downsample2(x) y_down = resample.resample_frac(x, 2, 1, rolloff=1) self.assertSimilar(y_down, y_gt_down, x, f"freq={freq} down") y_gt_up = resample._upsample2(x) y_up = resample.resample_frac(x, 1, 2, rolloff=1) self.assertSimilar(y_up, y_gt_up, x, f"freq={freq} up")
def test_default_output_length(self): x = th.ones(1, 2, 32000) resampler = resample.ResampleFrac(old_sr=32000, new_sr=48000) y = resampler(x) self.assertEqual(y.shape, (1, 2, 48000)) # Test functional version as well y = resample.resample_frac(x, old_sr=32000, new_sr=48000) self.assertEqual(y.shape, (1, 2, 48000))
def test_constant(self): x = th.ones(4096) for zeros in [4, 10]: for old_sr in [1, 4, 10]: for new_sr in [1, 4, 10]: y_low = resample.resample_frac(x, old_sr, new_sr, zeros=zeros) self.assertLessEqual((y_low - 1).abs().mean(), 1e-6, (zeros, old_sr, new_sr))
def test_custom_output_length(self): x = th.ones(1, 32001) resampler = resample.ResampleFrac(old_sr=32000, new_sr=48000) y = resampler(x, output_length=48001) self.assertEqual(y.shape, (1, 48001)) # Test functional version as well y = resample.resample_frac(x, old_sr=32000, new_sr=48000, output_length=47999) self.assertEqual(y.shape, (1, 47999))
def test_custom_output_length_extreme_resampling(self): """ Resample a signal from 1 hz to 499 hz to check that custom_length works correctly without extra internal padding """ x = th.ones(1, 1) resampler = resample.ResampleFrac(old_sr=1, new_sr=499) y = resampler(x, output_length=499) self.assertEqual(y.shape, (1, 499)) # Test functional version as well y = resample.resample_frac(x, old_sr=1, new_sr=499, output_length=3) self.assertEqual(y.shape, (1, 3))