✅作者简介:热爱科研的Matlab仿真开发者,擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。
🍎 往期回顾关注个人主页:Matlab科研工作室
👇 关注我领取海量matlab电子书和数学建模资料
🍊个人信条:格物致知,完整Matlab代码获取及仿真咨询内容私信。
🔥 内容介绍
摘要——在第三项任务中,我们开发了一种基于支持向量机(SVM)的心电图(ECG)节律分类流程,利用MIT- BIH 心律失常数据库区分正常与异常心搏。通过潘-汤普金斯算法处理五份记录的ECG信号以检测QRS波群,随后提取形态学、时间及频谱特征。评估了两种特征配置:基线配置(8个特征)和扩展配置(12个特征)。采用 RBF 核训练的 SVM 模型在扩展特征集中实现了更高的分类准确率,同时提升了敏感性和特异性。通过混淆矩阵和基于主成分分析(PCA)的特征分离等可视化手段,验证了该方法在真实ECG监测场景中检测心律失常的有效性。
Image
Image
Image
⛳️ 运行结果
Image
Image
Image
📣 部分代码
= 1).';
% right_ind = find(diff([poss_reg.' 0]) == -1).';
%
% % R, Q, S detection
% R_loc = zeros(numel(left_ind), 1);
% Q_loc = zeros(numel(left_ind), 1);
% S_loc = zeros(numel(left_ind), 1);
% for i = 1:numel(left_ind)
% [~, R_loc(i)] = max(ecg_m(left_ind(i):right_ind(i)));
% R_loc(i) = R_loc(i)-1+left_ind(i);
% [~, Q_loc(i)] = min(ecg_m(left_ind(i):R_loc(i)));
% Q_loc(i) = Q_loc(i)-1+left_ind(i);
% [~, S_loc(i)] = min(ecg_m(R_loc(i):right_ind(i)));
% S_loc(i) = S_loc(i)-1+R_loc(i);
% end
%
% % Clean incomplete edges
% if R_loc(1)*ts < 0.2
% Q_loc(1) = []; R_loc(1) = []; S_loc(1) = [];
% left_ind(1) = []; right_ind(1) = [];
% end
% if (numel(ecg_m)-R_loc(end))*ts < 0.2
% Q_loc(end) = []; R_loc(end) = []; S_loc(end) = [];
% left_ind(end) = []; right_ind(end) = [];
% end
% n_beats_det = numel(R_loc);
%
% % P and T detection
% P_loc = zeros(n_beats_det, 1);
% T_loc = zeros(n_beats_det, 1);
% for i = 1:n_beats_det
% pL = max(1, left_ind(i) - round(0.2 * fs));
% pR = left_ind(i);
% [~, p] = max(ecg_m(pL:pR));
% P_loc(i) = pL + p - 1;
%
% tL = right_ind(i);
% tR = min(numel(ecg_m), tL + round(0.4 * fs));
% [~, t] = max(ecg_m(tL:tR));
% T_loc(i) = tL + t - 1;
% end
%
% % Feature Extraction
% RS_width = ts * (S_loc - R_loc);
% QS_width = ts * (S_loc - Q_loc);
% QR_width = ts * (R_loc - Q_loc);
% pre_RR_int = [0; ts * diff(R_loc)];
% post_RR_int = [pre_RR_int(2:end); 0];
%
% MPSD = zeros(n_beats_det, 1);
% area_QR = zeros(n_beats_det, 1);
% area_RS = zeros(n_beats_det, 1);
% for i = 1:n_beats_det
% MPSD(i) = mean(abs(fft(ecg_m(P_loc(i):T_loc(i)))).^2);
% area_QR(i) = trapz(ecg_m(Q_loc(i):R_loc(i)));
% area_RS(i) = trapz(ecg_m(R_loc(i):S_loc(i)));
% end
%
% features = [QS_width, pre_RR_int, post_RR_int, QR_width, RS_width, MPSD, area_QR, area_RS];
% end
function [features, R_loc, Q_loc, S_loc, P_loc, T_loc, n_beats_det, left_ind, right_ind] = extract_ecg_features(ecg_m, ecg_MW, fs, template_normal)
ts = 1/fs;
% === Thresholding and region detection ===
poss_reg = (ecg_MW > mean(ecg_MW))';
left_ind = find(diff([0 poss_reg.']) == 1).';
right_ind = find(diff([poss_reg.' 0]) == -1).';
% === R, Q, S detection ===
R_loc = zeros(numel(left_ind), 1);
Q_loc = zeros(numel(left_ind), 1);
S_loc = zeros(numel(left_ind), 1);
for i = 1:numel(left_ind)
[~, R_loc(i)] = max(ecg_m(left_ind(i):right_ind(i)));
R_loc(i) = R_loc(i) - 1 + left_ind(i);
[~, Q_loc(i)] = min(ecg_m(left_ind(i):R_loc(i)));
Q_loc(i) = Q_loc(i) - 1 + left_ind(i);
[~, S_loc(i)] = min(ecg_m(R_loc(i):right_ind(i)));
S_loc(i) = S_loc(i) - 1 + R_loc(i);
end
% === Clean boundaries ===
if R_loc(1)*ts < 0.2
Q_loc(1) = []; R_loc(1) = []; S_loc(1) = [];
left_ind(1) = []; right_ind(1) = [];
end
if (numel(ecg_m)-R_loc(end))*ts < 0.2
Q_loc(end) = []; R_loc(end) = []; S_loc(end) = [];
left_ind(end) = []; right_ind(end) = [];
end
n_beats_det = numel(R_loc);
% === P and T detection ===
P_loc = zeros(n_beats_det, 1);
T_loc = zeros(n_beats_det, 1);
for i = 1:n_beats_det
pL = max(1, left_ind(i) - round(0.2 * fs));
pR = left_ind(i);
[~, p] = max(ecg_m(pL:pR));
P_loc(i) = pL + p - 1;
tL = right_ind(i);
tR = min(numel(ecg_m), tL + round(0.4 * fs));
[~, t] = max(ecg_m(tL:tR));
T_loc(i) = tL + t - 1;
end
% === Initialize feature arrays ===
QS_width = zeros(n_beats_det,1);
QR_width = zeros(n_beats_det,1);
RS_width = zeros(n_beats_det,1);
pre_RR_int = [0; ts * diff(R_loc)];
post_RR_int = [pre_RR_int(2:end); 0];
MPSD = zeros(n_beats_det,1);
area_QR = zeros(n_beats_det,1);
area_RS = zeros(n_beats_det,1);
auto_corr_val = zeros(n_beats_det,1);
ST_dev = zeros(n_beats_det,1);
ST_slope = zeros(n_beats_det,1);
template_corr = zeros(n_beats_det,1);
for i = 1:n_beats_det
% === Duration Features ===
QS_width(i) = ts * (S_loc(i) - Q_loc(i));
QR_width(i) = ts * (R_loc(i) - Q_loc(i));
RS_width(i) = ts * (S_loc(i) - R_loc(i));
% === MPSD ===
if P_loc(i) < T_loc(i)
window = ecg_m(P_loc(i):T_loc(i));
MPSD(i) = mean(abs(fft(window)).^2);
else
MPSD(i) = 0;
end
% === Area under QR, RS ===
if Q_loc(i) < R_loc(i)
area_QR(i) = trapz(ecg_m(Q_loc(i):R_loc(i)));
else
area_QR(i) = 0;
end
if R_loc(i) < S_loc(i)
area_RS(i) = trapz(ecg_m(R_loc(i):S_loc(i)));
else
area_RS(i) = 0;
end
% === Autocorrelation ===
if Q_loc(i) < S_loc(i)
segment = ecg_m(Q_loc(i):S_loc(i));
if length(segment) >= 2
acf = xcorr(segment, 'coeff');
acf(acf == 1 | isnan(acf)) = []; % Remove trivial peak
auto_corr_val(i) = max(acf(:), [], 'omitnan');
else
auto_corr_val(i) = 0;
end
else
auto_corr_val(i) = 0;
end
% === ST deviation and slope ===
st_start = S_loc(i) + round(0.02 * fs);
st_end = min(T_loc(i), numel(ecg_m));
if st_end > st_start
y = ecg_m(st_start:st_end);
x = (st_start:st_end) * ts;
ST_dev(i) = mean(y);
p = polyfit(x, y, 1);
ST_slope(i) = p(1);
else
ST_dev(i) = 0;
ST_slope(i) = 0;
end
% === Correlation with normal template ===
if Q_loc(i) < S_loc(i)
beat_seg = ecg_m(Q_loc(i):S_loc(i));
if length(beat_seg) >= 2 && length(template_normal) >= 2
beat_seg = interp1(1:length(beat_seg), beat_seg, ...
linspace(1,length(beat_seg), length(template_normal)), 'linear', 'extrap');
c = corrcoef(beat_seg, template_normal);
if size(c,1)==2 && size(c,2)==2
template_corr(i) = c(1,2);
else
template_corr(i) = 0;
end
else
template_corr(i) = 0;
end
else
template_corr(i) = 0;
end
end
% === Final Feature Matrix (12 features) ===
features = [QS_width, pre_RR_int, post_RR_int, ...
QR_width, RS_width, MPSD, ...
area_QR, area_RS, auto_corr_val, ...
ST_dev, ST_slope, template_corr];
end
🔗 参考文献
图片
🏆团队擅长辅导定制多种科研领域MATLAB仿真,助力科研梦: