I want to use zigzag curve to describe the trend of simple data. here is a list as


and I give new definition of FindPeaks and the related.

JFindPeaks[list_?ListQ] := MapAt[Round, FindPeaks[list] // N, {All, 1}]
JFindValleys[list_?ListQ] := Module[{x, y}, Map[({x, y} = #; {x, -y}) &, JFindPeaks[-list]]]
JFindExtremes[list_?ListQ] := Sort[JFindPeaks[list]~Join~JFindValleys[list]]

then some lists are computed as

peaks = JFindPeaks[lstPrices];
valls = JFindValleys[lstPrices];
extrs = JFindExtremes[lstPrices];

and two plots too,

p1 = ListLinePlot[lstPrices,
   Epilog -> {
     {Red, PointSize[0.015], Point[peaks]},
     {Blue, PointSize[0.015], Point[valls]}},
   PlotStyle -> Directive[Black, Dotted]
p2 = Graphics@Line@extrs;

finnally, the target plot comes out.

Show[p1, p2,
 AspectRatio -> 1/GoldenRatio,
 Frame -> True,
 GridLines -> Automatic,
 GridLinesStyle -> Directive[Gray, Dotted],
 ImageSize -> Large

It's like this,

enter image description here

but the most I want to get could be like the following one or the other similarly, or these sub-peaks-valleys should be ellminated on the plot.

enter image description here

so how to realize it? Maybe a threshold value is necessary. Thanks!


You can adjust the Gaussian blurring scale $\sigma$ in FindPeaks to do that:


sigma = 5;
JFindPeaks[list_List] := FindPeaks[list, sigma]
  • $\begingroup$ Yes! Thanks more! $\endgroup$
    – Jerry
    Apr 23 '17 at 7:23
  • $\begingroup$ How to compute the sigma value according to increase or decrease range by the target plot adjacent peak-valley points? e.x. If range = 0.20(20%), then sigma = ?, is this poissible ? $\endgroup$
    – Jerry
    Apr 23 '17 at 7:48
  • 1
    $\begingroup$ @Jerry sigma is a blurring parameter. It is related to the width of small peaks which you want to ignore. If you want to ignore small peaks which are narrower than 20% of your full range (120 units) you may take sigma about 0.20 * 120 / 2. $\endgroup$
    – Shadowray
    Apr 23 '17 at 8:27

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.