Finding local minima and maxima

86 Ansichten (letzte 30 Tage)
Samuel Courtney
Samuel Courtney am 1 Feb. 2021
Kommentiert: Star Strider am 2 Feb. 2021
Hi,
i have been trying find a way of plotting the local maxima and local minima.
I have used the findpeaks(); fuinction for the both of them, but it gives me two signals being plotted, i belive this will be because the singal has been negatively multiplied to try to find the local minima.
However i am trying to plot both max and min onto the same singnal, and the markers to be of diffenet colour.
Below is the code that i have tried to use, ive inlcuded a seperate way of finding local minima, but i am unsure of how to plot this on the same singal.
t = (0:100)*0.6*10^-3;
x1 = 3*sin(2*pi*100*t);
findpeaks(x1);
hold on
findpeaks(-x1);
%TF = islocalmin(x1);
%plot(t,x1,t(TF),x1(TF),'r*')

Akzeptierte Antwort

Star Strider
Star Strider am 1 Feb. 2021
You already found the correct functions. Applying them appropriately appears to be the problem.
Try this:
t = (0:100)*0.6*10^-3;
x1 = 3*sin(2*pi*100*t);
TFmax = islocalmax(x1);
TFmin = islocalmin(x1);
figure
plot(t, x1)
hold on
plot(t(TFmax), x1(TFmax), '^r', 'MarkerFaceColor','r')
plot(t(TFmin), x1(TFmin), 'vg', 'MarkerFaceColor','g')
hold off
grid
.
  2 Kommentare
Samuel Courtney
Samuel Courtney am 1 Feb. 2021
Hi,
Thanks for the great suggestion.
is there any way of using the findpeaks fucntion for this ??
Star Strider
Star Strider am 2 Feb. 2021
My pleasure!
To use findpeaks requires a bit of creativity.
[pks,plocs] = findpeaks(x1);
[vlys,vlocs] = findpeaks(-x1);
figure
plot(t, x1, '-b')
hold on
plot(t(plocs), pks, '^r', 'MarkerFaceColor','r')
plot(t(vlocs), -vlys, 'vg', 'MarkerFaceColor','g')
hold off
grid
Producing essentially the same plot.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by