How to find Q and S point in QRS complex of ECG signal?

56 views (last 30 days)
How to find Q and R points of ECG signal shown below?
Code:
load 16265m.mat;
plot(val(1,:))
  3 Comments
Explorer
Explorer on 9 Feb 2016
My attempt to code:
D=load('16265m.mat');
t=1:1280;
ECG_data=D.val(1,:);
% plot(t,ECG_data)
% grid on,
[~,locs_Rwave] = findpeaks(ECG_data,'MinPeakHeight',0.5,...
'MinPeakDistance',200);
ECG_inverted = -ECG_data;
[~,locs_Swave] = findpeaks(ECG_inverted,'MinPeakHeight',0.5,...
'MinPeakDistance',200);
figure
hold on
plot(t,ECG_data);
plot(locs_Rwave,ECG_data(locs_Rwave),'rv','MarkerFaceColor','r');
plot(locs_Swave,ECG_data(locs_Swave),'rs','MarkerFaceColor','b');
%axis([0 1850 -1.1 1.1]);
grid on;
legend('ECG Signal','R-waves','S-waves');
xlabel('Samples'); ylabel('Voltage(mV)')
title('R-wave and S-wave in Noisy ECG Signal')
smoothECG = sgolayfilt(ECG_data,7,21);
figure
plot(t,ECG_data,'b',t,smoothECG,'r'); grid on
axis tight;
xlabel('Samples'); ylabel('Voltage(mV)');
legend('Noisy ECG Signal','Filtered Signal')
title('Filtering Noisy ECG Signal')
[~,min_locs] = findpeaks(-smoothECG,'MinPeakDistance',40);
% Peaks between -0.2mV and -0.5mV
locs_Qwave = min_locs(smoothECG(min_locs)>-0.5 & smoothECG(min_locs)<-0.2);
figure
hold on
plot(t,smoothECG);
plot(locs_Qwave,smoothECG(locs_Qwave),'rs','MarkerFaceColor','g');
plot(locs_Rwave,smoothECG(locs_Rwave),'rv','MarkerFaceColor','r');
plot(locs_Swave,smoothECG(locs_Swave),'rs','MarkerFaceColor','b');
grid on
title('Thresholding Peaks in Signal')
xlabel('Samples'); ylabel('Voltage(mV)')
ax = axis;
%axis([0 1850 -1.1 1.1])
legend('Smooth ECG signal','Q-wave','R-wave','S-wave');
Still points are not detected correctly. Please help me in improving this code.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 9 Feb 2016
The first trace doesn’t have any S-waves, so don’t look for them. The EKG morphology is dependent on the lead placement and heart health. The second one is a very sick heart, with a left bundle-branch block pattern. It also has an RSR' pattern, so you have to be careful to not detect the first R-deflection.
My code:
q = load('Explorer 16265m');
EKG1 = q.val(1,:);
EKG2 = q.val(2,:);
Fs = 128;
t = [0:size(EKG1,2)-1]/Fs;
[R1,TR1] = findpeaks( EKG1, t, 'MinPeakHeight',200);
[Q1,TQ1] = findpeaks(-EKG1, t, 'MinPeakHeight',100); % NOTE: No ‘S’ Waves In EKG1
[R2,TR2] = findpeaks( EKG2, t, 'MinPeakHeight', 50);
[QS2,TQS2] = findpeaks(-EKG2, t, 'MinPeakHeight', 75);
figure(1)
subplot(2,1,1)
plot(t, EKG1)
hold on
plot(TR1, R1, '^r')
plot(TQ1, -Q1, 'vg')
hold off
grid
axis([0 2 ylim])
legend('EKG', 'R', 'Q')
subplot(2,1,2)
plot(t, EKG2)
hold on
plot(TR2, R2, '^r')
plot(TQS2(1:2:end), -QS2(1:2:end), 'vg')
plot(TQS2(2:2:end), -QS2(2:2:end), 'vb')
hold off
grid
axis([0 2 ylim])
legend('EKG', 'R', 'Q', 'S')
My plot:
  12 Comments
Explorer
Explorer on 18 Feb 2016
Edited: Explorer on 18 Feb 2016
Okay, thank you for detailed answer.
I was finding Q R and S points to find slope of QR wave and RS wave.
In trace 1, there are Q and R points. So slope of QR can be found using slope formula i.e. (y2-y1)/(x2-x1)
But the problem here I am encountering is number of Q points and R points are not equal. What should I do now?
Probably this is because false Q points are detected especially after 300 (x-axis).
axis([300 330 ylim])
Star Strider
Star Strider on 18 Feb 2016
My pleasure. Electrocardiography and the principles behind it are not trivial to learn.
I didn’t look at that signal in detail, but the number of Q-waves and R-waves should not differ by more than 1, if the EKG trace was cut off in the middle of a QRS complex. Otherwise, experiment with different values of 'MINPEAKDISTANCE' in your findpeaks call to be sure the peaks it returns aren’t too close to each other. Do that for both the Q-wave and R-wave findpeak calls. You can use the same value for 'MINPEAKDISTANCE' for both once you detetermine the best value for it.

Sign in to comment.

More Answers (2)

Shatha Jazar
Shatha Jazar on 13 Feb 2017
please can you predict the R wave in my signal ?

tursun wali
tursun wali on 20 Mar 2017
Dear @Star Strider, ----------------------------------- Answer by Star Strider on 9 Feb 2016 Accepted answer ----------------------------------- Your result is impressive and what I want. I want to find height of Q, R and S peaks ; so far find R and S not Q peak I have tested you code (I've changed one line:q = load('16265m.mat'); ) It shows this error message: ----------------------------------- Error using uddpvparse (line 122) Invalid Parameter/Value pairs.
Error in findpeaks>parse_inputs (line 84) hopts = uddpvparse('dspopts.findpeaks',varargin{:});
Error in findpeaks (line 59) [X,Ph,Pd,Th,Np,Str,infIdx] = parse_inputs(X,varargin{:});
Error in QRS_detect_tursun6 (line 6) [R1,TR1] = findpeaks( EKG1, t, 'MinPeakHeight',200); -----------------------------------
I would be very happy if you tell me how to fix it? Well actually My signal contains only one EKG (attachment: person6.mat): I want to find height of Q, R and S peaks ; so far find R and S not Q peak
  3 Comments
sania urooj
sania urooj on 8 Mar 2021
@Star Strider I have used your above code for detecting QRS peaks. It's working but my task is to detect peaks of multiple ECG signal that are already stored in one mat file. How can I do it as each signal may have different peaks ? Please help me.
Christopher McCausland
Christopher McCausland on 10 Mar 2021
@sania urooj I would suggest creating a new question. However to run any code on diffrent arrays a simple 'for' loop of the lenght/width (depending on the orentation of the ecg leads) of the data should surfice. Remeber to include an array within the loop to store loop data in and a second array outside the loop to write the loop data too once the loop completes.
Hope this helps!
For 1:width(ecgchanneldata)
% do this code
end

Sign in to comment.

Categories

Find more on Signal Generation and Preprocessing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!