我绘制了 Fourier spectrum
的频域 ( ECG
)信号。
有高0 Hz
峰值(基线漂移)和高 50 Hz
峰值(净功率)。所以我想用带通滤波 5 - 49 Hz
.
raw_data = 数据(y 轴)和 t = 时间(x 轴)
import matplotlib.pyplot as plt, numpy as np
from scipy.signal import butter, lfilter
## Raw data
raw_data = raw_data['data'][:300010, Channel - 1] # 1 (-1) is channel of ECG
fs = 1000 # Hz
tt_time = len(raw_data) / fs # total measure time (s)
t = np.arange(0, tt_time, 1 / fs) # Calculate time
plt.figure()
plt.subplot(3,1,1)
plt.plot(t, raw_data)
## fourier spectrum
frsp = np.fft.fft(raw_data) / len(raw_data) # fourier spectrum
frsp = frsp[range(int(len(raw_data) / 2))] # half of fourier for y axis
v = np.arange(int(tt_time * fs / 2)) # number of values
frqs = v / tt_time # frequencies for x axis
## Plot frequency domain spectrum
plt.subplot(3,1,2)
plt.plot(frqs, abs(frsp))
## Bandpass filter
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = .5 * fs
low = lowcut/nyq
high = highcut/nyq
b, a = butter(order, [low,high], btype='band')
return b, a
def butter_bandpass_filter(raw_data, lowcut, highcut, fs, order=5):
b,a = butter_bandpass(lowcut, highcut, fs, order=order)
y = lfilter(b,a,raw_data)
return y
lowcut = 5.0
highcut = 49.0
## Plot filtered signal
plt.subplot(3,1,3)
y = butter_bandpass_filter(t, lowcut, highcut, fs, order=5)
plt.plot(t, y)
尝试此代码后,它不会像需要过滤那样进行过滤。我知道我需要带通行证,但我不知道如何将其应用于我的代码和数据。谁能帮我?先感谢您 :)前 150 秒输出示例:
最佳答案
代替
y = butter_bandpass_filter(t, lowcut, highcut, fs, order=5)
和
y = butter_bandpass_filter(raw_data, lowcut, highcut, fs, order=5)
感谢 Norok2 (看评论)
关于python - 频域 fft 后的带通滤波器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59424853/