在Python中应用巴特沃斯带通滤波器后,可以通过以下步骤删除信号开头的大尖峰:
import numpy as np
from scipy.signal import butter, filtfilt
import matplotlib.pyplot as plt
order = 4 # 滤波器阶数
fs = 1000 # 采样频率
lowcut = 10 # 低频截止频率
highcut = 100 # 高频截止频率
def butter_bandpass_filter(data, lowcut, highcut, fs, order):
nyquist = 0.5 * fs
low = lowcut / nyquist
high = highcut / nyquist
b, a = butter(order, [low, high], btype='band')
y = filtfilt(b, a, data)
return y
t = np.linspace(0, 1, fs, endpoint=False)
data = np.sin(2 * np.pi * 30 * t) + np.sin(2 * np.pi * 60 * t) + np.random.randn(fs) * 0.1
filtered_data = butter_bandpass_filter(data, lowcut, highcut, fs, order)
plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
plt.plot(t, data, 'b-', label='Original Signal')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.legend()
plt.subplot(2, 1, 2)
plt.plot(t, filtered_data, 'g-', linewidth=2, label='Filtered Signal')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.legend()
plt.tight_layout()
plt.show()
通过以上步骤,我们可以应用巴特沃斯带通滤波器对信号进行滤波,并绘制出滤波后的信号图形。如果信号开头存在大尖峰,滤波后的信号图形将不再包含该尖峰。
领取专属 10元无门槛券
手把手带您无忧上云