You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to do segmentation on ECG signal for feature extraction
21 views (last 30 days)
Show older comments
I have a 1-minute ECG signal. How do I segment it into 10s? The Fs is 250Hz
thank you in advanced!
Accepted Answer
Star Strider
on 5 Aug 2020
Calculating ‘n’ is straightforward:
n = 10 * 250
producing:
n =
2500
So 10 seconds of a signal sampled at 250 Hz is 2500 samples.
26 Comments
Star Strider
on 6 Aug 2020
You can plot it any way you want to.
One possibility:
D = load('cu01m.mat');
EKG = D.val;
Fs = 250;
t = linspace(0, numel(EKG), numel(EKG))/Fs;
n = 10 * 250;
bufEKG = buffer(EKG,n);
ofstEKG = bsxfun(@plus, bufEKG, (0:5)*max(EKG));
figure
plot((0:size(bufEKG,1)-1)/Fs, ofstEKG)
grid
producing:
It asppears to be a normal EKG. I see no obvious pathology.
.
Mohamad Aswad Bachtear Effendy
on 7 Aug 2020
i want to do feature extraction so do i need to plot the frames individually? and how to calculate r-r intervals on the segmented signals?
Star Strider
on 7 Aug 2020
It is straightforward to plot the frames individually. It would likely be easiest to use subplot for this, however that is not the only option.
Use either findpeaks (with appropriate name-value pair arguments) or islocalmax (with appropriate name-value pair arguments) to detect the R-deflections, then do the necessary calculations to get the R-R intervals, and instantaneous rate. There may be other options as well, however those are the functions I generally use for these sorts of problems.
Mohamad Aswad Bachtear Effendy
on 7 Aug 2020
is this the correct way to subplot it? I think it's wrong
subplot(2,2,1)
ofstECG0 = bsxfun(@plus, bufECG, (0:0)*max(filtered_ECG));
plot((0:size(bufECG,1)-1)/Fs, ofstECG0)
Star Strider
on 7 Aug 2020
It is wrong!
Please think carefullly about what you want to do. Also, review the documentation for subplot (that I linked to).
Try this:
figure
bufSz = size(bufEKG,2);
for k = 1:bufSz
subplot(bufSz,1,k)
plot((0:size(bufECG,1)-1)/Fs, filtered_ECG(:,k))
grid
end
.
Mohamad Aswad Bachtear Effendy
on 8 Aug 2020
there is an error saying the vectors must be in same length
Mohamad Aswad Bachtear Effendy
on 8 Aug 2020
Edited: Mohamad Aswad Bachtear Effendy
on 8 Aug 2020
solved the error
the code is to detect R peaks. Is this correct?
[max,rr] = islocalmax(ofstECG, 'MinProminence',400);
v = find(max);
RR = rr(v);
Star Strider
on 8 Aug 2020
Not entirely.
Note that max is an important built-in function, and naming a variable ‘max’ overshadows it, so that you will not be able to use the function later in your code. Other than that (that you must correct), ‘v’ is correct. I wouild call it ‘Rpeak’ or something appropriately descriptive.
I have no idea what ‘RR’ does, since ‘rr’ returns the prominence values of the peaks. It has nothing to do with the R-R intervals. I would call it ‘Rprom’.
Other than with respect to max, the others are suggestions to make your code more descriptive and easier to understand later.
Mohamad Aswad Bachtear Effendy
on 8 Aug 2020
is the ofstECG in the islocalmax is correct? because it seems that the R peaks are not detected in the frames
Star Strider
on 8 Aug 2020
Note that ‘ofstEKG’ is simply designed to present all the records in one axes in the plot. You probably want to use the columns of ‘bufEKG’ instead, with each column processed individually. That would be easier. and would give the correct result for each segment.
Mohamad Aswad Bachtear Effendy
on 8 Aug 2020
The amplitude ranges are different in each frames, how to adjust it so the amplitude range are the same ?
Star Strider
on 8 Aug 2020
You are plotting ‘ofstEKG’. You should be plotting the columns of ‘bufEKG’. That will solve the problem.
Mohamad Aswad Bachtear Effendy
on 9 Aug 2020
Sorry but why the QRS points are not in the correct places?
Mohamad Aswad Bachtear Effendy
on 10 Aug 2020
[QSpeak,qs] = islocalmin(bufECG, 'MinProminence',0.1); % Detect: Q & S
w = find(QSpeak);
QSprom = qs(w);
[Rpeak,rr] = islocalmax(bufECG, 'MinProminence',1.5); % Detect: R
v = find(Rpeak);
Rprom = rr(v);
figure
bufSz = size(bufECG,2);
for k = 1:bufSz
subplot(bufSz,1,k)
plot((0:size(bufECG,1)-1)/Fs, bufECG(:,k))
hold on
plot(t(v), bufECG(v), 'r^')
plot(t(w), bufECG(w), 'gv')
hold off
grid
legend('ECG', 'R', 'Q S')
xlim([0 10])
title('QRS marking')
xlabel('Time(s)')
ylabel('Amplitude(mV)')
end
I'm a bit blank. Which part need to be adjusted in order to analyse the frames one by one?
Star Strider
on 10 Aug 2020
Use a loop. The islocalmin and islocalmax calls need to be in a loop so that ‘bufEKG’ is referred to by column:
for k = 1:size(bufEKG,2)
bufEKG(:,k)
end
I leave the details to you.
Star Strider
on 11 Aug 2020
The findpeaks function returns [pks,locs] if you only call it with the individual columns of ‘bufEKG’.
The plot call for them for each column should then be:
plot(t(locs), pks, '^r')
That is all there is to it.
Mohamad Aswad Bachtear Effendy
on 15 Aug 2020
Sir, how to find QRS duration? Do I need to replace islocalmin with findpeaks too?
[QSpeak,qs] = islocalmin(bufECG(:,k), 'MinProminence',0.05); % Detect: Q & S
w = find(QSpeak);
QSProm = qs(w);
[pks,locs_R] = findpeaks(bufECG(:,k),'MinPeakHeight',0.5,'MinPeakDistance',1); %R peaks
Star Strider
on 15 Aug 2020
The islocalmin function will provide the locations of the maximum negative extent of the Q and S deflections. I doubt that findpeaks will improve on islocalmin in that calculation, however it may be worthwhile to do that experiment.
However the QRS complex is defined as beginning with the departure from isoelectric reference immediately preceding the Q-deflection, and the return to isoelectric immediately following the S-deflection in a normal EKG, and the return to the level of the ST segment if there are ST-T abnormalities, as can occur with ischaemia or subendocardial myocardial infarction (ST-T depression), STEMI (ST-T elevation myocardial infarction), or drug or electrolyte abnormailites. So it is necessary to determine the point at which the Q-deflection begins and the S-deflection ends, in order to calculate those intervals correctly.
It is possible to use the maximmum negative deflection of the Q and S deflections to estimate the QRS duration, as determined by islocalmin, however that must be noted, since it is at variance with the standard definition of the QRS duration calculation, and will provide a shorter value for the QRS duration.
More Answers (0)
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)