Search code examples
rdplyrmaxs-plus

Finding the peaks and troughs of data


I have the following data:

              [,1] [,2]  [,3]
statSignal   Time   Max_Val Min_Val 
            5001.000  172  -179
            8334.333  162  -188
           11667.667 1205  -674
           15001.000 1205 -1649
           18334.333 1205 -1649
           21667.667  703 -1649
           25001.000  632  -669
           28334.333  881  -872
           31667.667 1640  -872
           35001.000 1640  -872
           38334.333 1640  -552
           41667.667  215  -444
           45001.000 1408  -652
           48334.333 1408 -1711
           51667.667 1408 -1711
           55001.000  772 -1711
           58334.333  540  -205
           61667.667  268  -205
           65001.000  268  -205

For example, for this the local peaks of this data from max_val would be {1205, 1640, 1408}. I used the function peak to find these values. However, what i found was that if the local maxima was the first value as seen in the following data set for X2, the peak would not be recognized. If there was any way of solving this problem, I would immensely appreciate it (maybe manipulating the peaks function or finding another function/ writing more in depth coe). Many thanks!

           [,1] [,2]  [,3]
statSignal Time   Max_Val Min_Val
            5001.000 1298 -2176
            8334.333 1048 -2176
           11667.667 1048  -222
           15001.000  294  -222
           18334.333  132  -221
           21667.667 1959  -925
           25001.000 1959 -2064
           28334.333 1959 -2064
           31667.667 1326 -2064
           35001.000 1326  -344
           38334.333  402 -1237
           41667.667 1754 -2861
           45001.000 4369 -2861
           48334.333 4369 -2861
           51667.667 4369 -2020
           55001.000  851 -2020


Solution

  • Are you using splus2R::peaks()? If so, setting end_behavior = 1 should give you what you want. From the docs:

    endbehavior

    a value of 0, 1 or 2 that determines how peaks are computed within a halfwidth (= floor(span/2)) of the ends of the sequence. A value of 0 means no value within halfwidth of the start or end can be considered a peak. A value of 1 means maximum values within halfwidth of the start or end can be considered a peak. A value of 2 returns NA for the values within halfwidth of the start or end. The default is endbehavior=0).

    With your data:

    library(splus2R)
    
    Max_Val_Peaks <- peaks(X2[, "Max_Val"], span = 5, strict = FALSE, endbehavior = 1)
    
    X2[, "Max_Val"][Max_Val_Peaks]
    # 1298 1959 1959 1959 4369 4369 4369