使用pracma R包中的findpeaks函数识别持续峰

时间:2017-12-20 20:38:43

标签: r

我在peakpat R包(v.2.1.1)中的findpeaks函数中遇到pramca选项的语法问题。我正在使用R 3.4.3 x64窗口。

我希望该函数能够识别可能有两个重复值的峰值,我相信选项peakpat就是我能做到的。

这个问题已经asked before,但我还没有遇到过如何实施汉斯所指的选项的例子。这看起来非常基础,在编码时我也是一个初学者。在help file online中,它描述了关于peakpat的以下内容:

"将峰值定义为常规模式,例如默认模式``[+] 1,[ - ] 1,'&#39 ;;如果提供了模式,则不考虑参数nups和ndowns。"

我在解释什么是" [+] 1,[ - ] 1"手段。有任何想法吗?我已尝试过我认为这意味着的变体,但每次尝试都会导致NULL。请参阅下面的示例,非常感谢任何帮助/见解。

    # Example:
 install.packages("pracma")
 library(pracma) 
 subset = c(570,584,500,310,261,265,272,313,314,315,330,360,410,410,360,365,368,391,390,414)


# Plots
 plot(subset)
 lines(subset)


# findpeaks without defining repeated values; 
# the result does not identify the peak at subset[13:14] (repeated 'peak' values)
result  = findpeaks(subset)
pks1    = data.matrix(result[,1])
locs1   = data.matrix(result[,2])


# findpeaks with my futile attempt at defining peakpat
result  = findpeaks(subset, nups=2, ndowns=nups, zero = "0", peakpat="[+]2,[-]2,")
result  = findpeaks(subset, nups=1, ndowns=nups, zero = "0", peakpat="[+]1,[-]1,")
result  = findpeaks(subset, nups=1, ndowns=nups, zero = "0", peakpat="[+]{,1},[-]{,1}")
result  = findpeaks(subset, nups=1, ndowns=nups, zero = "0", peakpat="[+]{1,},[-]{1,}")
result  = findpeaks(subset, nups=2, ndowns=nups, zero = "0", peakpat="[2],[2]")
result  = findpeaks(subset, nups=2, ndowns=nups, zero = "0", peakpat="[1],[1]")

# all of the above results in NULL

谢谢!

2 个答案:

答案 0 :(得分:1)

在这种情况下,文档不太有用,但您可以通过检查函数体来获得一些线索。

在控制台中键入功能名称可让您检查其来源。没有详细说明,这一行很有帮助:

peakpat <- sprintf("[+]{%d,}[-]{%d,}", nups, ndowns)

这向我们显示默认参数对应于"[+]{1,}[-]{1,}"的peakpat。

这也应该强调为什么如果您指定peakpat,则无需为nupsndowns指定任何内容。

执行你所追求的模式,对于两个重复值的峰值:

result <- findpeaks(subset, peakpat = "[+]{1,}[0]{1,}[-]{1,}")

逗号指定间隔。因此,如果您希望将搜索限制为重复值最多为3的峰值:

result <- findpeaks(subset, peakpat = "[+]{1,}[0]{1,2}[-]{1,}")

该函数的工作原理是将数据转换为字符串并应用正则表达式,因此应该使用正则表达式的常用规则。

答案 1 :(得分:0)

这是我使用传统的peakpat定义找到峰值并定义持续峰值的最终解决方案(Callum的答案):

# EXAMPLE: findpeaks

    library(pracma) 
     subset = c(570,584,500,310,261,265,272,313,314,315,330,360,410,410,360,365,368,391,390,414)

    # Plots
     plot(subset)
     lines(subset)

    # findpeaks without sustained peaks:
    # the result does not identify the peak at subset[13:14] (repeated 'peak' values)
    result  = findpeaks(subset)
    pks1    = result[,1]
    locs1   = result[,2]

    # findpeaks by defining sustained peaks (2 or more consecutive values):
    result  = findpeaks(subset, peakpat = "[+]{1,}[0]{1,}[-]{1,}") 
    pks2    = result[,1]
    locs2   = result[,2]