带阻滤波器不过滤预期频率
Bandstop filter doesn't filter expected frequency
我一直在使用 scipy 来尝试过滤信号。阅读文档并通过几个示例后,我以为我明白了,但似乎我做错了什么并且无法让它工作。
使用下面的带阻滤波器,我希望变量 fy 几乎保持不变,但我发现数据和滤波后的正弦波之间没有区别。
import pylab as plt
import numpy as np
import scipy
numsamples=1000
f0=1/10
x=np.linspace(0,100,numsamples)
y=np.sin(2*np.pi*f0*x)
fs=numsamples/(max(x)-min(x))
nyquist=0.5*fs
fstart=(3/4)*f0/nyquist
fstop=(4/3)*f0/nyquist
a,b = scipy.signal.butter(2,[fstart,fstop],'bandstop', analog=True)
fy = scipy.signal.lfilter(a,b,y, axis=-1, zi=None)
plt.plot(y)
plt.plot(fy)
plt.show()
感谢您的帮助,
麦克
我认为您应该从对 scipy.signal.butter 的调用中删除 "analog=True" 并使用默认的数字滤波器。当我对您的数据执行此操作时,效果非常好。
来自docs:
A scalar or length-2 sequence giving the critical frequencies. For a
Butterworth filter, this is the point at which the gain drops to
1/sqrt(2) that of the passband (the “-3 dB point”). For digital
filters, Wn is normalized from 0 to 1, where 1 is the Nyquist
frequency, pi radians/sample. (Wn is thus in half-cycles / sample.)
For analog filters, Wn is an angular frequency (e.g. rad/s).
因为您请求模拟滤波器,所以您不应该将起始频率和终止频率归一化为奈奎斯特频率。这仅适用于数字滤波器。
您想在这里使用模拟滤波器的原因是什么?我总是使用数字滤波器。
此外,考虑使用 scipy.signal.filtfilt
而不是 scipy.signal.lfilter
。参考文献:
How To apply a filter to a signal in python
我一直在使用 scipy 来尝试过滤信号。阅读文档并通过几个示例后,我以为我明白了,但似乎我做错了什么并且无法让它工作。
使用下面的带阻滤波器,我希望变量 fy 几乎保持不变,但我发现数据和滤波后的正弦波之间没有区别。
import pylab as plt
import numpy as np
import scipy
numsamples=1000
f0=1/10
x=np.linspace(0,100,numsamples)
y=np.sin(2*np.pi*f0*x)
fs=numsamples/(max(x)-min(x))
nyquist=0.5*fs
fstart=(3/4)*f0/nyquist
fstop=(4/3)*f0/nyquist
a,b = scipy.signal.butter(2,[fstart,fstop],'bandstop', analog=True)
fy = scipy.signal.lfilter(a,b,y, axis=-1, zi=None)
plt.plot(y)
plt.plot(fy)
plt.show()
感谢您的帮助,
麦克
我认为您应该从对 scipy.signal.butter 的调用中删除 "analog=True" 并使用默认的数字滤波器。当我对您的数据执行此操作时,效果非常好。
来自docs:
A scalar or length-2 sequence giving the critical frequencies. For a Butterworth filter, this is the point at which the gain drops to 1/sqrt(2) that of the passband (the “-3 dB point”). For digital filters, Wn is normalized from 0 to 1, where 1 is the Nyquist frequency, pi radians/sample. (Wn is thus in half-cycles / sample.) For analog filters, Wn is an angular frequency (e.g. rad/s).
因为您请求模拟滤波器,所以您不应该将起始频率和终止频率归一化为奈奎斯特频率。这仅适用于数字滤波器。
您想在这里使用模拟滤波器的原因是什么?我总是使用数字滤波器。
此外,考虑使用 scipy.signal.filtfilt
而不是 scipy.signal.lfilter
。参考文献:
How To apply a filter to a signal in python