Context:
I'm trying to create a low pass filter to cut off frequencies above 10khz of a soundfile.
import librosa
import scipy.signal as sig
import numpy as np
import matplotlib.pyplot as plt
filename = librosa.example('nutcracker')
y, sr = librosa.load(filename)
# modeled after example in scipy.signal docs:
sos = sig.butter(10, 11, btype='lowpass', analog=False, output='sos')
filtered = sig.sosfilt(sos, y)
Now, I know what a low-pass filter does but not how it does it or the math behind it. So the first two arguments of scipy.signal.butter(N, Wn, ... )
are a little bit mysterious to me:
N : int
The order of the filter.
Wn : array_like
The critical frequency or frequencies. For lowpass and highpass filters, Wn is a scalar; for bandpass and bandstop filters, Wn is a length-2 sequence.
At first I thought Wn
, described as 'critical frequency` was the cutoff/threshold for the filter. However, setting it at anything over 1 results in an error telling me the value must be between 0 and 1.
Here's my work/research:
Googling 'low pass filter critical frequency' results in a lot of results about cut-off frequencies and corner frequencies, which sure seem to resemble my original idea of a 'cutoff point'.
I also found some formulas to calculate the cut-off frequency based on a filter's 'transfer function', but apparently there are many types of low pass filters, and each one might have a different transfer function.
This related question talks about Nyquist frequencies used to calculate
Wn
. I know what the Nyquist sampling rate is, which is apparently different. The Wikipedia article completely avoids talking about what nyquist frequency is conceptually.
My Questions:
Obviously I know almost nothing about signal processing except what I'm learning on the fly. Explain like I'm 5, please:
- What are the first two arguments of
signal.butter()
- How does changing these arguments functionally alter the filter?
- How do I calculate them?