How do I determine the peak area with findpeak?
Show older comments
I am trying to calculate the area of the peaks from my HPLC run. With the findpeak function I can get the height and the weight at 50% of the peak. Is there a findpeak parameter to either get the area or the starting and end points of each peaks?
Is there a different function or approach I could try?
Answers (2)
Star Strider
on 23 Dec 2022
0 votes
It would help to have an example of your data..
See if the approach in How can I calculate the area under each peak / display the AUC on the graph? will do what you want.
.
2 Comments
Jasmin
on 23 Dec 2022
Your peaks do not correspond the those of the previous code, so I needed to tweak my code to accomomodate them.
Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1241547/HPLC_VV1.txt', 'VariableNamingRule','preserve')
t = T1{:,1};
s = T1{:,2};
sr = s;
tr = t;
sfcn = @(b,x) b(1).*exp(-(x-b(2)).^2*b(3)) + b(4);
[pks,locs,fwhm,prom] = findpeaks(sr, 'MinPeakProminence',5E+4, 'WidthReference','halfheight');
figure
plot(tr,sr)
hold on
plot(tr(locs),sr(locs),'^r')
hold off
grid
xlim([0 20])
% xlim([0 50])
% ylim([-0.01 0.2])
format shortg
for k = 1:numel(locs)
ixrng = max(1,locs(k)-100) : min(locs(k)+100,numel(t));
trv = tr(ixrng) - tr(ixrng(1));
B(k,:) = fminsearch(@(b)norm(sr(ixrng) - sfcn(b,trv)), [pks(k)*1 median(trv) 10 0.01]);
sfit{k} = [trv+tr(ixrng(1)), sfcn(B(k,:),trv), ixrng(:)];
figure
% plot(sfit{k}(:,1), [sfit{k}(:,2) sr(ixrng)])
Area(k,:) = trapz(trv, sfit{k}(:,2));
end
% B
% Area
figure
plot(tr,sr)
hold on
for k = 1:numel(locs)
plot(sfit{k}(:,1), sfit{k}(:,2),'-r')
end
% plot(tr(locs),sr(locs),'^r')
hold off
grid
text(tr(locs), pks, compose('\\leftarrow Area = %.2fx10^{3}', Area*1E-3), 'Horiz','left', 'Vert','middle', 'Rotation',30, 'FontSize',7)
% xlim([-10 max(tr)])
ylim([-0.01 5E+6])
PeakAreas = table(tr(locs),Area, 'VariableNames',{'PeakCentre','Area'})
% figure
% plot(tr,sr)
% hold on
% for k = 1:numel(locs)
% plot(sfit{k}(:,1), sfit{k}(:,2),'-r')
% end
% % plot(tr(locs),sr(locs),'^r')
% hold off
% grid
% text(tr(locs), pks, compose('\\leftarrow Area = %.2fx10^{3}', Area*1E-3), 'Horiz','left', 'Vert','middle', 'Rotation',30, 'FontSize',7)
% xlim([0 17])
% % ylim([-0.01 0.3])
% title('Subset Showing Detail')
Check to be sure those make sense. (HPLC brings back some interesting memories!)
.
Image Analyst
on 23 Dec 2022
Not sure how you're defining the area. Is it the signal under the half max points? Is it the total area under the curve between the valleys on either side of the peak? You can get the valley points by negating the signal
[valleyValues, indexesOfValleys] = findpeaks(-signal);
valleyValues = -valleyValues; % Need to undo signal inversion.
Then to get the area under the curve one way to do it is to sum the signal samples. For example if you want the sum of the signal between the 2nd valley and the 3rd valley (inclusive) you can do
area = sum(signal(indexesOfValleys(2) : indexesOfValleys(3))
3 Comments
Jasmin
on 23 Dec 2022
Jasmin
on 23 Dec 2022
Image Analyst
on 23 Dec 2022
Edited: Image Analyst
on 23 Dec 2022
How about if you simply threshold it and use regionprops. It's in the Image Processing Toolbox. Something like
fileName = 'HPLC_VV1.txt';
data = readmatrix(fileName)
x = data(:, 1);
y = data(:, 2);
subplot(2, 1, 1);
plot(x, y, 'b-');
title('Original Signal')
grid on;
mask = y > 3e4;
props = regionprops(mask, y, 'MeanIntensity', 'Area')
areas = [props.MeanIntensity] .* [props.Area]
subplot(2, 1, 2);
bar(areas)
grid on;
axis on;
title('Areas under the peaks')
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

