0
$\begingroup$

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

lstPrices={4.36,4.32,4.2,4.2,4.22,4.12,4.28,4.29,4.29,4.31,4.25,4.35,4.59,4.68,4.61,4.59,5.05,4.95,5.09,5.11,4.99,4.96,5.11,5.37,5.6,5.38,5.42,5.36,4.9,4.92,4.98,4.89,4.99,4.8,4.79,4.62,4.65,4.7,4.68,4.7,4.81,4.84,4.77,4.85,4.78,4.69,4.71,4.66,4.69,4.78,4.78,4.81,4.85,4.78,5.1,5.29,5.19,5.28,5.22,5.18,5.07,5.08,5.09,5.07,5.1,5.05,5.05,5.13,5.1,5.09,5.21,5.24,5.26,5.35,5.19,5.24,5.09,5.18,5.19,5.18,5.13,5.15,5.06,5.09,5.08,5.01,4.99,4.99,4.94,4.98,4.92,4.87,4.91,4.91,4.92,4.95,4.9,4.93,4.99,5.04,4.98,5.17,5.07,5.08,5.14,5.17,5.08,5.53,5.57,5.49,5.47,5.64,5.48,5.47,5.31,5.36,5.35,5.31,5.37,5.35};

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!

$\endgroup$
3
$\begingroup$

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

ClearAll[JFindPeaks];

sigma = 5;
JFindPeaks[list_List] := FindPeaks[list, sigma]
$\endgroup$
3
  • $\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.