You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PaddleSpeech/tests/unit/audiotools/core/test_bands.py

55 lines
1.5 KiB

# MIT License, Copyright (c) 2020 Alexandre Défossez.
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Modified from julius(https://github.com/adefossez/julius/blob/main/tests/test_bands.py)
import random
import sys
import unittest
import paddle
from paddlespeech.audiotools.core import pure_tone
from paddlespeech.audiotools.core import split_bands
from paddlespeech.audiotools.core import SplitBands
def delta(a, b, ref, fraction=0.9):
length = a.shape[-1]
compare_length = int(length * fraction)
offset = (length - compare_length) // 2
a = a[..., offset:offset + length]
b = b[..., offset:offset + length]
return 100 * paddle.abs(a - b).mean() / ref.std()
TOLERANCE = 0.5 # Tolerance to errors as percentage of the std of the input signal
class _BaseTest(unittest.TestCase):
def assertSimilar(self, a, b, ref, msg=None, tol=TOLERANCE):
self.assertLessEqual(delta(a, b, ref), tol, msg)
class TestLowPassFilters(_BaseTest):
def setUp(self):
paddle.seed(1234)
random.seed(1234)
def test_keep_or_kill(self):
sr = 256
low = pure_tone(10, sr)
mid = pure_tone(40, sr)
high = pure_tone(100, sr)
x = low + mid + high
decomp = split_bands(x, sr, cutoffs=[20, 70])
self.assertEqual(len(decomp), 3)
for est, gt, name in zip(decomp, [low, mid, high],
["low", "mid", "high"]):
self.assertSimilar(est, gt, gt, name)
if __name__ == "__main__":
unittest.main()