如何使用 Scipy.signal.butter 实现带通巴特沃斯滤波器
- 2025-04-16 08:56:00
- admin 原创
- 11
问题描述:
更新:
我根据这个问题找到了一个 Scipy Recipe!感兴趣的朋友可以直接访问:目录 » 信号处理 » 巴特沃斯带通
我很难完成最初看似简单的任务,即为 1-D numpy 数组(时间序列)实现巴特沃斯带通滤波器。
我必须包含的参数是采样率、以赫兹为单位的截止频率以及可能的顺序(其他参数,如衰减、固有频率等,对我来说比较模糊,因此任何“默认”值都可以)。
我现在有的是这个,它似乎可以用作高通滤波器,但我不确定我是否做得对:
def butter_highpass(interval, sampling_rate, cutoff, order=5):
nyq = sampling_rate * 0.5
stopfreq = float(cutoff)
cornerfreq = 0.4 * stopfreq # (?)
ws = cornerfreq/nyq
wp = stopfreq/nyq
# for bandpass:
# wp = [0.2, 0.5], ws = [0.1, 0.6]
N, wn = scipy.signal.buttord(wp, ws, 3, 16) # (?)
# for hardcoded order:
# N = order
b, a = scipy.signal.butter(N, wn, btype='high') # should 'high' be here for bandpass?
sf = scipy.signal.lfilter(b, a, interval)
return sf
文档和示例令人困惑且晦涩难懂,但我想实现推荐中标记为“用于带通”的格式。评论中的问号表明我只是复制粘贴了一些示例,并没有理解到底发生了什么。
我不是电气工程师或科学家,只是一名医疗设备设计师,需要对 EMG 信号执行一些相当简单的带通滤波。
解决方案 1:
您可以跳过使用 buttord,只需为滤波器选择一个阶数,然后查看它是否符合您的过滤标准。要生成带通滤波器的滤波器系数,请为 butter() 函数指定滤波器阶数、截止频率Wn=[lowcut, highcut]
、采样率fs
(与截止频率的单位相同)以及带类型btype="band"
。
这是一个脚本,定义了几个用于处理巴特沃斯带通滤波器的便捷函数。脚本运行时,它会生成两张图。一张图显示了相同采样率和截止频率下不同滤波器阶数的频率响应。另一张图展示了滤波器(阶数为 6)对样本时间序列的影响。
from scipy.signal import butter, lfilter
def butter_bandpass(lowcut, highcut, fs, order=5):
return butter(order, [lowcut, highcut], fs=fs, btype='band')
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = lfilter(b, a, data)
return y
if __name__ == "__main__":
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import freqz
# Sample rate and desired cutoff frequencies (in Hz).
fs = 5000.0
lowcut = 500.0
highcut = 1250.0
# Plot the frequency response for a few different orders.
plt.figure(1)
plt.clf()
for order in [3, 6, 9]:
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
w, h = freqz(b, a, fs=fs, worN=2000)
plt.plot(w, abs(h), label="order = %d" % order)
plt.plot([0, 0.5 * fs], [np.sqrt(0.5), np.sqrt(0.5)],
'--', label='sqrt(0.5)')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain')
plt.grid(True)
plt.legend(loc='best')
# Filter a noisy signal.
T = 0.05
nsamples = T * fs
t = np.arange(0, nsamples) / fs
a = 0.02
f0 = 600.0
x = 0.1 * np.sin(2 * np.pi * 1.2 * np.sqrt(t))
x += 0.01 * np.cos(2 * np.pi * 312 * t + 0.1)
x += a * np.cos(2 * np.pi * f0 * t + .11)
x += 0.03 * np.cos(2 * np.pi * 2000 * t)
plt.figure(2)
plt.clf()
plt.plot(t, x, label='Noisy signal')
y = butter_bandpass_filter(x, lowcut, highcut, fs, order=6)
plt.plot(t, y, label='Filtered signal (%g Hz)' % f0)
plt.xlabel('time (seconds)')
plt.hlines([-a, a], 0, T, linestyles='--')
plt.grid(True)
plt.axis('tight')
plt.legend(loc='upper left')
plt.show()
以下是该脚本生成的图表:
解决方案 2:
已接受答案中的滤波器设计方法是正确的,但它存在缺陷。使用 b、a 设计的 SciPy 带通滤波器不稳定,并且可能导致滤波器阶数较高时出现错误滤波器。
相反,使用滤波器设计的 sos(二阶部分)输出。
from scipy.signal import butter, sosfilt, sosfreqz
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
sos = butter(order, [low, high], analog=False, btype='band', output='sos')
return sos
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
sos = butter_bandpass(lowcut, highcut, fs, order=order)
y = sosfilt(sos, data)
return y
此外,您还可以通过改变
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
w, h = freqz(b, a, worN=2000)
到
sos = butter_bandpass(lowcut, highcut, fs, order=order)
w, h = sosfreqz(sos, worN=2000)
解决方案 3:
对于带通滤波器,ws 是一个包含下限和上限转折频率的元组。它们表示滤波器响应比通带低 3 dB 时的数字频率。
wp 是一个包含阻带数字频率的元组。它们表示最大衰减开始的位置。
gpass 是通带中的最大衰减(以 dB 为单位),而 gstop 是阻带中的衰减。
假设您想设计一个滤波器,采样率为每秒 8000 次,转折频率分别为 300 Hz 和 3100 Hz。奈奎斯特频率是采样率除以 2,在本例中为 4000 Hz。等效数字频率为 1.0。那么两个转折频率分别为 300/4000 和 3100/4000。
现在假设您希望阻带比转角频率低 30 dB +/- 100 Hz。这样,您的阻带将从 200 Hz 和 3200 Hz 开始,从而得到 200/4000 和 3200/4000 的数字频率。
要创建过滤器,你可以调用 buttord
fs = 8000.0
fso2 = fs/2
N,wn = scipy.signal.buttord(ws=[300/fso2,3100/fso2], wp=[200/fs02,3200/fs02],
gpass=0.0, gstop=30.0)
所得滤波器的长度取决于阻带的深度和响应曲线的陡度,后者由转角频率和阻带频率之间的差异决定。
扫码咨询,免费领取项目管理大礼包!