【发布时间】: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
【问题讨论】:
标签: python scipy butterworth