[R] Peak detector help!?

PIKAL Petr petr.pikal at precheza.cz
Fri Feb 8 10:22:15 CET 2013


Hi

I am not sure if it suits your needs but I use this function for identifying peaks in different spectral data.

peaks <- function (series, span = 3, ties.method = "first") 
{
    if ((span <- as.integer(span))%%2 != 1) 
        stop("'span' must be odd")
    z <- embed(series, span)
    s <- span%/%2
    v <- max.col(z, ties.method = ties.method) == 1 + s
    pad <- rep(FALSE, s)
    result <- c(pad, v, pad)
    result
  }

Works for both your examples.

Regards
Petr

test <- c(9,8,7,5,4,1,1,2,1,1,3,4,5,6,7,5,4,3,2,1,1,3,4,5,6,7,8)
plot(seq(length(test)),test)
peaks(test)
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
[13] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE
pp<-peaks(test)
abline(v=seq(length(test))[pp], col=2)

test <- c(rep(1,10),20,rep(1,10))
plot(seq(length(test)),test)
pp<-peaks(test)
abline(v=seq(length(test))[pp], col=2)


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Johannes Graumann
> Sent: Thursday, February 07, 2013 1:42 PM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] Peak detector help!?
> 
> Johannes Graumann wrote:
> 
> > Grrr ... new trial with code here: http://pastebin.com/RjHNNG9J Maybe
> > the amount of inline-code prevented posting?
> >
> > Hello,
> >
> > I am writing a simple peak detector and it works quite well ...
> > however there's one special case below, that I can't get my head
> > wrapped around ... the problem is in the "Deal with not fully
> > qualified peaks at the sequence extremes" section, but I cannot seem
> > to come up with a condition that would be met in the special case
> > below and base a fix on it ... mind completely poisoned with trial
> solutions that didn't work ...
> >
> > A fresh hint anyone?
> >
> > Sincerely, Joh
> 
> Sleep brought some insight.
> 
> The code here http://pastebin.com/UXzbzqp8 works for now - I have
> already quite a number of test cases that gave me problems and now
> don't ...
> probably more corner cases somewhere, but they will be dealt with as
> they show up.
> 
> Cheers, Joh
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list