Find value between the overall maximum and its following minimum

1 view (last 30 days)
Felix R on 21 Nov 2021
Commented: Felix R on 22 Nov 2021
The command y = peak2peak(x) gives the value between the overall maximum max(y) and the overall minimum min(y).
In my case, 0,0052 and -0,0066 which equivalates to 0,0118.
I would need the value between the overall maximum and the immediate following minimum, 0,0052 and -0,0026 which equivalates to 0,0078.
Is there a command or work-around to return me the y-value of the minimum which follows the overall maximum? dpb on 21 Nov 2021
Edited: dpb on 21 Nov 2021
Couple ways...
First,
[mx,imx]=max(X); % return global max, location
imn=find(diff(X(imx:end))>=0,1)+imx; % find first point after imx slope X is positive
imn=imn-1; % correct back to the previous point -- the minimum
The resolution of your plot isn't good enough to tell; if there is a local minimum/any noise in the signal, the above will find that inflection, not, a global local minimum.
Obviously, the "-1" correction can be made in the initial search, don't need the extra code; left above as the original comment regarding finding the first point with a positive slope is correct; that explains why need a -1. So, I'd use
[mx,imx]=max(X); % return global max, location
imn=find(diff(X(imx:end))>=0,1)+imx-1; % minimum before first after imx slope X is positive
mn=X(imn); % and the value associated with location
Second,
[pkMx,iMx]=findpeaks(X,'NPeaks',1); % return max peak, location
[pkMn,iMn]=findpeaks(-X(iMx:end),1); % return min peaks, location rest of signal
The local minimum is the local peak of the negative of the input signal; findpeaks will return the peak and location for the minimum in the second call. With the optional input arguments to findpeaks you can fine tune what it returns to eliminate any small glitches that might be present in the signal that the first solution would find.
findpeaks does require the Signal Processing TB.
Felix R on 22 Nov 2021
Thank you very much!

Image Analyst on 21 Nov 2021
To get the very next local min immediately following the max (at some particular index), I'd just "fall down" the signal until it turns around and heads upwards again
minIndex = maxIndex + 1; % Initialize search to start at the index right next to the max.
if maxIndex < length(y) - 1
while y(minIndex + 1) <= y(minIndex) && (minIndex + 1) < length(y)
minIndex = minIndex + 1;
end
end

Image Analyst on 21 Nov 2021
If you have the Image Processing Toolbox you can use imregionalmax() and imregionalmin():
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 16;
y = rand(1, 25);
x = 1 : length(y);
plot(y, 'b.-', 'LineWidth',2, 'MarkerSize', 20)
grid on;
% Find local max
maxVector = imregionalmax(y)
xOfMaxes = x(maxVector)
yOfMaxes = y(maxVector)
% Plot max
hold on
plot(xOfMaxes, yOfMaxes, 'rv', 'LineWidth',2, 'MarkerSize',20);
% Find local min
minVector = imregionalmin(y)
xOfMins = x(minVector)
yOfMins = y(minVector)
% Plot min
hold on
plot(xOfMins, yOfMins, 'r^', 'LineWidth',2, 'MarkerSize',20);
% Say we want to find the y value of the first min after the third max.
% First get the index of where that min would be.
index = find(xOfMins > xOfMaxes(3), 1, 'first');
% Next get the y value there.
nextY = yOfMins(index) R2021b

Community Treasure Hunt

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

Start Hunting!