Skip to content

Commit 696f44b

Browse files
author
zhuang
committed
Merge branch 'master' of https://github.com/danpovey/filtering
2 parents 8d3b603 + bd3581a commit 696f44b

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

tests/test_resampler.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir))
1111

1212
import lilfilter
13-
13+
import librosa
1414

1515
class TestResampler(unittest.TestCase):
1616
def test_constructor_and_forward(self):
@@ -37,8 +37,6 @@ def test_energy(self):
3737
n1, n2 = pair
3838
print("n1,n2 = {},{}".format(n1, n2))
3939

40-
a = lilfilter.Resampler(n1, n2, dtype=torch.float32)
41-
4240
nyquist = math.pi * min(n2 / n1, 1)
4341
omega = 0.85 * nyquist # enough less than nyquist that energy should be preserved.
4442
length = 500
@@ -48,38 +46,50 @@ def test_energy(self):
4846

4947
input_energy = (signal * signal).sum().item()
5048
print("Energy of input signal is ", input_energy)
51-
s = a.resample(signal)
52-
s2 = lilfilter.resample(signal, n1, n2)
53-
assert torch.all(torch.eq(s, s2))
5449

55-
b = lilfilter.Resampler(n2, n1, dtype=torch.float32)
56-
t = b.resample(s)
50+
s = lilfilter.resample(signal, n1, n2)
51+
s_rosa = torch.tensor(librosa.core.resample(signal.numpy(), n1, n2))
52+
53+
t = lilfilter.resample(s, n2, n1)
54+
t_rosa = torch.tensor(librosa.core.resample(s_rosa.numpy(), n2, n1))
5755

5856
length = min(t.shape[1], signal.shape[1])
5957

6058
sig1 = signal[:,:length]
6159
sig2 = t[:,:length]
62-
63-
6460
prod1 = (sig1 * sig1).sum()
6561
prod2 = (sig2 * sig2).sum()
6662
prod3 = (sig1 * sig2).sum()
6763

64+
length_rosa = min(t_rosa.shape[1], signal.shape[1])
65+
sig1_rosa = signal[:,:length_rosa]
66+
sig2_rosa = t_rosa[:,:length_rosa]
67+
prod1_rosa = (sig1_rosa * sig1_rosa).sum()
68+
prod2_rosa = (sig2_rosa * sig2_rosa).sum()
69+
prod3_rosa = (sig1_rosa * sig2_rosa).sum()
70+
6871

69-
print("The following numbers should be the same: {},{},{}".format(
72+
print("The following [lilfilter] numbers should be the same: {},{},{}".format(
7073
prod1, prod2, prod3))
7174

7275
r1 = prod1 / prod2
7376
r2 = prod2 / prod3
7477
assert( abs(r1-1.0) < 0.001 and abs(r2-1.0) < 0.001)
7578

79+
print("The following [librosa] numbers should be the same: {},{},{}".format(
80+
prod1_rosa, prod2_rosa, prod3_rosa))
81+
82+
r1_rosa = prod1_rosa / prod2_rosa
83+
r2_rosa = prod2_rosa / prod3_rosa
84+
#assert( abs(r1_rosa-1.0) < 0.001 and abs(r2_rosa-1.0) < 0.001)
85+
7686

7787
#plt.plot(np.arange(length), sig1.squeeze(0).numpy())
7888
#plt.plot(np.arange(length), sig2.squeeze(0).numpy())
7989
#plt.show()
8090

81-
print("Length of input signal is {}, downsampled {}, reconstructed {}".format(
82-
signal.shape[-1], s.shape[-1], t.shape[-1]))
91+
print("Length of input signal is {}, downsampled {}, reconstructed {}, librosa-reconstructed".format(
92+
signal.shape[-1], s.shape[-1], t.shape[-1], t_rosa.shape[-1]))
8393

8494

8595

0 commit comments

Comments
 (0)