10
10
sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), os .pardir ))
11
11
12
12
import lilfilter
13
-
13
+ import librosa
14
14
15
15
class TestResampler (unittest .TestCase ):
16
16
def test_constructor_and_forward (self ):
@@ -37,8 +37,6 @@ def test_energy(self):
37
37
n1 , n2 = pair
38
38
print ("n1,n2 = {},{}" .format (n1 , n2 ))
39
39
40
- a = lilfilter .Resampler (n1 , n2 , dtype = torch .float32 )
41
-
42
40
nyquist = math .pi * min (n2 / n1 , 1 )
43
41
omega = 0.85 * nyquist # enough less than nyquist that energy should be preserved.
44
42
length = 500
@@ -48,38 +46,50 @@ def test_energy(self):
48
46
49
47
input_energy = (signal * signal ).sum ().item ()
50
48
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 ))
54
49
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 ))
57
55
58
56
length = min (t .shape [1 ], signal .shape [1 ])
59
57
60
58
sig1 = signal [:,:length ]
61
59
sig2 = t [:,:length ]
62
-
63
-
64
60
prod1 = (sig1 * sig1 ).sum ()
65
61
prod2 = (sig2 * sig2 ).sum ()
66
62
prod3 = (sig1 * sig2 ).sum ()
67
63
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
+
68
71
69
- print ("The following numbers should be the same: {},{},{}" .format (
72
+ print ("The following [lilfilter] numbers should be the same: {},{},{}" .format (
70
73
prod1 , prod2 , prod3 ))
71
74
72
75
r1 = prod1 / prod2
73
76
r2 = prod2 / prod3
74
77
assert ( abs (r1 - 1.0 ) < 0.001 and abs (r2 - 1.0 ) < 0.001 )
75
78
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
+
76
86
77
87
#plt.plot(np.arange(length), sig1.squeeze(0).numpy())
78
88
#plt.plot(np.arange(length), sig2.squeeze(0).numpy())
79
89
#plt.show()
80
90
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 ] ))
83
93
84
94
85
95
0 commit comments