How to calculate the area under two peaks?

5 次查看(过去 30 天)
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 个)

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by