Suppose I have a list of data which is oscillating. I want to calculate the positon of multiple local minimas present in the data. How can i do this?
2 Answers
$\begingroup$
$\endgroup$
1
You can use FindPeaks
as follows:
ClearAll[findValleys]
findValleys = {1, -1} # & /@ FindPeaks[-#, 0] &;
SeedRandom[77]
l = RandomReal[{0, 1}, 10];
findValleys @ l
{{1, 0.000468887}, {3, 0.190391}, {6, 0.406117}, {9, 0.0674747}}
ListLinePlot[{l, findValleys @ l}, Joined -> {True, False},
PlotStyle -> {Blue, Directive[Red, PointSize[Large]]}]
Alternatively, you can use MinDetect
:
ClearAll[findValleys2]
findValleys2 = With[{p = PositionIndex[MinDetect @ #] @ 1}, Transpose[{p, #[[p]]}]] &;
findValleys @ l == findValleys2 @ l
True
$\begingroup$
$\endgroup$
You can define a simple function that compares neighbors of each points and checks if the value of a point is less than those of its neighbors.
positionOfMinima[l_] := Position[l, #] & /@ Pick[l[[2 ;; -2]], #, True] &@MovingMap[
#[[1]] > #[[2]] && #[[2]] < #[[3]] &, l, 2
] // Flatten
l = RandomReal[{0, 1}, 10];
ListLinePlot[l,
Epilog -> {Red, PointSize[Large],
Point[{#, l[[#]]}] & /@ positionOfMinima[l]}]