10
$\begingroup$

One of the great new function in v10 is PeakDetect.

It can detect peaks (local maxima) according to desired sharpness as shown in this example:

data = Table[{x, (Sin[10 x] + 2) Exp[-x^2]}, {x, -4, 4, .01}];
peaks = Pick[data, PeakDetect[data[[;; , 2]], .01, .0005], 1];
ListPlot[{data, peaks}, 
 PlotStyle -> {Automatic, Directive[Red, PointSize[0.02]]}]

enter image description here

The question is how to find valleys (local minima) of the such data using this new function?

$\endgroup$
  • 5
    $\begingroup$ Apply it to $-f$? $\endgroup$ – Mark McClure Jul 14 '14 at 1:04
  • $\begingroup$ @MarkMcClure Just saw your comment after I posted it. Yes, that is the first thing I though about also. $\endgroup$ – Nasser Jul 14 '14 at 1:08
  • $\begingroup$ @MarkMcClure great trick. thanks to you and to Nasser. $\endgroup$ – Algohi Jul 14 '14 at 1:09
15
$\begingroup$

As has been observed

enter image description here

data = Table[{x, (Sin[10 x] + 2) Exp[-x^2]}, {x, -4, 4, .01}];
peaks = Pick[data, PeakDetect[data[[;; , 2]], .01, .0005], 1];
troughs = Pick[data, PeakDetect[-data[[;; , 2]], .01, .0005], 1];
ListPlot[{data, peaks, troughs}, 
 PlotStyle -> {Automatic, Directive[Red, PointSize[0.02]], 
   Directive[Green, PointSize[0.02]]}]
$\endgroup$
  • $\begingroup$ It would be nice to have a method to find local minima and maxima in one go. For large data, this doubles the time, somewhat unnecessarily. $\endgroup$ – István Zachar Apr 27 '16 at 11:38
  • $\begingroup$ @IstvánZachar yes you are right. I will think about . It approaches 2 years since the answer. MMa has changed, I have changed but not in a desirable direction. I would very much value a better answer than mine. If you post one "comment me" in. $\endgroup$ – ubpdqn Apr 27 '16 at 11:41
9
$\begingroup$

Not an answer, more of a extended comment. (Since the question requires the use of PeakDetect.)

Some (more than half actually) of the local extrema are missed. This becomes obvious using Log plots (modifying the code of ubpdqn):

data = Table[{x, (Sin[10 x] + 2) Exp[-x^2]}, {x, -4, 4, .01}];
peaks = Pick[data, PeakDetect[data[[;; , 2]], .01, .0005], 1];
troughs = Pick[data, PeakDetect[-data[[;; , 2]], .01, .0005], 1];
opts = {PlotStyle -> {Automatic, Directive[Red, PointSize[0.02]], 
     Directive[Green, PointSize[0.02]]}, PlotTheme -> "Detailed", 
   ImageSize -> Medium};
Grid[{{ListPlot[{data, peaks, troughs}, opts], 
   ListLogPlot[{data, peaks, troughs}, opts]}}]

enter image description here

I tried few times to get better results by tweaking the PeakDetect parameters without success.

Using Quantile regression fitting to find the local extrema gives better results:

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/Applications/QuantileRegressionForLocalExtrema.m"]

Block[{data = data, qfuncs, showFunc},
 {qfuncs, extrema} = QRFindExtrema[data, 40, 2, 12, {0.5}];
 showFunc[listPlotFunc_] :=
  listPlotFunc[Join[{data}, extrema], 
   PlotStyle -> {{}, {PointSize[Medium], Green}, {PointSize[Medium], 
      Red}}, PlotTheme -> "Detailed", PlotRange -> All, 
   ImageSize -> Medium];
 Grid[{{showFunc[ListPlot], showFunc[ListLogPlot]}}]
 ]

enter image description here

Here is a related discussion: "Finding very weak peaks".

$\endgroup$
7
$\begingroup$
data = Table[{x, -(Sin[10 x] + 2) Exp[-x^2]}, {x, -4, 4, .01}];
peaks = Pick[data, PeakDetect[data[[;; , 2]], .01, .0005], 1];
ListPlot[{data, peaks}, PlotStyle -> {Automatic, Directive[Red, PointSize[0.02]]}];
Rotate[%, 180 Degree]

Mathematica graphics

Or as Pickett mentioned below, just add - to data as in

data = Table[{x, (Sin[10 x] + 2) Exp[-x^2]}, {x, -4, 4, .01}];
peaks = Pick[data, PeakDetect[-data[[;; , 2]], .01, .0005], 1];
ListPlot[{data, peaks}, PlotStyle -> {Automatic, Directive[Red, PointSize[0.02]]}]
$\endgroup$
  • 2
    $\begingroup$ It's easier to just add - in front of data[[;;,2]]. This way you don't have to rotate the graph. $\endgroup$ – C. E. Jul 14 '14 at 1:12
  • $\begingroup$ @Nasser, it would better if you ListPlot[{-data,-peaks},....] $\endgroup$ – Algohi Jul 14 '14 at 1:13
  • $\begingroup$ @Pickett, sure that is better so no rotation is needed. Thanks. $\endgroup$ – Nasser Jul 14 '14 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.