以下是一个示例的Python代码,实现了四个不同频率的四阶零相位巴特沃斯带通滤波器。

import numpy as np
from scipy.signal import butter, filtfilt

# 定义采样率和要滤波的频率范围
fs = 1000  # 采样率
lowcut1 = 1  # 低频截止频率1
highcut1 = 4  # 高频截止频率1
lowcut2 = 4  # 低频截止频率2
highcut2 = 8  # 高频截止频率2
lowcut3 = 8  # 低频截止频率3
highcut3 = 12  # 高频截止频率3
lowcut4 = 12  # 低频截止频率4
highcut4 = 30  # 高频截止频率4

# 计算巴特沃斯滤波器系数
nyquist = 0.5 * fs
b1, a1 = butter(4, [lowcut1 / nyquist, highcut1 / nyquist], btype='band')
b2, a2 = butter(4, [lowcut2 / nyquist, highcut2 / nyquist], btype='band')
b3, a3 = butter(4, [lowcut3 / nyquist, highcut3 / nyquist], btype='band')
b4, a4 = butter(4, [lowcut4 / nyquist, highcut4 / nyquist], btype='band')

# 模拟脑电波信号
t = np.linspace(0, 1, fs)
brain_wave = np.sin(2 * np.pi * 10 * t) + np.sin(2 * np.pi * 20 * t) + np.sin(2 * np.pi * 30 * t)

# 使用巴特沃斯滤波器进行滤波
filtered1 = filtfilt(b1, a1, brain_wave)
filtered2 = filtfilt(b2, a2, brain_wave)
filtered3 = filtfilt(b3, a3, brain_wave)
filtered4 = filtfilt(b4, a4, brain_wave)

# 绘制滤波前后的信号
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.plot(t, brain_wave)
plt.title('Original Signal')
plt.subplot(2, 2, 2)
plt.plot(t, filtered1)
plt.title('Filtered ({}-{} Hz)'.format(lowcut1, highcut1))
plt.subplot(2, 2, 3)
plt.plot(t, filtered2)
plt.title('Filtered ({}-{} Hz)'.format(lowcut2, highcut2))
plt.subplot(2, 2, 4)
plt.plot(t, filtered3)
plt.title('Filtered ({}-{} Hz)'.format(lowcut3, highcut3))
plt.show()

这段代码使用了scipy库中的butter函数来计算巴特沃斯滤波器的系数,并使用filtfilt函数对信号进行滤波。最后,使用matplotlib库绘制了滤波前后的信号图。根据需要,你可以根据自己的频率范围和信号进行相应的修改

四个不同的四阶零相位巴特沃斯带通滤波器:区分不同频率脑电波;给出python代码

原文地址: https://www.cveoy.top/t/topic/hZcQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录