基于MATLAB的MIT-BIH ECG数据PQRST波定位实现

一、数据读取与预处理

1.1 MIT-BIH数据读取
% 读取头文件信息
header = read_header('100.hea');  % 自定义函数解析.hea文件
fs = header.Fs;                   % 采样率(360Hz)
gain = header.gain(1);            % 增益(200 counts/mV)
zerovalue = header.zerovalue(1);  % 零点偏移

% 读取二进制数据
fid = fopen('100.dat','r');
A = fread(fid, [3,inf], 'uint8');
fclose(fid);

% 解析212格式数据
M1H = bitshift(A(:,2), -4);
M2H = bitand(A(:,2), 15);
M(:,1) = bitshift(M1H,8) + A(:,1) - (M1H & 128)*256;
M(:,2) = bitshift(M2H,8) + A(:,3) - (M2H & 128)*256;

% 转换为电压值
ECG = (M - zerovalue) / gain;
1.2 数据预处理
% 去基线漂移
baseline = movmean(ECG(:,1), 500);
ECG_denoised = ECG(:,1) - baseline;

% 差分增强
diff_ecg = diff(ECG_denoised);
smooth_diff = movmean(abs(diff_ecg), 5);

二、差分算法实现R波检测

2.1 动态阈值计算
% 计算差分信号特征
diff_max = movmax(smooth_diff, 10);
diff_min = movmin(smooth_diff, 10);
thresh = 0.6*(diff_max - diff_min) + diff_min;

% R波定位
[r_peaks, r_locs] = findpeaks(smooth_diff, 'MinPeakHeight', thresh, ...
    'MinPeakDistance', round(0.2*fs));
2.2 形态学优化
% 二次微分增强
se = strel('disk',2);
diff_ecg_morph = imclose(imopen(diff_ecg, se), se);

% 阈值分割
binary = diff_ecg_morph > 0.4*max(diff_ecg_morph(:));

三、小波变换实现PQRST波定位

3.1 小波分解
% 选择小波基和分解层数
wname = 'db4';
level = 5;
[c,l] = wavedec(ECG_denoised, level, wname);

% 提取细节系数
d5 = detcoef(c,l,5);  % 高频细节(5-10Hz)
d4 = detcoef(c,l,4);  % 中频细节(1-5Hz)
3.2 多尺度特征提取
% P波检测(高频细节)
thresh_p = 0.5*max(d5);
p_peaks = findpeaks(d5, 'MinPeakHeight', thresh_p, ...
    'MinPeakDistance', round(0.05*fs));

% T波检测(中频细节)
thresh_t = 0.3*max(d4);
t_peaks = findpeaks(d4, 'MinPeakHeight', thresh_t, ...
    'MinPeakDistance', round(0.15*fs));

四、波形综合定位算法

4.1 波形关联逻辑
% 建立时间关联矩阵
dt = 0.05;  % 时间窗(50ms)
associations = zeros(length(r_locs),5);

for i = 1:length(r_locs)
    t = r_locs(i)/fs;
    
    % P波定位
    p_idx = find(t - dt <= p_peaks/fs & p_peaks/fs <= t + dt);
    associations(i,2) = p_peaks(p_idx);
    
    % T波定位
    t_idx = find(t - dt <= t_peaks/fs & t_peaks/fs <= t + dt);
    associations(i,4) = t_peaks(t_idx);
end
4.2 可视化验证
figure;
plot(ECG_denoised);
hold on;
plot(r_locs/fs, ECG_denoised(r_locs), 'ro', 'MarkerSize', 8);
plot(associations(:,2)/fs, ECG_denoised(associations(:,2)), 'go', 'MarkerSize', 6);
plot(associations(:,4)/fs, ECG_denoised(associations(:,4)), 'bo', 'MarkerSize', 6);
legend('ECG','R波','P波','T波');
xlabel('时间(s)');
ylabel('幅度(mV)');

参考代码 MATLAB读取MIT库的ECG数据程序 www.youwenfan.com/contentcnj/64125.html

五、优化

  1. 并行计算加速

    parfor i = 1:length(r_locs)
        % 并行处理每个R波关联
    end
    
  2. 自适应阈值调整

    thresh = median(diff_ecg)*1.5;  % 基于中位数的动态阈值
    
  3. 多导联联合分析

    % 同步分析MLII和V1导联
    [p_locs_II, p_locs_V1] = cross_validate_p_wave(r_locs);
    

六、完整代码框架

function [annotations] = locate_pqrst('100.dat')
    % 数据读取(见第一部分)
    
    % 差分检测R波
    [r_peaks, r_locs] = diff_detect(ECG_denoised);
    
    % 小波分解
    [c,l] = wavedec(ECG_denoised, 5, 'db4');
    
    % 多尺度特征提取
    [p_locs, t_locs] = wavelet_detect(c,l);
    
    % 波形关联
    annotations = associate_waveforms(r_locs, p_locs, t_locs);
    
    % 可视化
    plot_annotations(ECG_denoised, annotations);
end
posted @ 2025-10-28 10:56  晃悠人生  阅读(4)  评论(0)    收藏  举报