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

Replace indexing+matmul operation in lfilter with conv1d #1318

Merged
merged 6 commits into from
Feb 26, 2021
Merged
Changes from 3 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
30 changes: 8 additions & 22 deletions torchaudio/functional/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,34 +863,20 @@ def lfilter(
assert n_order > 0

# Pad the input and create output
padded_waveform = torch.zeros(
n_channel, n_sample_padded, dtype=dtype, device=device
)
padded_waveform[:, n_order - 1:] = waveform
padded_output_waveform = torch.zeros(
n_channel, n_sample_padded, dtype=dtype, device=device
)

padded_waveform = torch.nn.functional.pad(waveform, [n_order - 1, 0])
padded_output_waveform = torch.zeros_like(padded_waveform)

# Set up the coefficients matrix
# Flip coefficients' order
a_coeffs_flipped = a_coeffs.flip(0)
b_coeffs_flipped = b_coeffs.flip(0)

# calculate windowed_input_signal in parallel
# create indices of original with shape (n_channel, n_order, n_sample)
window_idxs = torch.arange(n_sample, device=device).unsqueeze(0) + torch.arange(
n_order, device=device
).unsqueeze(1)
window_idxs = window_idxs.repeat(n_channel, 1, 1)
window_idxs += (
torch.arange(n_channel, device=device).unsqueeze(-1).unsqueeze(-1)
* n_sample_padded
)
window_idxs = window_idxs.long()
# (n_order, ) matmul (n_channel, n_order, n_sample) -> (n_channel, n_sample)
input_signal_windows = torch.matmul(
b_coeffs_flipped, torch.take(padded_waveform, window_idxs)
)
# calculate windowed_input_signal in parallel using convolution
input_signal_windows = torch.nn.functional.conv1d(
padded_waveform.unsqueeze(1),
b_coeffs_flipped.view(1, 1, -1)
).squeeze(1)

input_signal_windows.div_(a_coeffs[0])
a_coeffs_flipped.div_(a_coeffs[0])
Expand Down