Azzera filtri
Azzera filtri

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

3 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Star Strider
Star Strider il 3 Apr 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 Commenti

Accedi per commentare.

Più risposte (1)

darova
darova il 3 Apr 2021
Modificato: darova il 3 Apr 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 Commenti
Lukas Poviser
Lukas Poviser il 3 Apr 2021
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 il 4 Apr 2021
xc and x are row and column. Try this
x1 = [xc(:); x(:)]; % merge together
y1 = [yc(:); y(:)];

Accedi per commentare.

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by