MATLAB Answers

To find the first 3 local maximas of a plot/ dataset without the islocalmax and findpeaks

3 views (last 30 days)
GH
GH on 8 Jul 2020
Commented: Star Strider on 9 Jul 2020
Hi, everyone
Is it possible to find the first 3 local maximas of a plot/ dataset without the islocalmax and findpeaks syntax?
Thanks in advance, Grace

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 8 Jul 2020
Yes.
Is this for homework or an exam?

  4 Comments

Show 1 older comment
Star Strider
Star Strider on 8 Jul 2020
O.K.
Try this:
t = [1:0.01:5]; % Time Vector
y = sin(2*pi*t); % Signal
dydt = gradient(y, mean(diff(t))); % Derivative
zci = @(v) find(diff(sign(v))); % Define Function
zx = zci(dydt); % Zero Crossings
[max1,idx] = maxk(y(zx),3); % First Three Maxima & Indices
Out = [t(zx(idx)); y(zx(idx))]; % Result Matrix
figure
plot(t, y, '-b')
hold on
plot(Out(1,:), Out(2,:), 'r^')
hold off
grid
.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!