使用 scipy 对截止频率建模

modelling a cut-off frequency using scipy

我正在努力为扬声器编写交叉滤波器。此函数旨在创建一个虚拟输入信号,然后可以对其进行过滤,但是当我在调用该函数时更改频率和顺序时,它将不再在图表上正确绘制过滤后的频率。

在 scipy 网站上,他们说建议在过滤时使用二阶截面格式,以避免传递函数 (ba) 格式出现数值错误,但我现在不确定这是最好的解决方案或者如果我在这里忽略了一个简单的错误。

参见:

sosfilt:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.sosfilt.html#scipy.signal.sosfilt

黄油: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.butter.html#scipy.signal.butter

更新 - 这似乎发生在更高频率(我使用 3000 Hz 截止频率)以及 .butter、.sosfilt 和 .irrfilter、.lfilter 命令

from scipy.signal import butter, sosfilt
import matplotlib.pyplot as plt
import numpy as np
def demo(f_s=1000,fc=15,fmin=10,fmax=20,n=10,nf=2):
    # demo filtered signal
    t = np.linspace(0,(15/fc),f_s,False) # sample size should give roughly ten peaks
    # generate signal
    inputf = np.linspace(fmin,fmax,nf) 
    # starts off with only the two frequencies,
    # can add more to check how the filter will respond
    sig = 0   
    for i in inputf:
        sig += np.sin(2*np.pi*i*t)
    # returns the filtered signal over a given time
    fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
    ax1.plot(t, sig)
    ax1.set_title('Initail sinusoidial signal from {} to {} Hz'.format(fmin,fmax))
    ax1.axis([0, (15/fc), -nf, nf])
    sosh = butter(n, fc, 'hp', fs=f_s, output='sos') #highpass
    sosl = butter(n, fc, 'lp', fs=f_s, output='sos') #lowpass
    filteredh = sosfilt(sosh, sig) #filtered high-pass
    filteredl = sosfilt(sosl, sig) #filtered low-pass
    ax2.plot(t, filteredh) # visualised high-pass signal
    ax2.plot(t, filteredl) # visualised low-pass signal
    ax2.set_title('After {} Hz Cut-off (high and low)-pass filter'.format(fc))
    ax2.axis([0, (15/fc), -nf, nf])
    ax2.set_xlabel('Time [seconds]')
    plt.tight_layout()
    plt.show()
demo()

This is the filter response the code needs to match for values that are not preset

This is what I'm currently getting

棘手。我认为您对时基感到困惑。 f_s 应该是采样频率,你生成 f_s 个样本,这总是一整秒。但是当 fc=3000 时,您只将 X 轴显示为 0 到 .005 秒。此外,当 fc=15 时,您会生成 f_s 时间样本 运行ning 从 0 到 1。但是当 fc=3000 时,您的时基只会 运行 从 0 到 1/200,即使你应该生成一整秒的数据。

我把时基改成了

    t = np.linspace(0,1,f_s,False)

而且我认为它更接近您的预期。