MATLAB导入心电图代码.data,心电图信号计算并绘制心率 (HR)
时间: 2024-05-29 09:10:44 浏览: 125
%% 导入心电图数据
data = load('ecg.mat');
ecg = data.ecg;
fs = data.fs; % 采样频率
%% 预处理心电图信号
% 带通滤波
[b,a] = butter(2,[0.5 40]/(fs/2),'bandpass');
ecg_filtered = filtfilt(b,a,ecg);
% 去除基线漂移
ecg_baseline = smooth(ecg_filtered,fs*0.2);
ecg_clean = ecg_filtered - ecg_baseline;
% 心电图信号归一化
ecg_norm = (ecg_clean - mean(ecg_clean)) / std(ecg_clean);
%% 计算心率
[~,locs_Rwave] = findpeaks(ecg_norm,'MinPeakDistance',0.5*fs,'MinPeakHeight',0.6);
RR_interval = diff(locs_Rwave)/fs;
HR = 60./RR_interval;
%% 绘制心电图和心率曲线
t = (1:length(ecg)) / fs;
figure
subplot(2,1,1)
plot(t,ecg_norm)
hold on
scatter(locs_Rwave./fs,ecg_norm(locs_Rwave),'r')
xlabel('Time (s)')
ylabel('Normalized Amplitude')
title('ECG Signal')
subplot(2,1,2)
plot(locs_Rwave(2:end)./fs,HR)
xlabel('Time (s)')
ylabel('Heart Rate (bpm)')
title('Heart Rate')
阅读全文