【问题标题】:modelling a cut-off frequency using scipy使用 scipy 对截止频率进行建模
【发布时间】:2021-03-03 08:17:33
【问题描述】:

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

在 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

更新 - 对于 .butter、.sosfilt 和 .irrfilter、.lfilter 命令的较高频率(我使用 3000 Hz 截止频率)似乎会发生这种情况

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

【问题讨论】:

    标签: python scipy butterworth


    【解决方案1】:

    棘手。我认为您对时基有混淆。 f_s 应该是采样频率,并且您生成 f_s 样本,它总是一整秒。但是当 fc=3000 时,您只将 X 轴显示为 0 到 0.005 秒。此外,当 fc=15 时,您会生成从 0 到 1 的 f_s 时间样本。但是当 fc=3000 时,您的时基只会从 0 到 1/200,即使您应该生成一整秒数据。

    我把时基改成

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

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

    【讨论】:

    • 谢谢!为我节省了很多时间
    猜你喜欢
    • 2015-06-09
    • 2017-09-22
    • 2014-08-29
    • 2019-12-18
    • 2021-03-09
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    相关资源
    最近更新 更多