读取该心电数据.csv文件,进行低通滤波处理,然后绘图显示python
时间: 2024-10-29 22:20:38 浏览: 17
python读取csv文件.txt
在Python中,读取CSV文件并进行低通滤波处理通常需要结合pandas库用于数据处理,numpy库用于数值计算,以及matplotlib或seaborn库用于绘图。以下是简单的步骤:
1. **导入所需的库**:
```python
import pandas as pd
import numpy as np
from scipy.signal import butter, lfilter, freqz
import matplotlib.pyplot as plt
```
2. **加载数据**:
```python
data = pd.read_csv('心电数据.csv') # 假设文件名为心电数据.csv
df = data.dropna() # 删除缺失值行
electrode_data = df.values # 将数据框转换为数组
```
3. **预处理(假设心率信号在列0)**:
```python
fs = 1000 # 假设采样频率为1000 Hz
ecg_signal = electrode_data[:, 0] # 提取心电信号列
lowcut = 5 # 设定低通滤波截止频率
highcut = 50 # 高频截止频率
order = 4 # 滤波器阶数
# 定义滤波函数
def butter_lowpass(cutoff, fs, order=4):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
# 进行动态滤波
b, a = butter_lowpass(lowcut, fs)
filtered_ecg = lfilter(b, a, ecg_signal)
```
4. **绘制原始信号和滤波后的信号**:
```python
plt.figure(figsize=(10,6))
plt.subplot(2,1,1)
plt.plot(ecg_signal, label='Original ECG')
plt.legend()
plt.title('Original Signal')
plt.subplot(2,1,2)
plt.plot(filtered_ecg, label='Filtered ECG (Low Pass)')
plt.xlabel('Sample')
plt.legend()
plt.title(f'Filtered Signal (Low Cut: {lowcut} Hz)')
plt.tight_layout()
plt.show()
```
阅读全文