How can i find the peak/maxima of a plot in just a selected region?

2 views (last 30 days)
Bernardo Farfan Valencia
Bernardo Farfan Valencia on 22 Dec 2021
Answered: Star Strider on 22 Dec 2021
I have a plot of 2 vectors in a graph. I have to find the peak/maxima of just a small region. This region changes for different vectors. Can i choose that region somehow on the plot and find the correct peak?

Answers (3)

KSSV on 22 Dec 2021
If you know the range of your values, use logical indexing and use max to get maximum in that region.
If you know the range of your maximum value, use findpeaks and pick the maximum/ peak value in your required range using logical indexing.

Image Analyst
Image Analyst on 22 Dec 2021
If you have a vector and you know the starting and ending indexes of the region where you want to look, you can do
signal2 = signal; % Make copies of original
% Make outside the region minus infinity so we don't find anything there
signal2(1:startingIndex) = -inf;
signal2(endingIndex : end) = -inf;
% Find max value
maxValue = max(signal2);
% Find all the indexes where this value is located (there might be more than one)
indexesOfMax = signal2 == maxValue

Star Strider
Star Strider on 22 Dec 2021
I am not exactly certain what the problem is, however I am assuming that there are many peaks and the intent is simply to get the one in the region-of-interest.
I would do this in two steps —
First: Find the peak locations
Second: Isolate the peak in the region of interest
Example —
t = linspace(0, 10, 1E+3); % Independent Variable
s = sin(2*pi*t/3) .* cos(2*pi*t/7); % Signal
[pks,locs] = findpeaks(s) % Detect Peaks (Use Appropriate Name-Value Pairs To Detect The Correct Peaks)
pks = 1×5
0.8178 0.5402 0.2351 0.9788 0.0290
locs = 1×5
63 250 485 679 888
idxv = randi(numel(t), 1, 2) % Region-Of-Interest Index Begin & End Values
idxv = 1×2
782 905
idxrng = min(idxv) : max(idxv); % Index Range VEctor
ChosenPeakIdx = locs( (locs>=min(idxrng)) & (locs <= max(idxrng))) % Peak In Region-Of-Interest
ChosenPeakIdx = 888
plot(t, s, 'DisplayName','Original Data')
hold on
plot(t(ChosenPeakIdx), s(ChosenPeakIdx), '^r', 'MarkerFaceColor','r', 'DisplayName','Selected Peak')
patch([t(idxrng) flip(t(idxrng))], [ones(size(idxrng))*min(ylim) ones(size(idxrng))*max(ylim)], [1 1 1]*0.25, 'FaceAlpha',0.25, 'EdgeColor','none', 'DisplayName','Region-Of-Interest')
hold off
Experiment with the available data to get the desired results.




Community Treasure Hunt

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

Start Hunting!