How to calculate the area under two peaks?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sam
el 15 de Ag. de 2022
Comentada: Sam
el 15 de Ag. de 2022
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.
0 comentarios
Respuesta aceptada
Walter Roberson
el 15 de Ag. de 2022
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)
[~,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(ybc, x)
Más respuestas (0)
Ver también
Categorías
Más información sobre Spectral Estimation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!