MATLAB Answers

How to calculate the peak areas of every single peak from the mass spectrum?

69 views (last 30 days)
Maggie Chen
Maggie Chen on 5 Apr 2019
Edited: Maggie Chen on 10 Apr 2019
Hi, I am wondering any toolboxs or codes could be used to calculate the peak areas of every single peak from the txt file or fig file. I know how to integrate the peak area in the excel using the txt file, but have to find the tails from the baseline manually. The picture showed how peak area was identified. I also attached a txt file from the mass spectrum. For the output, the centroid mass and peak area of every peak will be needed.Peak Area.jpg

  0 Comments

Sign in to comment.

Answers (1)

A. Sawas
A. Sawas on 5 Apr 2019
Edited: A. Sawas on 5 Apr 2019
The function findpeaks can find the peak values and locations.
data = importdata('Example.txt');
plot(data);
[pks,loc] = findpeaks(data);
findpeaks(data); %Use findpeaks without output arguments to display the peaks
Then you can find the boundaries around that peaks in order to find the area.
for k = 1:length(loc)
i = loc(k);
while i > 1 && data(i-1) <= data(i)
i = i - 1;
end
b_min(k) = i;
i = loc(k);
while i < length(data) && data(i+1) <= data(i)
i = i + 1;
end
b_max(k) = i;
end
%b_min: minimum boundary
%b_max: maximum boundary
To find the area under the first peak:
A = trapz(data([b_min(1):b_max(1)]));
You can find the area under all the peaks then the one with maxium area as follows:
for k=1:length(b_min)
A(k) = trapz(X([b_min(k):b_max(k)]),Y([b_min(k):b_max(k)]));
end
[max_area, i] = max(A);
max_peak = loc(i);

  8 Comments

Show 5 older comments
Maggie Chen
Maggie Chen on 9 Apr 2019
I think it is because I named the script findpeaks. After I changed the name to findpeakareas and ran the script, the output is here.
Screen Shot 2019-04-09 at 18.36.56.png
A. Sawas
A. Sawas on 9 Apr 2019
Ok, so now the problem is with the input data. Since your data is in two columns format the first being the X and second is Y. You need to add this line and replace data in the remainng code with Y
X = data(:,1);
Y = data(:,2);
[pks,loc] = findpeaks(Y);
% the rest of the code after changing data to Y
Maggie Chen
Maggie Chen on 10 Apr 2019
Thanks! No errors this time! I got a figure as output. Could the output be saved as txt file? Also I believe the Y in the output is the peak height not the peak area of the individual peak and the X is not the X value at the peak centroid.
Thank you so much for help!

Sign in to comment.