How to generate Image from Raw Data and find Peak Value

5 views (last 30 days)
Hello, I have the following Dataset in which first column shows the X-axis and 2nd Column shows the Y-axis.
I want to create Image from the Dataset and find Peak Value as shown in the image below and
Save the X and Y axis Values Corresponding Peak.
How can i do that in MATLAB

Answers (2)

Nikhilesh
Nikhilesh on 27 Jan 2023
For your case it would be better if you can pronounce i want to creat a plot/graph out of a dataset instead of image. For plotting the data you can use the following code.
plot(Dataset(:,1),Dataset(:,2));
Plese have a look at the "max" function. Which you can use to find the peak
peak_value = max(Dataset(:,2));
peak_index = find(Dataset(:,2) == peak_value);
peak_x = Dataset(peak_index, 1);
peak_y = Dataset(peak_index, 2);
  1 Comment
Med Future
Med Future on 31 Jan 2023
In the Following Dataset I have two peaks. Can you please modified the code for this.
For example. find the first peak and its X and Y values. then Find the 2nd peak and its X and Y Values.
And Subtract the 2nd X value with 1st X value.

Sign in to comment.


Mathieu NOE
Mathieu NOE on 27 Jan 2023
Edited: Mathieu NOE on 27 Jan 2023
hello
try this code below
the peak coordinates are stored in xmax and ymax
for one single peak , max is sufficient; if you need to identify multiple peaks you can use findpeaks , islocalmax, islocalmin
load('DatasetCir.mat')
x = Dataset(:,1);
y = Dataset(:,2);
% find max value
[ymax,idx] = max(y);
xmax = x(idx);
plot(x,y,'o',xmax,ymax,'*r');
text(xmax*1.005,ymax,['x = ' num2str(xmax)]);
text(xmax*1.005,ymax*0.95,['y = ' num2str(ymax)]);
  7 Comments
Mathieu NOE
Mathieu NOE on 31 Jan 2023
the definition of "peaks" can vary from one data to another and what you are looking for
for example one person is interested to find not only the dominant peaks but also some local maxima
like on this plot
so this person want all green points , but you don't want this maybe
that's why there is no single method that covers all needs ; somehow you need to tell the algo what you want
this is why findpeaks does have many options , you need to learn how to use it
I can show you an alternative , but here again you must give two parameters to get the right output
again, there is no magical code that can detect in your brain what you want and how to proceed
you can look at the File Exchange and pick one function that may better serve your needs (here I choose : PeakSeek - File Exchange - MATLAB Central (mathworks.com) )
load('twopeaksdata.mat')
x = Twopeaksdata(:,1);
y = Twopeaksdata(:,3);
% find max value
% [ymax,idx] = findpeaks(y,'NPeaks',2,'SortStr','descend');
% [ymax,idx] = findpeaks(y,'MinPeakHeight',max(y)/2,'SortStr','descend');
% alternative with peakseek (Fex :
minpeakdist = 1000; %
minpeakh = max(y)/10;
[idx, ymax]=peakseek(y,1000,max(y)/2);
xmax = x(idx);
% Subtract the 2nd X value with 1st X value.
deltaX = xmax(2) - xmax(1)
plot(x,y,'o',xmax,ymax,'*r');
for ck = 1:numel(xmax)
text(xmax(ck)+0.05*max(x),ymax(ck),['x = ' num2str(xmax(ck))]);
text(xmax(ck)+0.05*max(x),ymax(ck)*0.95,['y = ' num2str(ymax(ck))]);
end
% Fex submission :
% https://fr.mathworks.com/matlabcentral/fileexchange/26581-peakseek?s_tid=srchtitle
function [locs, pks]=peakseek(x,minpeakdist,minpeakh)
% x is a vector input (generally a timecourse)
% minpeakdist is the minimum desired distance between peaks (optional, defaults to 1)
% minpeakh is the minimum height of a peak (optional)
%
% (c) 2010
% Peter O'Connor
% peter<dot>ed<dot>oconnor .AT. gmail<dot>com
if size(x,2)==1, x=x'; end
% Find all maxima and ties
locs=find(x(2:end-1)>=x(1:end-2) & x(2:end-1)>=x(3:end))+1;
if nargin<2, minpeakdist=1; end % If no minpeakdist specified, default to 1.
if nargin>2 % If there's a minpeakheight
locs(x(locs)<=minpeakh)=[];
end
if minpeakdist>1
while 1
del=diff(locs)<minpeakdist;
if ~any(del), break; end
pks=x(locs);
[garb, mins]=min([pks(del) ; pks([false del])]); %#ok<ASGLU>
deln=find(del);
deln=[deln(mins==1) deln(mins==2)+1];
locs(deln)=[];
end
end
if nargout>1
pks=x(locs);
end
end
Mathieu NOE
Mathieu NOE on 31 Jan 2023
here another altrenative with
this is very simple to use as this function only ask you the number of peaks to find out
it differs from more complex functions like findpeaks (with the many options)
load('twopeaksdata.mat')
x = Twopeaksdata(:,1);
y = Twopeaksdata(:,3);
% find max value
% [ymax,idx] = findpeaks(y,'NPeaks',2,'SortStr','descend');
% [ymax,idx] = findpeaks(y,'MinPeakHeight',max(y)/2,'SortStr','descend');
% alternative with peakfind
Np = 2;
[PeakData]=peakfind(x,y,Np); % PeakData is 3 columns array : data indice , x , y values
xmax = PeakData(:,2);
ymax = PeakData(:,3);
% Subtract the 2nd X value with 1st X value.
deltaX = xmax(2) - xmax(1)
plot(x,y,'o',xmax,ymax,'*r');
for ck = 1:numel(xmax)
text(xmax(ck)+0.05*max(x),ymax(ck),['x = ' num2str(xmax(ck))]);
text(xmax(ck)+0.05*max(x),ymax(ck)*0.95,['y = ' num2str(ymax(ck))]);
end
function [PeakData]=peakfind(Xdata,Ydata,NumPeaks)
% function [PeakData]=peakfind(Xdata,Ydata,NumPeaks)
% Returns array PeakData with NumPeaks entries consisting of
% the xindex,xvalue,yvalue
% If the Ydata is a complex array, look at the magnitude.
%
% make sure data is row oriented (MN)
Xdata = (Xdata(:))';
Ydata = (Ydata(:))';
iYpeak = 3;
PeakState = [1 1 -inf];
PeakData = [];
ibeg = 1; % ignore DC line
iend = length(Ydata);
if ~isreal(Ydata)
Ydata = abs(Ydata);
end;
for i = 1:NumPeaks
ix=find(Ydata(ibeg:iend) > [-inf, Ydata(ibeg:iend-1)] & ... % must be > point on left
Ydata(ibeg:iend) > [Ydata(ibeg+1:iend), -inf] & ... % must be > point on right
Ydata(ibeg:iend) < PeakState(iYpeak)); % must be < previous peak
if isempty(ix)
[y , ix] = max(Ydata); % no more peaks: go to max
else
[y , i] = max(Ydata(ix)); % there are more: go to biggest
ix = ix(i);
end;
PeakState= [ix,Xdata(ix),y];
PeakData = [PeakData;PeakState];
end

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!