求解:频域后带通滤波器

2024-05-15 17:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

!!解决了的!!见下面Norok2的评论

我绘制了Fourier spectrum信号的频域(ECG)。 存在高0 Hz峰值(基线漂移)和高50 Hz峰值(净功率)。所以我想用带通滤波器5 - 49 Hz原始数据=数据(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秒输出示例: enter image description here


Tags: datarawtimenporderpltfsspectrum
1条回答
网友
1楼 · 发布于 2024-05-15 17:23:36

替换

y = butter_bandpass_filter(t, lowcut, highcut, fs, order=5)

y = butter_bandpass_filter(raw_data, lowcut, highcut, fs, order=5)

感谢Norok2(参见评论)

相关问题 更多 >