How to calculate area under each peak in plot from sampled data
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Lukas Poviser
le 3 Avr 2021
Commenté : Star Strider
le 4 Avr 2021
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.
0 commentaires
Réponse acceptée
Star Strider
le 3 Avr 2021
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 commentaires
Plus de réponses (1)
darova
le 3 Avr 2021
Modifié(e) : darova
le 3 Avr 2021
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 commentaires
darova
le 4 Avr 2021
xc and x are row and column. Try this
x1 = [xc(:); x(:)]; % merge together
y1 = [yc(:); y(:)];
Voir également
Catégories
En savoir plus sur Simulation, Tuning, and Visualization dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!