# Distance between Global Maximum of a Wave Pattern and next Consecutive Maximum/Minimum

7 views (last 30 days)
Hollis Williams on 9 Jul 2017
Answered: David Fink on 17 Jul 2017 I have a set of wave patterns (one example wave pattern included with highest peak visible somewhere around x = 420) with various alternating peaks and troughs and have been able to determine the global maximum of the wave pattern using 'max'. I now need to determine the distance from this global maximum to the next consecutive local minimum immediately afterwards and the distance to the next consecutive local maximum.
I have used findpeaks to find positive and negative peaks using the code as follows:
pospks = findpeaks(h_dat(end,xi(i):xf(i)));
negpks = findpeaks(-((h_dat(end,xi(i):xf(i)))))
since the negative version should return the minima. h_dat(end,xi(i):xf(i)) is just the code which returns the wave pattern based on my data (corresponding to multiple wave patterns with different parameters at 'end times' when a stable state has been reached with a corresponding stable global maximum). I have attempted to sort them using the sort function but I am not sure if that helps as it will put them in ascending/descending order and you can see from my image that the heights of the local maxima and minima tend to be arbitrary so sorting them in ascending/descending order would not guarantee finding the distance from the highest peak (call it hmax) to the next maximum or minimum. My idea was to use a distance function to find the distance from hmax to the next closest member of pospks and then to the closest member of negpks in order to find both relevant distances but have researched into it and still not sure how to do this (apologies if all this is very simple as I am quite new to Matlab).

David Fink on 17 Jul 2017
To find the next minimum and maximum after the global maximum, use the "findpeaks" function, but limit the data to after the global maximum occurred. See the attached picture with the output of this code.
% Sample Data:
x = 0:0.1:20;
y = sin(x).*exp(-abs(x-10)/10);
plot(x,y);
hold on;
% Find global maximum and its index
[globalMaxY,globalMaxIndex] = max(y);
globalMaxX = x(globalMaxIndex);
% Plot global maximum in red
plot(globalMaxX,globalMaxY,'Marker','o','MarkerSize',10,'MarkerEdgeColor','red');
% Create a subset of data limited to after the global maximum
xSubset = x(globalMaxIndex:end);
ySubset = y(globalMaxIndex:end);
% Find the minimum peaks after the global maximum
[negHeights,xMinLocations] = findpeaks(-ySubset,xSubset);
minPeakX = xMinLocations(1);
minPeakY = -negHeights(1);
% Plot the next minimum in blue
plot(minPeakX,minPeakY,'Marker','o','MarkerSize',10,'MarkerEdgeColor','blue');
% Find the maximum peaks after the global maximum
[heights,xMaxLocations] = findpeaks(ySubset,xSubset);
maxPeakX = xMaxLocations(1);
maxPeakY = heights(1);
% Plot the next maximum in green
plot(maxPeakX,maxPeakY,'Marker','o','MarkerSize',10,'MarkerEdgeColor','green');
% From here you can compute x and y distances between the given maxima and minimum. 