clear all
close all
x1 = load('ecg3.dat');
fs = 200;
N = length (x1);
t = [0:N-1]/fs;
figure(1)
subplot(2,1,1)
plot(t,x1)
xlabel('second');ylabel('Volts');title('Input ECG Signal')
subplot(2,1,2)
plot(t(200:600),x1(200:600))
xlabel('second');ylabel('Volts');title('Input ECG Signal 1-3 second')
xlim([1 3])
Cancellation DC drift and normalization
x1 = x1 - mean (x1 );
x1 = x1/ max( abs(x1 ));
figure(2)
subplot(2,1,1)
plot(t,x1)
xlabel('second');ylabel('Volts');title(' ECG Signal after cancellation DC drift and normalization')
subplot(2,1,2)
plot(t(200:600),x1(200:600))
xlabel('second');ylabel('Volts');title(' ECG Signal 1-3 second')
xlim([1 3])
Low Pass Filtering
b=[1 0 0 0 0 0 -2 0 0 0 0 0 1];
a=[1 -2 1];
h_LP=filter(b,a,[1 zeros(1,12)]);
x2 = conv (x1 ,h_LP);
x2 = x2/ max( abs(x2 ));
figure(3)
subplot(2,1,1)
plot([0:length(x2)-1]/fs,x2)
xlabel('second');ylabel('Volts');title(' ECG Signal after LPF')
xlim([0 max(t)])
subplot(2,1,2)
plot(t(200:600),x2(200:600))
xlabel('second');ylabel('Volts');title(' ECG Signal 1-3 second')
xlim([1 3])
High Pass Filtering
b = [-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 -32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1];
a = [1 -1];
h_HP=filter(b,a,[1 zeros(1,32)]);
x3 = conv (x2 ,h_HP);
x3 = x3/ max( abs(x3 ));
figure(4)
subplot(2,1,1)
plot([0:length(x3)-1]/fs,x3)
xlabel('second');ylabel('Volts');title(' ECG Signal after HPF')
xlim([0 max(t)])
subplot(2,1,2)
plot(t(200:600),x3(200:600))
xlabel('second');ylabel('Volts');title(' ECG Signal 1-3 second')
xlim([1 3])
Derivative Filter
h = [-1 -2 0 2 1]/8;
x4 = conv (x3 ,h);
x4 = x4 (2+[1: N]);
x4 = x4/ max( abs(x4 ));
figure(5)
subplot(2,1,1)
plot([0:length(x4)-1]/fs,x4)
xlabel('second');ylabel('Volts');title(' ECG Signal after Derivative')
subplot(2,1,2)
plot(t(200:600),x4(200:600))
xlabel('second');ylabel('Volts');title(' ECG Signal 1-3 second')
xlim([1 3])
Squaring
x5 = x4 .^2;
x5 = x5/ max( abs(x5 ));
figure(6)
subplot(2,1,1)
plot([0:length(x5)-1]/fs,x5)
xlabel('second');ylabel('Volts');title(' ECG Signal Squarting')
subplot(2,1,2)
plot(t(200:600),x5(200:600))
xlabel('second');ylabel('Volts');title(' ECG Signal 1-3 second')
xlim([1 3])
Moving Window Integration
h = ones (1 ,31)/31;
Delay = 15;
x6 = conv (x5 ,h);
x6 = x6 (15+[1: N]);
x6 = x6/ max( abs(x6 ));
figure(7)
subplot(2,1,1)
plot([0:length(x6)-1]/fs,x6)
xlabel('second');ylabel('Volts');title(' ECG Signal after Averaging')
subplot(2,1,2)
plot(t(200:600),x6(200:600))
xlabel('second');ylabel('Volts');title(' ECG Signal 1-3 second')
xlim([1 3])
Find QRS Points Which it is different than Pan-Tompkins algorithm
figure(7)
subplot(2,1,1)
max_h = max(x6);
thresh = mean (x6 );
poss_reg =(x6>thresh*max_h)';
figure (8)
subplot(2,1,1)
hold on
plot (t(200:600),x1(200:600)/max(x1))
box on
xlabel('second');ylabel('Integrated')
xlim([1 3])
subplot(2,1,2)
plot (t(200:600),x6(200:600)/max(x6))
xlabel('second');ylabel('Integrated')
xlim([1 3])
left = find(diff([0 poss_reg])==1);
right = find(diff([poss_reg 0])==-1);
left=left-(6+16);
right=right-(6+16);
for i=1:length(left)
[R_value(i) R_loc(i)] = max( x1(left(i):right(i)) );
R_loc(i) = R_loc(i)-1+left(i);
[Q_value(i) Q_loc(i)] = min( x1(left(i):R_loc(i)) );
Q_loc(i) = Q_loc(i)-1+left(i);
[S_value(i) S_loc(i)] = min( x1(left(i):right(i)) );
S_loc(i) = S_loc(i)-1+left(i);
end
Q_loc=Q_loc(find(Q_loc~=0));
R_loc=R_loc(find(R_loc~=0));
S_loc=S_loc(find(S_loc~=0));
figure
subplot(2,1,1)
title('ECG Signal with R points');
plot (t,x1/max(x1) , t(R_loc) ,R_value , 'r^', t(S_loc) ,S_value, '*',t(Q_loc) , Q_value, 'o');
legend('ECG','R','S','Q');
subplot(2,1,2)
plot (t,x1/max(x1) , t(R_loc) ,R_value , 'r^', t(S_loc) ,S_value, '*',t(Q_loc) , Q_value, 'o');
xlim([1 3])
With ref to the command
ReplyDeletex1 = load('100.dat'); % load the ECG signal from the file
How to load the file..I have saved 100.dat file in the same path as this code but it is not working.
This code works with dat file that has three coloumn text data, not MIT-BIH binary data.
ReplyDeleteFrom where can i get that data??? can you please provide me the link.. my email is mhanzalakhan@gmail.com..I have a project due in a week's time and i have not reached at any substantial result.I am working on analysing an ECG signal using wavelet transform and need to detect the p wave QRS complex and t wave and for any abnormality identify the corresponding heart disorder.
Deletehttp://www.enel.ucalgary.ca/People/Ranga/enel563/SIGNAL_DATA_FILES/ECG3.dat
DeleteThis is ecg3.dat. Good luck.
This comment has been removed by the author.
DeleteHow can I load MIT-BIH binary data? or .mat files?
ReplyDeletesir tell the proper procedure that how to import ECG .DAT files downloaded from www.physionet.org into the this matlab code..
ReplyDeleteCan I load it with file.txt or .mat file?
ReplyDeletebest shiat ever, works perfectly motherfucka
ReplyDeleteS and Q have the same location, S is supposed to come after R. What would be the correct interval for the S min-Search?
ReplyDeleteHow to download ECg4.dat or ECG 3.dat ??? If I click on this I am getting the values in a new window which is getting saved only as a text file!!! Can anyone help me ???
ReplyDeleteCan u explain Decision rule for QRS detection in your code???pls urgent
ReplyDeletehow to generate a synthetic ecg?does any1 knows the code?
ReplyDeleteHow did you consider that particular transfer function for LPF? Is there any specific reason or is it a randomly choosen one ? Could you please specify!!!
ReplyDelete