Positioning a cutted signal from a ECK Signal
4 views (last 30 days)
Show older comments
Samuel the Helpless
on 30 Apr 2020
Commented: Star Strider
on 3 May 2020
Me and my Group need to filter a ECK Signal with Findpeaks, We need to then level the Signal so that the highest point(R Spike) are all on the same Position. Also we need to start and end the Signal at 0 so that we can compare the cutted Signals and draw a conclusion. We started and got some help from this link
now that we cutted the signal we wanted to allign the Signals bringing the R spike of every signal to the same spot.
Also we still need to make the signals start and end at 0 but that is not as important as alligning the signals.
This is the code we have till now (look code below)
We included the Text file with the signal and also our code
this is what we get atm. and we dont really now how to contiun and finish the task
as you can see the R spikes are not alligned at all. and we want to align them all so all the r spikes are at the same spot
Thanks for the Help.
clear all;
D = readmatrix("3_1_ecg60s.txt");
tv = D(:,1);
[pks, locs] = findpeaks(tv, 'MinPeakHeight',-0.95, 'MinPeakDistance',600);
locs1 = locs + 480;
for k1 = 1:numel(locs1)-1 % Define MUAP Frames
tvc{k1} = tv(locs1(k1)-1:locs1(k1+1)-1);
end
plot(tvc{k1})
%findpeaks(tvc{k1},2,'Annotate','extents','WidthReference','halfheight')
figure(2)
hold all
for k1 = 1:numel(tvc)
plot(tvc{k1}-tvc{k1}(1))
%axis([750 1500 -3.5 1]); % Plot MUAP Frames
end
hold off
grid
2 Comments
Ameer Hamza
on 30 Apr 2020
Can you explain, what do you mean by start and end at 0?
Also, when aligning peaks, what should be the location of all peaks. For example, if will align all peaks at x=400, then some of the signals will need to be truncated or moved to the negative x-axis.
Accepted Answer
Star Strider
on 30 Apr 2020
You quoted part of my previous code (not entirely correctly) , so I’m ‘in for 1d, in for 1£ ', i guess.
Try this:
filename = '3_1_ecg60s.txt';
opts = detectImportOptions(filename);
D = readmatrix(filename,opts);
EKG = D(2:end,1);
Ts = 1E-3; % Sampling Interval (seconds)
tv = linspace(0, 1, numel(EKG))*Ts*numel(EKG); % Time Vector
Start = 250; % Ensemble Start: 250 ms Before R-Wave
figure
plot(tv, EKG)
grid
xlabel('Time (ms)')
ylkabel('Amplitude (mV)')
title('Original Signal')
[pks, locs, w, p] = findpeaks(EKG, 'MinPeakProminence',1.5);
for k1 = 1:numel(locs)-1 % Define EKG Frames
EKGc{k1} = EKG(locs(k1)-Start:locs(k1+1)-Start);
tvc{k1} = tv(locs(k1)-Start:locs(k1+1)-Start);
end
figure
hold on
for k1 = 1:numel(EKGc)
plot(tvc{k1}, EKGc{k1})
end
hold off
grid
xlabel('Time (ms)')
ylkabel('Amplitude (mV)')
title('Segmented EKG')
figure
hold on
for k1 = 1:numel(EKGc)
plot(tvc{k1}-tvc{k1}(1), EKGc{k1})
end
hold off
grid
xlabel('Time (ms)')
ylkabel('Amplitude (mV)')
title('Ensemble EKG')
It obviously still needs a good bit of filtering and other enhancements. I leave all that to you.
This should get you started.
28 Comments
Star Strider
on 3 May 2020
This is simply for your information and reference. It would not be ethical to copy any of it and submit it as your own work.
My code for this problem (some of which I already posted):
filename = '3_1_ecg60s.txt';
opts = detectImportOptions(filename);
D = readmatrix(filename,opts);
EKG = D(2:end,1);
Ts = 1E-3; % Sampling Interval (seconds)
tv = linspace(0, 1, numel(EKG))*Ts*numel(EKG); % Time Vector
Start = 250; % Ensemble Start: 250 ms Before R-Wave
figure
plot(tv, EKG)
grid
xlabel('Time (ms)')
ylabel('Amplitude (mV)')
title('Original Signal')
Fs = 1/Ts; % Sampling Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency (Hz)
L = numel(EKG); % Signal Lengthj
FT_EKG = fft(EKG - mean(EKG))/L; % Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
plot(Fv, abs(FT_EKG(Iv))*2)
grid
xlabel('Frequency (Hz)')
ylabel('Amplitude')
axis([0 100 0 0.05])
Wp = [ 5 40]/Fn; % Normalised Passband (Passband = 5 Hz To 40 Hz)
Ws = [ 2 45]/Fn; % Normalised Stopband (Passband = 2 Hz To 45 Hz)
Rp = 1; % Passband Ripple/Attenuation
Rs = 50; % Stopband Ripple/Attenuation
[n,Wp] = ellipord(Wp, Ws, Rp, Rs); % Calculate Elliptic Filter Optimum Order
[z,p,k] = ellip(n, Rp, Rs, Wp,'bandpass'); % Elliptic Filter
[sos,g] = zp2sos(z,p,k); % Second-Order-Section For Stability
EKG = filtfilt(sos, g, EKG); % Filtered EKG
figure
plot(tv, EKG)
grid
xlabel('Time (ms)')
ylabel('Amplitude (mV)')
title('Filtered Signal')
[pks, locs, w, p] = findpeaks(EKG, 'MinPeakProminence',1.5);
for k1 = 1:numel(locs)-1 % Define EKG Frames
EKGc{k1} = EKG(locs(k1)-Start:locs(k1+1)-Start);
tvc{k1} = tv(locs(k1)-Start:locs(k1+1)-Start);
end
figure
hold on
for k1 = 1:numel(EKGc)
plot(tvc{k1}, EKGc{k1})
end
hold off
grid
xlabel('Time (ms)')
ylabel('Amplitude (mV)')
title('Segmented EKG')
figure
hold on
for k1 = 1:numel(EKGc)
plot(tvc{k1}-tvc{k1}(1), EKGc{k1}-EKGc{k1}(1))
end
hold off
grid
xlabel('Time (ms)')
ylabel('Amplitude (mV)')
title('Ensemble EKG')
minLen = min(cellfun(@(x)numel(x), tvc)); % Minimum EKG Segment Record Length
EKGce = cellfun(@(x)x(1:minLen), EKGc, 'Unif',0); % Trim All Segments To Shortest Length
EKGensavg = mean(cell2mat(EKGce),2); % Calculate Ensemble AVerage
figure
plot((0:minLen-1)*Ts, EKGensavg)
grid
xlabel('Time (ms)')
ylabel('Amplitude (mV)')
title('Ensemble Averaged EKG')
You can also plot ‘EKGce’ if you want them all to be the same length. I calculated those and added the ensemble average to see what it would look like.
.
More Answers (0)
See Also
Categories
Find more on Denoising and Compression 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!