Having trouble to calculate the area under curve for each peaks and display the area under curve in graph?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi there.I am new to mathlab and having trouble to calculate the area under curve for each peaks and display the area under curve in graph? i have attached the file over here.Hope someone can help me. thanks in advance.
댓글 수: 2
Scott MacKenzie
2021년 8월 10일
It's not clear what you are trying to do; i.e, what precisely do you mean by "peak"? Running your data through findpeaks indicates 18,015 peaks. With some smoothing... Well, let me just illustrate my point as follows:
f = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/708072/time%20vs%20Muscle%20contraction.txt';
M = readmatrix(f);
x = M(:,1);
y = M(:,2);
tiledlayout('flow');
nexttile;
plot(x,y);
nexttile;
y2 = smoothdata(y);
plot(x,y2);
[pks, loc] = findpeaks(y);
[pks2, loc2] = findpeaks(y2);
[length(pks) length(pks2)]
채택된 답변
Star Strider
2021년 8월 10일
It depends entirely on what the definition of ‘peak’ is, and if the data first need to be detrended.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/708072/time%20vs%20Muscle%20contraction.txt', 'VariableNamingRule','preserve')
t = T1.TIME;
v = T1.('MC[v]');
Ts = mean(diff(t),'omitnan');
Fs = 1/Ts;
Fn = Fs/2;
figure
plot(t, v)
grid
xlabel('t')
ylabel('v')
title('Original Data')
vfilt = highpass(v, 1E-4, Fs, 'ImpulseResponse','iir');
zxi = find(diff(sign(vfilt)));
for k = 1:numel(zxi)
idxrng = max(1, zxi(k)-1) : min(numel(v),zxi(k)+1);
tzc(k) = interp1(vfilt(idxrng),t(idxrng),0);
end
tzc = unique(tzc);
for k = 1:numel(tzc)-1
Lv = (t>=tzc(k) & t<=tzc(k+1));
tv{k} = t(Lv);
vv{k} = vfilt(Lv);
if (numel(vv{k}) > 1)
Area(k) = trapz(tv{k},vv{k});
else
Area(k) = 0;
end
tvm(k) = mean(tv{k});
maxvv = max(vv{k});
if (~isempty(maxvv))
pkv(k) = maxvv;
else
pkv(k) = NaN;
end
end
figure
plot(t, vfilt)
hold on
plot(tzc, zeros(size(tzc)), 'xr')
hold off
grid
xlabel('t')
ylabel('v')
title('Original Data (Detrended)')
legend('Data', 'Zero-Crossings', 'Location','best')
% text(tvm,pkv, compose('\\leftarrow %.3f',Area), 'Horiz','center', 'Vert','bottom', 'Rotation',90)
.
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spectral Estimation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!