MATLAB Answers

0

Help with loop script

Asked by sam van Bohemen on 4 Dec 2019 at 23:23
Latest activity Commented on by Ridwan Alam on 5 Dec 2019 at 14:04
I have created a loop script to calculate peak amplitude for ECG data
x is the data file
TS is the total number of data points
L is the number of points sampled in each window
I have included an outlier code to remove peaks that are more than 3 std deviations from the mean.
I have written the code so I create a matrix that shows mean peak amplitude, peak std dev, number of peaks after outlier removal and number of peaks before outlier removal for each window.
In my code Bx represents the peaks detected for each window.
After I run my script Bx only shows the peaks detected for the last window
I want to alter my script so I can view Bx for all windows, Ideally in a matrix
Can somebody please help me?
My code is below:
Thanks!
xMatrix=reshape(x,L,TS/L);
%Filtering
[b,a]=butter(4,[0.0390625 0.46875],'bandpass'); %4th order bandpass butterworth 5-60Hz
xMatrix=filtfilt(b,a,xMatrix);
for N=(1:TS/L)
%Findpeaks
figure(1) % filtered ECG Plot x
plot(xMatrix(:,N));
[xpks,locs]=findpeaks(xMatrix(:,N),'MinPeakDistance',20,'MinPeakHeight',750);
Bx=rmoutliers(xpks,'mean');
[Bx,TFx]=rmoutliers(xpks);
xpeakmean=mean(Bx);
xpeakstd=std(Bx);
xspikes=numel(Bx);
xHR=numel(xpks);
findpeaks(xMatrix(:,N),'MinPeakDistance',20,'MinPeakHeight',750);
title('filtered ECG x')
xlabel('Samples')
ylabel('uV')
xlim([0 L])
ylim([-2000 2000])
%New Matrix
xpkMatrix(N,1)=xpeakmean;
xpkMatrix(N,2)=xpeakstd;
xpkMatrix(N,3)=xspikes;
xpkMatrix(N,4)=xHR;
end

  0 Comments

Sign in to comment.

Tags

1 Answer

回答者: Ridwan Alam
2019 年 12 月 5 日 0:54
編集済み: Ridwan Alam
2019 年 12 月 5 日 0:56

If you can share x, I could test it properly. Otherwise, I think you just need 'hold on':
xMatrix=reshape(x,L,TS/L);
%Filtering
[b,a]=butter(4,[0.0390625 0.46875],'bandpass'); %4th order bandpass butterworth 5-60Hz
xMatrix=filtfilt(b,a,xMatrix);
figure(1) % filtered ECG Plot x
for N=(1:TS/L)
%Findpeaks
plot(xMatrix(:,N));hold on;
[xpks,locs]=findpeaks(xMatrix(:,N),'MinPeakDistance',20,'MinPeakHeight',750);
Bx=rmoutliers(xpks,'mean');
[Bx,TFx]=rmoutliers(xpks);
xpeakmean=mean(Bx);
xpeakstd=std(Bx);
xspikes=numel(Bx);
xHR=numel(xpks);
findpeaks(xMatrix(:,N),'MinPeakDistance',20,'MinPeakHeight',750);hold on;
title('filtered ECG x')
xlabel('Samples')
ylabel('uV')
xlim([0 L])
ylim([-2000 2000])
%New Matrix
xpkMatrix(N,1)=xpeakmean;
xpkMatrix(N,2)=xpeakstd;
xpkMatrix(N,3)=xspikes;
xpkMatrix(N,4)=xHR;
end
or new windows:
xMatrix=reshape(x,L,TS/L);
%Filtering
[b,a]=butter(4,[0.0390625 0.46875],'bandpass'); %4th order bandpass butterworth 5-60Hz
xMatrix=filtfilt(b,a,xMatrix);
% filtered ECG Plot x
for N=(1:TS/L)
%Findpeaks
figure;plot(xMatrix(:,N));hold on;
[xpks,locs]=findpeaks(xMatrix(:,N),'MinPeakDistance',20,'MinPeakHeight',750);
Bx=rmoutliers(xpks,'mean');
[Bx,TFx]=rmoutliers(xpks);
xpeakmean=mean(Bx);
xpeakstd=std(Bx);
xspikes=numel(Bx);
xHR=numel(xpks);
findpeaks(xMatrix(:,N),'MinPeakDistance',20,'MinPeakHeight',750);
title('filtered ECG x')
xlabel('Samples')
ylabel('uV')
xlim([0 L])
ylim([-2000 2000])
%New Matrix
xpkMatrix(N,1)=xpeakmean;
xpkMatrix(N,2)=xpeakstd;
xpkMatrix(N,3)=xspikes;
xpkMatrix(N,4)=xHR;
end

  2 Comments

sam van Bohemen 2019 年 12 月 5 日 1:32
Hi Ridwan
Thanks for your help.
I have attached x as a zip file.
I tried altering my code as you suggested but I still can't view Bx for all windows.
Ridwan Alam
2019 年 12 月 5 日 14:04
Hi Sam,
I don't know how the Bx is calculated as you haven't shared rmoutliers(). But, the plotting is working as you requested, right? I tried to run the code without the Bx part, it did generate multiple plot windows showing both the signal and their peaks.
xnew = x(1:end-144);
L = 1000;
TS = length(xnew);
xMatrix=reshape(xnew,L,floor(TS/L));
%Filtering
[b,a]=butter(4,[0.0390625 0.46875],'bandpass'); %4th order bandpass butterworth 5-60Hz
xMatrix=filtfilt(b,a,xMatrix);
for i=1:TS/L
%Findpeaks
figure;plot(xMatrix(:,i));hold on;
findpeaks(xMatrix(:,i),'MinPeakDistance',20,'MinPeakHeight',750);
title('filtered ECG x')
xlabel('Samples')
ylabel('uV')
xlim([0 L])
ylim([-2000 2000])
% [xpks,locs]=findpeaks(xMatrix(:,i),'MinPeakDistance',20,'MinPeakHeight',750);
% Bx=rmoutliers(xpks,'mean');
% [Bx,TFx]=rmoutliers(xpks);
% xpeakmean=mean(Bx);
% xpeakstd=std(Bx);
% xspikes=numel(Bx);
% xHR=numel(xpks);
%New Matrix
% xpkMatrix(i,1)=xpeakmean;
% xpkMatrix(i,2)=xpeakstd;
% xpkMatrix(i,3)=xspikes;
% xpkMatrix(i,4)=xHR;
end
Please provide details about how far you got (make sure to show whole code) and what issues you are facing.

Sign in to comment.