MATLAB Answers

How can I plot 2 data in 1 graph and find common peak

2 views (last 30 days)
How can I plot 2 data in 1 graph and find common peak
Common peak is a peak that appear at the same X coordinate but have different Y value
pure_brain = readmatrix('Pure Brain Spectra.csv');
[pks,locs] = findpeaks(pure_brain(:,2));
x = 1:size(pure_brain,1);
Y = pure_brain(locs(Lv),2);
Y = Y .* 100/max(Y);
[pks_min,pks_max] = bounds(pks) % Minimum & Maximum Values Of ‘pks’
Lv = pure_brain(locs,2)>1E+4; % Set Threshold = 1E+4
% Lv = pure_brain(locs,2)>1; % Set Threshold = 1
%plot(x(locs(Lv)), pure_brain(locs(Lv),2), '.r')
plot(x(locs(Lv)), Y, '.r')
title('Pure brain peak');
%YB1=AB(end); %1Y
%YB2=AB(end-1); %2Y
%YB3=AB(end-2); %3Y
%SYB = [ YB1 YB2 YB3 ];
%SXB = [ XB1 XB2 XB3 ];

Accepted Answer

Pavan Guntha
Pavan Guntha on 31 Mar 2021
Hi Phudit,
You can refer to documentation of hold function to plot 2 different plots on same figure. To calculate the common peak, you can first find the common indices where a peak exists for both the datasets and then find the value of the peaks occured at those indices. The following code illustrates this idea:
pure_brain = readmatrix('Pure Brain Spectra.csv');
[pks,locsBr] = findpeaks(pure_brain(:,2));
LvBrain = pure_brain(locsBr,2)>1E+4; % Set Threshold = 1E+4
xBrain = 1:size(pure_brain,1);
YBrain = pure_brain(locsBr(LvBrain),2);
YBrain = YBrain .* 100/max(YBrain);
[pks_min,pks_max] = bounds(pks); % Minimum & Maximum Values Of pks
pure_Liver = readmatrix('Pure Liver Spectra.csv');
[pks,locsLiv] = findpeaks(pure_Liver(:,2));
LvLiver = pure_Liver(locsLiv,2)>1E+4; % Set Threshold = 1E+4
xLiver = 1:size(pure_Liver,1);
YLiver = pure_Liver(locsLiv(LvLiver),2);
YLiver = YLiver .* 100/max(YLiver);
plot(xBrain(locsLiv(LvBrain)), YBrain, '.r')
hold on
plot(xLiver(locsLiv(LvLiver)), YLiver, '*b')
hold off
% To find common peaks considering the threshold of 1E+4:
lB = locsBr(LvBrain);
lL = locsLiv(LvLiver);
s = max(length(lB), length(lL));
locsLivNew = [lL; false(s-length(lL),1)];
locsBrNew = [lB; false(s-length(lB),1)];
common = locsLivNew == locsBrNew; % Find the common location of peaks
Hope this helps!
  1 Comment
Phudit Kanittasut
Phudit Kanittasut on 2 Apr 2021
Your answer is so helpful ,thank you , but I have one question I open common and I cant get the location of the common peak.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!