Skip to content

Commit 06b2c3f

Browse files
committed
fix resampling scale for CQT
undid test relaxation for cqt position updated image comparison test fixtures for cqt-based outputs fixed resample-scaling, should be by sqrt(ratio)
1 parent 2694e95 commit 06b2c3f

File tree

7 files changed

+18
-11
lines changed

7 files changed

+18
-11
lines changed

librosa/core/audio.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def to_mono(y):
203203

204204

205205
@cache
206-
def resample(y, orig_sr, target_sr, res_type='sinc_best', fix=True, **kwargs):
206+
def resample(y, orig_sr, target_sr, res_type='sinc_best', fix=True, scale=False, **kwargs):
207207
"""Resample a time series from orig_sr to target_sr
208208
209209
Parameters
@@ -232,6 +232,10 @@ def resample(y, orig_sr, target_sr, res_type='sinc_best', fix=True, **kwargs):
232232
adjust the length of the resampled signal to be of size exactly
233233
`ceil(target_sr * len(y) / orig_sr)`
234234
235+
scale : bool
236+
Scale the resampled signal so that `y` and `y_hat` have approximately
237+
equal total energy.
238+
235239
kwargs : additional keyword arguments
236240
If `fix==True`, additional keyword arguments to pass to
237241
`librosa.util.fix_length`.
@@ -271,20 +275,23 @@ def resample(y, orig_sr, target_sr, res_type='sinc_best', fix=True, **kwargs):
271275
if orig_sr == target_sr:
272276
return y
273277

274-
n_samples = int(np.ceil(y.shape[-1] * float(target_sr) / orig_sr))
278+
ratio = float(target_sr) / orig_sr
279+
280+
n_samples = int(np.ceil(y.shape[-1] * ratio))
275281

276282
scipy_resample = (res_type == 'scipy')
277283

278284
if _HAS_SAMPLERATE and not scipy_resample:
279-
y_hat = samplerate.resample(y.T,
280-
float(target_sr) / orig_sr,
281-
res_type).T
285+
y_hat = samplerate.resample(y.T, ratio, res_type).T
282286
else:
283287
y_hat = scipy.signal.resample(y, n_samples, axis=-1)
284288

285289
if fix:
286290
y_hat = util.fix_length(y_hat, n_samples, **kwargs)
287291

292+
if scale:
293+
y_hat /= np.sqrt(ratio)
294+
288295
return np.ascontiguousarray(y_hat, dtype=y.dtype)
289296

290297

librosa/core/constantq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def cqt(y, sr=22050, hop_length=512, fmin=None, n_bins=84,
248248

249249
# Resample (except first time)
250250
if i > 0:
251-
my_y = audio.resample(my_y, my_sr, my_sr/2.0, res_type=res_type)
251+
my_y = audio.resample(my_y, my_sr, my_sr/2.0, res_type=res_type, scale=True)
252252
my_sr /= 2.0
253253
assert my_hop % 2 == 0
254254
my_hop //= 2
@@ -593,7 +593,7 @@ def __early_downsample(y, sr, hop_length, res_type, n_octaves,
593593
assert hop_length % downsample_factor == 0
594594
hop_length //= downsample_factor
595595

596-
y = audio.resample(y, sr, sr / downsample_factor, res_type=res_type)
596+
y = audio.resample(y, sr, sr / downsample_factor, res_type=res_type, scale=True)
597597

598598
sr /= downsample_factor
599599

-4.06 KB
Loading
-7.11 KB
Loading
-87 Bytes
Loading

tests/test_constantq.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ def __test(note_min):
169169
# Make sure that the max outside the peak is sufficiently small
170170
Cscale = Cbar / Cbar[idx]
171171
Cscale[idx] = np.nan
172-
173172
assert np.nanmax(Cscale) < 1e-1
174173

175174
Cscale[idx-1:idx+2] = np.nan

tests/test_core.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ def test_segment_load():
8181

8282
def test_resample_mono():
8383

84-
def __test(y, sr_in, sr_out, res_type, fix):
84+
def __test(y, sr_in, sr_out, res_type, fix, scale):
8585

8686
y2 = librosa.resample(y, sr_in, sr_out,
8787
res_type=res_type,
88-
fix=fix)
88+
fix=fix, scale=scale)
8989

9090
# First, check that the audio is valid
9191
librosa.util.valid_audio(y2, mono=True)
@@ -109,7 +109,8 @@ def __test(y, sr_in, sr_out, res_type, fix):
109109
for sr_out in [8000, 22050]:
110110
for res_type in ['sinc_fastest', 'sinc_best', 'scipy']:
111111
for fix in [False, True]:
112-
yield (__test, y, sr_in, sr_out, res_type, fix)
112+
for scale in [False, True]:
113+
yield (__test, y, sr_in, sr_out, res_type, fix, scale)
113114

114115

115116
def test_resample_stereo():

0 commit comments

Comments
 (0)