-
Notifications
You must be signed in to change notification settings - Fork 664
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
Speed up resample with kernel generation modification #2553
Changes from 4 commits
b2034e6
1db6103
f0f1b62
2b48d4c
715c4b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1414,7 +1414,6 @@ def _get_sinc_resample_kernel( | |
new_freq = int(new_freq) // gcd | ||
|
||
assert lowpass_filter_width > 0 | ||
kernels = [] | ||
base_freq = min(orig_freq, new_freq) | ||
# This will perform antialiasing filtering by removing the highest frequencies. | ||
# At first I thought I only needed this when downsampling, but when upsampling | ||
|
@@ -1445,31 +1444,33 @@ def _get_sinc_resample_kernel( | |
# There is probably a way to evaluate those filters more efficiently, but this is kept for | ||
# future work. | ||
idx_dtype = dtype if dtype is not None else torch.float64 | ||
idx = torch.arange(-width, width + orig_freq, device=device, dtype=idx_dtype) | ||
|
||
for i in range(new_freq): | ||
t = (-i / new_freq + idx / orig_freq) * base_freq | ||
t = t.clamp_(-lowpass_filter_width, lowpass_filter_width) | ||
idx = torch.arange(-width, width + orig_freq, dtype=idx_dtype)[None, None] / orig_freq | ||
|
||
# we do not use built in torch windows here as we need to evaluate the window | ||
# at specific positions, not over a regular grid. | ||
if resampling_method == "sinc_interpolation": | ||
window = torch.cos(t * math.pi / lowpass_filter_width / 2) ** 2 | ||
else: | ||
# kaiser_window | ||
if beta is None: | ||
beta = 14.769656459379492 | ||
beta_tensor = torch.tensor(float(beta)) | ||
window = torch.i0(beta_tensor * torch.sqrt(1 - (t / lowpass_filter_width) ** 2)) / torch.i0(beta_tensor) | ||
t *= math.pi | ||
kernel = torch.where(t == 0, torch.tensor(1.0).to(t), torch.sin(t) / t) | ||
kernel.mul_(window) | ||
kernels.append(kernel) | ||
t = torch.arange(0, -new_freq, -1, dtype=dtype)[:, None, None] / new_freq + idx | ||
t *= base_freq | ||
t = t.clamp_(-lowpass_filter_width, lowpass_filter_width) | ||
|
||
scale = base_freq / orig_freq | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. scale can be moved down closer to where it's actually being used, after the window parts |
||
kernels = torch.stack(kernels).view(new_freq, 1, -1).mul_(scale) | ||
|
||
if resampling_method == "sinc_interpolation": | ||
window = torch.cos(t * math.pi / lowpass_filter_width / 2) ** 2 | ||
else: | ||
# kaiser_window | ||
if beta is None: | ||
beta = 14.769656459379492 | ||
beta_tensor = torch.tensor(float(beta)) | ||
window = torch.i0(beta_tensor * torch.sqrt(1 - (t / lowpass_filter_width) ** 2)) / torch.i0(beta_tensor) | ||
|
||
t *= torch.pi | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can leave it using math.pi |
||
|
||
kernels = torch.where(t == 0, torch.tensor(1.0).to(t), t.sin() / t) | ||
kernels *= window * scale | ||
kernels.to(device) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
|
||
if dtype is None: | ||
kernels = kernels.to(dtype=torch.float32) | ||
|
||
return kernels, width | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you leave this comment in?