How can i find the peak/maxima of a plot in just a selected region?
22 views (last 30 days)
Show older comments
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?
0 Comments
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.
0 Comments
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
0 Comments
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)
idxv = randi(numel(t), 1, 2) % Region-Of-Interest Index Begin & End Values
idxrng = min(idxv) : max(idxv); % Index Range VEctor
ChosenPeakIdx = locs( (locs>=min(idxrng)) & (locs <= max(idxrng))) % Peak In Region-Of-Interest
figure
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
grid
legend('Location','best')
Experiment with the available data to get the desired results.
.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!