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

Fir filter input #65

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add butter filter
  • Loading branch information
jmoso13 committed Sep 9, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 92cf006700d6d00a4c31a7f71587bc29a2ff9d48
29 changes: 28 additions & 1 deletion auraloss/perceptual.py
Original file line number Diff line number Diff line change
@@ -55,14 +55,17 @@ class FIRFilter(torch.nn.Module):
a sampling rate of 44.1 kHz, considering adjusting this value at differnt sampling rates.
"""

def __init__(self, filter_type="hp", coef=0.85, fs=44100, ntaps=101, plot=False):
def __init__(self, filter_type="hp", coef=0.85, fs=44100, ntaps=101, butter_order=2, butter_freq=(250, 5000), butter_filter_type="bandpass", plot=False):
Copy link
Owner

Choose a reason for hiding this comment

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

I think adding the butterworth parameters to this constructor make the behavior a bit confusing since we also have the filter_type parameter which will get ignored. I do not know the best solution yet, but it feels like we should instead just pass taps into this constructor. Then we will need to supply some helper functions for hp and butterworth filters that will produce a tensor of taps. What do you think?

"""Initilize FIR pre-emphasis filtering module."""
super(FIRFilter, self).__init__()
self.filter_type = filter_type
self.coef = coef
self.fs = fs
self.ntaps = ntaps
self.plot = plot
self.butter_order = butter_order
self.butter_freq = butter_freq
self.butter_filter_type = butter_filter_type

import scipy.signal

@@ -113,6 +116,30 @@ def __init__(self, filter_type="hp", coef=0.85, fs=44100, ntaps=101, plot=False)
if plot:
from .plotting import compare_filters
compare_filters(b, a, taps, fs=fs)
elif filter_type == "butter":
# Define butter filter
filts = signal.lti(*signal.butter(self.butter_order, self.butter_freq, self.butter_filter_type, analog=True))

# convert analog filter to digital filter
b, a = scipy.signal.bilinear(filts.num, filts.den, fs=fs)

# compute the digital filter frequency response
w_iir, h_iir = scipy.signal.freqz(b, a, worN=512, fs=fs)

# then we fit to 101 tap FIR filter with least squares
taps = scipy.signal.firls(ntaps, w_iir, abs(h_iir), fs=fs)

# now implement this digital FIR filter as a Conv1d layer
self.fir = torch.nn.Conv1d(
1, 1, kernel_size=ntaps, bias=False, padding=ntaps // 2
)
self.fir.weight.requires_grad = False
self.fir.weight.data = torch.tensor(taps.astype("float32")).view(1, 1, -1)

if plot:
from .plotting import compare_filters
compare_filters(b, a, taps, fs=fs)


def forward(self, input, target):
"""Calculate forward propagation.