How to calculate area under each peak in plot from sampled data

조회 수: 2 (최근 30일)
Lukas Poviser
Lukas Poviser 2021년 4월 3일
댓글: Star Strider 2021년 4월 4일
Hi, I would like to know area of each peak from my sampled data [time,flow] plot as shown in picture. Area of each peak should be written to matrice [time_of_peak, area], so I would be able to plot graph.

채택된 답변

Star Strider
Star Strider 2021년 4월 3일
Try this:
D = load('flow_inspi3.txt');
t = D(:,1);
s = D(:,2);
brk = find(ischange(s,'linear','Threshold',1E-8),1); % First Peak Start
brk1 = [brk; find(islocalmin(s, 'FlatSelection','first'))]; % Peak Start
brk2 = [find(islocalmin(s, 'FlatSelection','last')); numel(s)]; % Peak End
for k = 1:size(brk,1)
idxrng = brk1(k):brk2(k);
AUC(k) = trapz(t(idxrng), s(idxrng));
end
locs = find(islocalmax(s));
pks = s(locs);
figure
plot(t,s)
grid
text(t(locs), pks/2, compose('Area = %9.3f',AUC), 'Horiz','center', 'Vert','middle', 'Rotation',90)
xlim([1.6E+5 1.8E+5]) % Display First Four Peaks (Delete Later)
The file only has information for positive deflections, not negative as in the original plot image. Data with positive and negative deflections would require a slightly different approach.
A mnore robust version:
D = load('flow_inspi3.txt');
t = D(:,1);
s = D(:,2);
zrx = find(diff(sign(s)));
for k = 1:numel(zrx)
idxrng = max(1,zrx(k)-2):min(zrx(k)+2,numel(s));
B = [t(idxrng) ones(size(idxrng(:)))] \ s(idxrng);
intcpt(k) = -B(2)/B(1);
end
mindifft = min(diff(t));
for k = 1:numel(intcpt)-1
if (intcpt(k+1)-intcpt(k)) > mindifft
idxrng = t>=intcpt(k) & t<=intcpt(k+1);
AUC(k) = trapz(t(idxrng), s(idxrng));
[pks(k),locs1] = max(s(idxrng));
locs(k) = locs1*mindifft+round(intcpt(k));
end
end
posidx = AUC > 0;
figure
plot(t,s)
grid
text(locs(posidx), pks(posidx)/2, compose('Area = %9.3f',AUC(posidx)), 'Horiz','center', 'Vert','middle', 'Rotation',90)
xlim([1.6E+5 1.8E+5])
.
  댓글 수: 6
Lukas Poviser
Lukas Poviser 2021년 4월 4일
It works perfectly! Thank you!
Star Strider
Star Strider 2021년 4월 4일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

darova
darova 2021년 4월 3일
편집: darova 2021년 4월 3일
Here is ax example
x = 0:20;
y = sin(x);
[xc,yc] = polyxpoly(x,y,[0 20],[0 0]); % find '0' points intersections
x1 = [xc' x]; % merge together
y1 = [yc' y];
[xx,ix] = sort(x1); % sort x coordinate
yy = y1(ix); % order y coordinate
ix1 = find(abs(yy)<0.01); % zero point indices
plot(xx,yy,'marker','.')
for i = 1:length(ix1)-1
ii = ix1(i):ix1(i+1); % indices of area
s = trapz(xx(ii),yy(ii)); % calculate area
text(xx(ix1(i)),yy(ix1(i)),num2str(s))
end
  댓글 수: 4
Lukas Poviser
Lukas Poviser 2021년 4월 3일
Matlab respond is:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in Volume_mathworks (line 33)
x1 = [xc' x]; % merge together
Maybe I am not sure how to edit your code to fit to my data. Variable x is for time of sample and y is for value of sample? Like that?
x = sample_time;
y = sample_value;
Do I need to change something else in your code?
darova
darova 2021년 4월 4일
xc and x are row and column. Try this
x1 = [xc(:); x(:)]; % merge together
y1 = [yc(:); y(:)];

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Scopes and Data Logging에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by