Area under a peak

21 ビュー (過去 30 日間)
Haris Ashraf
Haris Ashraf 2022 年 4 月 11 日
コメント済み: Mathieu NOE 2022 年 4 月 11 日
Hi, I am struggling with calculating the area under the peak shown in the graph by red color. I want to calculate the area under the peak as well as area of the entire curve (including the peak).
The data file in.txt format has x and y axis data.

回答 (2 件)

Mathieu NOE
Mathieu NOE 2022 年 4 月 11 日
hello
based on @Davide Masiello excellent proposition, I tried to make the selection of the peak zone a bit more automatic...
the computation of the area of the peak may significantly vary based on where start and stop the selected segment , as how much of the root (being wider) will impact the result
this is how is works with the code below :
you can change the threshold (threshold_above_curve) to see the effect
clear,clc
A = readmatrix('875nm.txt'); % faster
x = A(:,1);
y = A(:,2);
[~,imax] = max(y);
% Total area
area_tot = trapz(x,y)
%area_tot = -4.5442e+03
% Area below peak
window = 200; % must be wider than expected peak
threshold_above_curve = 0.25; % above 0 and less than 0.5
[val,ind] = max(y);
x_tmp = x(ind-window/2:ind+window/2);
y_tmp = y(ind-window/2:ind+window/2);
y_tmp_d = detrend(y_tmp);
y_tmp_d = y_tmp_d - min(y_tmp_d);
ind = find(y_tmp_d>threshold_above_curve);
area_peak = trapz(x_tmp(ind),y_tmp(ind))
figure
plot(x,y,x_tmp(ind),y_tmp(ind),'r');
  4 件のコメント
Sam Chak
Sam Chak 2022 年 4 月 11 日
Another +1
Mathieu NOE
Mathieu NOE 2022 年 4 月 11 日
thanks again !
it's the beginning of the glory ! haha

サインインしてコメントする。


Davide Masiello
Davide Masiello 2022 年 4 月 11 日
編集済み: Davide Masiello 2022 年 4 月 11 日
clear,clc
A = readtable('875nm.txt');
x = A.Var1;
y = A.Var2;
[~,imax] = max(y);
% Total area
area_tot = trapz(x,y)
area_tot = -4.5442e+03
% Area below peak
area_peak = trapz(x(imax-50:imax+50),y(imax-50:imax+50))
area_peak = -26.6612
figure
plot(x,y,x(imax-50:imax+50),y(imax-50:imax+50),'r')
Mind that the areas are both negative because your curve is almost all below the x axis.
  2 件のコメント
Haris Ashraf
Haris Ashraf 2022 年 4 月 11 日
Here you assumed that peak will start and end 50 points ahead and back from the index of the peaks respectviely. Is there any particular reason for this assumption?
Davide Masiello
Davide Masiello 2022 年 4 月 11 日
編集済み: Davide Masiello 2022 年 4 月 11 日
No, you can vary that number depending on what you decide your peak is. I have added a plot in my answer to show you what 50 points around the maximum actually encompasses.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by