How to calculate the area under two peaks?

조회 수: 4 (최근 30일)
Sam
Sam 2022년 8월 15일
댓글: Sam 2022년 8월 15일
I am trying to calculate the area under two peaks before and after background subtraction.
My code currently looks like this (example data is attached, in .txt form):
%Import the data in from excel
num = importdata('i22-569960_example.dat')
%Smooth data
data = smoothdata(num)
%Calculate area of full graph
x = data(:,1);
y = data(:,2);
[~,imax] = max(y);
area_tot = trapz(x,y)
%Baseline correction
B = [x([1 end]) ones(2,1)] \ y([1 end]);
ydt = [x ones(size(y))] * B;
ybc = y-ydt; % Baseline Corrected
figure
plot(x, ybc)
grid
%Calculate area under peaks
[pks, locs, w, p] = findpeaks(x, ybc)
As you can see, I have managed to calculate the total area under the peaks, however I am having issues calculating the area under the peak with findpeaks following this. This is the error code:
Error using findpeaks
Expected X to be strictly increasing.
Error in findpeaks>parse_inputs (line 236)
validateattributes(Xin1,{'numeric'},{'real','finite','vector','increasing'},'findpeaks','X');
Error in findpeaks (line 135)
= parse_inputs(isInMATLAB,Yin,varargin{:});
Error in OrientationScript (line 25)
[pks, locs, w, p] = findpeaks(x, ybc)
I'm not quite sure why findpeaks won't work with my data, as my X axis is increasing . I have also looked at using trapz, but I am less familar with that.

채택된 답변

Walter Roberson
Walter Roberson 2022년 8월 15일
y goes before x in findpeaks() call.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1098535/i22-569960_example.txt';
num = readmatrix(filename);
data = smoothdata(num);
x = data(:,1);
y = data(:,2);
issorted(x)
ans = logical
1
[~,imax] = max(y);
area_tot = trapz(x,y)
area_tot = 2.4505e+04
%Baseline correction
B = [x([1 end]) ones(2,1)] \ y([1 end]);
ydt = [x ones(size(y))] * B;
ybc = y-ydt; % Baseline Corrected
figure
plot(x, ybc)
grid
%Calculate area under peaks
[pks, locs, w, p] = findpeaks(ybc, x)
pks = 2×1
36.6826 36.0098
locs = 2×1
77.7273 265.9091
w = 2×1
97.9142 83.9262
p = 2×1
36.6826 33.3195
  댓글 수: 1
Sam
Sam 2022년 8월 15일
Oh wow, that was a very easy fix! How on earth did I miss that?!
Thank you so much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by