Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make parameter optional string #641

Merged
merged 3 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/torchscript_consistency_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def func(_):
f_max = 20.0
n_mels = 10
sample_rate = 16000
norm = ""
norm = "slaney"
return F.create_fb_matrix(n_stft, f_min, f_max, n_mels, sample_rate, norm)

dummy = torch.zeros(1, 1)
Expand Down
12 changes: 8 additions & 4 deletions torchaudio/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def create_fb_matrix(
f_max: float,
n_mels: int,
sample_rate: int,
norm: str = "",
norm: Optional[str] = None
) -> Tensor:
r"""Create a frequency bin conversion matrix.

Expand All @@ -346,8 +346,8 @@ def create_fb_matrix(
f_max (float): Maximum frequency (Hz)
n_mels (int): Number of mel filterbanks
sample_rate (int): Sample rate of the audio waveform
norm (str): If 'slaney', divide the triangular mel weights by the width of the mel band
(area normalization). (Default: '')
norm (Optional[str]): If 'slaney', divide the triangular mel weights by the width of the mel band
(area normalization). (Default: ``None``)

Returns:
Tensor: Triangular filter banks (fb matrix) of size (``n_freqs``, ``n_mels``)
Expand All @@ -356,6 +356,10 @@ def create_fb_matrix(
size (..., ``n_freqs``), the applied result would be
``A * create_fb_matrix(A.size(-1), ...)``.
"""

if norm is not None and norm != "slaney":
raise ValueError("norm must be one of None or 'slaney'")

# freq bins
# Equivalent filterbank construction by Librosa
all_freqs = torch.linspace(0, sample_rate // 2, n_freqs)
Expand All @@ -376,7 +380,7 @@ def create_fb_matrix(
up_slopes = slopes[:, 2:] / f_diff[1:] # (n_freqs, n_mels)
fb = torch.max(zero, torch.min(down_slopes, up_slopes))

if norm == "slaney":
if norm is not None and norm == "slaney":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not allow values other than "slaney" or None to prevent the case where unintended value is passed, like typo.

# Slaney-style mel is scaled to be approx constant energy per channel
enorm = 2.0 / (f_pts[2:n_mels + 2] - f_pts[:n_mels])
fb *= enorm.unsqueeze(0)
Expand Down