Help with loop script
조회 수: 4 (최근 30일)
이전 댓글 표시
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
댓글 수: 2
답변 (1개)
Ridwan Alam
2019년 12월 5일
편집: Ridwan Alam
2019년 12월 5일
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
Ridwan Alam
2019년 12월 5일
편집: Ridwan Alam
2020년 1월 8일
Hi Sam,
II 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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Digital Filter Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!