# How to generate Image from Raw Data and find Peak Value

4 views (last 30 days)
Med Future on 27 Jan 2023
Commented: Mathieu NOE on 31 Jan 2023
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

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);
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.

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
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)]);
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)
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

### Categories

Find more on Get Started with Statistics and Machine Learning Toolbox in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!