python - 频域 fft 后的带通滤波器

标签 python python-3.x

我绘制了 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 秒输出示例:
enter image description here

最佳答案

代替

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/

相关文章:

python - 耗尽的迭代器 - 如何处理它们?

python - 实用Python项目的Setup.py

python - 在字典中访问字典

python - 使用 biopython 从外部 pubmed ID 列表中提取多个摘要

python - 在没有标准库的情况下嵌入 Python3

python - 使用 python 正则表达式查找特定字符

python - 在 abaqus python 中使用 Element() 构造函数创建零件元素

python - 使用函数接受不是标识符的 kwargs 关键字参数是否安全?

python - 如何在 Python3 中使用构造函数模拟对象?

python - 如何在 yield 中扩展元组?