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

问题描述 投票:1回答:2

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

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

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

“将峰值定义为常规模式,例如默认模式``[+] 1,[ - ] 1,'';如果提供模式,则不考虑参数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

谢谢!

r
2个回答
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,}")

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


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]
© www.soinside.com 2019 - 2024. All rights reserved.