Пример #1
0
 def test_fft_nofft(self):
     for _ in range(10):
         x = th.randn(1024)
         freq = random.uniform(0.01, 0.5)
         y_fft = lowpass_filter(x, freq, fft=True)
         y_ref = lowpass_filter(x, freq, fft=False)
         self.assertSimilar(y_fft, y_ref, x, f"freq={freq}", tol=0.01)
Пример #2
0
    def test_keep_or_kill(self):
        for _ in range(10):
            freq = random.uniform(0.01, 0.4)
            sr = 1024
            tone = pure_tone(freq * sr, sr=sr, dur=10)

            # For this test we accept 5% tolerance, as 5% of delta in amplitude is -52dB.
            tol = 5
            zeros = 16

            # If cutoff freauency is under freq, output should be zero
            y_killed = lowpass_filter(tone, 0.9 * freq, zeros=zeros)
            self.assertSimilar(y_killed,
                               0 * y_killed,
                               tone,
                               f"freq={freq}, kill",
                               tol=tol)

            # If cutoff freauency is under freq, output should be input
            y_pass = lowpass_filter(tone, 1.1 * freq, zeros=zeros)
            self.assertSimilar(y_pass,
                               tone,
                               tone,
                               f"freq={freq}, pass",
                               tol=tol)
    def apply_transform(
        self,
        samples: Tensor = None,
        sample_rate: Optional[int] = None,
        targets: Optional[Tensor] = None,
        target_rate: Optional[int] = None,
    ) -> ObjectDict:

        batch_size, num_channels, num_samples = samples.shape

        cutoffs_as_fraction_of_sample_rate = (
            self.transform_parameters["cutoff_freq"] / sample_rate
        )
        # TODO: Instead of using a for loop, perform batched compute to speed things up
        for i in range(batch_size):
            samples[i] = julius.lowpass_filter(
                samples[i], cutoffs_as_fraction_of_sample_rate[i].item()
            )

        return ObjectDict(
            samples=samples,
            sample_rate=sample_rate,
            targets=targets,
            target_rate=target_rate,
        )
Пример #4
0
 def test_same_as_downsample(self):
     for _ in range(10):
         x = th.randn(2 * 3 * 4 * 100)
         rolloff = 0.945
         for old_sr in [2, 3, 4]:
             y_resampled = resample_frac(x, old_sr, 1, rolloff=rolloff, zeros=16)
             y_lowpass = lowpass_filter(x, rolloff / old_sr / 2, stride=old_sr, zeros=16)
             self.assertSimilar(y_resampled, y_lowpass, x, f"old_sr={old_sr}")
Пример #5
0
def test(table, freq, zeros, fft=None, device="cpu"):
    sr = 44_100

    attns = []
    for ratio in [0.9, 1, 1.1]:
        x = pure_tone(ratio * freq * sr, sr, 4, device=device)
        with Chrono() as chrono:
            y = lowpass_filter(x, freq, fft=fft, zeros=zeros)
        attns.append(format(volume(y) - volume(x), ".2f"))

    table.line([freq] + attns + [int(1000 * chrono.duration)])
    def apply_transform(self,
                        selected_samples: torch.Tensor,
                        sample_rate: int = None):
        batch_size, num_channels, num_samples = selected_samples.shape

        if sample_rate is None:
            sample_rate = self.sample_rate

        cutoffs_as_fraction_of_sample_rate = (
            self.transform_parameters["cutoff_freq"] / sample_rate)
        # TODO: Instead of using a for loop, perform batched compute to speed things up
        for i in range(batch_size):
            selected_samples[i] = julius.lowpass_filter(
                selected_samples[i],
                cutoffs_as_fraction_of_sample_rate[i].item())

        return selected_samples
Пример #7
0
 def test_constant(self):
     x = th.ones(2048)
     for zeros in [4, 10]:
         for freq in [0.01, 0.1]:
             y_low = lowpass_filter(x, freq, zeros=zeros)
             self.assertLessEqual((y_low - 1).abs().mean(), 1e-6, (zeros, freq))