forked from asteroid-team/torch-audiomentations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request asteroid-team#73 from asteroid-team/ij/use-new-rfft
Use torch.fft.rfft instead of the deprecated torch.rfft when possible
- Loading branch information
Showing
3 changed files
with
38 additions
and
29 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright Contributors to the Pyro project. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# Note: Code in this module has been copied from the https://github.com/pyro-ppl/pyro | ||
# repository, where the license was Apache 2.0. Any modifications to the original code can be | ||
# found at https://github.com/asteroid-team/torch-audiomentations/commits | ||
|
||
import torch | ||
|
||
try: | ||
# This works in PyTorch>=1.7 | ||
from torch.fft import irfft, rfft | ||
except ModuleNotFoundError: | ||
# This works in PyTorch<=1.6 | ||
def rfft(input, n=None): | ||
if n is not None: | ||
m = input.size(-1) | ||
if n > m: | ||
input = torch.nn.functional.pad(input, (0, n - m)) | ||
elif n < m: | ||
input = input[..., :n] | ||
return torch.view_as_complex(torch.rfft(input, 1)) | ||
|
||
def irfft(input, n=None): | ||
if torch.is_complex(input): | ||
input = torch.view_as_real(input) | ||
else: | ||
input = torch.nn.functional.pad(input[..., None], (0, 1)) | ||
if n is None: | ||
n = 2 * (input.size(-1) - 1) | ||
return torch.irfft(input, 1, signal_sizes=(n,)) |