Skip to content

Commit

Permalink
add resample warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Caroline Chen committed May 5, 2021
1 parent a8fbbda commit 5f3feed
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
20 changes: 19 additions & 1 deletion test/torchaudio_unittest/functional/functional_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from parameterized import parameterized
from scipy import signal

from torchaudio_unittest.common_utils import TestBaseMixin, get_sinusoid, nested_params
from torchaudio_unittest.common_utils import TestBaseMixin, get_sinusoid, nested_params, get_whitenoise


class Functional(TestBaseMixin):
Expand Down Expand Up @@ -259,6 +259,24 @@ def test_mask_along_axis_iid_preserve(self, mask_param, mask_value, axis):

self.assertEqual(specgrams, specgrams_copy)

def test_resample_no_warning(self):
sample_rate = 44100
waveform = get_whitenoise(sample_rate=sample_rate)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
F.resample(waveform, sample_rate, sample_rate / 2)
assert len(w) == 0

def test_resample_warning(self):
sample_rate = 44100
waveform = get_whitenoise(sample_rate=sample_rate)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
F.resample(waveform, sample_rate, 5512.5)
assert len(w) == 1


class FunctionalComplex(TestBaseMixin):
complex_dtype = None
Expand Down
10 changes: 10 additions & 0 deletions torchaudio/functional/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,16 @@ def resample(

assert orig_freq > 0.0 and new_freq > 0.0

if not (int(orig_freq) == orig_freq and int(new_freq) == new_freq):
warnings.warn(
"Non-integer frequencies are cast to integers and may result in poor resampling quality."
"The algorithm is dependent on the integer ratio between `orig_freq` and `new_freq`,"
"so please manually convert frequencies to integer values that maintain the ratio"
"prior to passing them into the function"
"Example: To downsample a 44100 hz waveform by a factor of 8, you can use"
"`orig_freq=8` and `new_freq=1` instead of `orig_freq=44100` and `new_freq=5512.5`"
)

orig_freq = int(orig_freq)
new_freq = int(new_freq)
gcd = math.gcd(orig_freq, new_freq)
Expand Down

0 comments on commit 5f3feed

Please sign in to comment.