Re: [R] peak finding

From: Robert A LaBudde <ral_at_lcfltd.com>
Date: Tue, 25 Mar 2008 10:25:41 -0500

At 10:48 PM 3/24/2008, Chistos wrote:
>John,
>
>There is a peak finding algorithm attributed to Prof. Ripley in the R-help
>archive. It has a span parameter which might give you something close to
>what you seem to be looking for.
>
>http://finzi.psych.upenn.edu/R/Rhelp02a/archive/33097.html
>
>-Christos

Finding peaks and valleys has several issues:

  1. Impact of noise.
  2. Mathematical smoothness of the underlying signal.
  3. Boundary conditions at the beginning and end of the data series.
  4. Bias from smoothing.

If the noise is not too bad, I'd try fitting a smoothing spline, and then use the null derivative points to punctuate the extrema.

If the noise is severe, you probably will need some domain knowledge, and will end up with perhaps locally weighted regression, followed by extrema search.

For cases where the noise is trivial, the following short function might be useful in picking off peaks (and symmetrically modified, valleys):

#findpeaks()
#Copyright 2007 by Robert A LaBudde, all rights reserved
#find peaks in (x,y) curve

findpeaks<- function(x, y) {

   nx<- length(x)
   ny<- length(y)
   if (nx != ny) {

     print ('>> findpeaks01: x and y must be same length!')
     stop
     }
   ipeak<- NULL

   xpeak<- NULL
   ypeak<- NULL
   yprv<- y[1]
   for (i in 1:ny) {
     ynext<- ifelse(i==ny,y[ny],y[i+1])
     if(yprv < y[i] & y[i] > ynext) { #found local peak
       ipeak<- c(ipeak,i)
       xpeak<- c(xpeak,x[i])
       ypeak<- c(ypeak,y[i])
       }
     yprv<- y[i]
     }

   return(data.frame(ipeak,x=xpeak,y=ypeak))    }

Trivial though it may be, it actually works quite well for its intended purpose.



Robert A. LaBudde, PhD, PAS, Dpl. ACAFS e-mail: ral_at_lcfltd.com
Least Cost Formulations, Ltd.            URL: http://lcfltd.com/
824 Timberlake Drive                     Tel: 757-467-0954
Virginia Beach, VA 23464-3239            Fax: 757-467-2947

"Vere scire est per causas scire"



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. Received on Tue 25 Mar 2008 - 14:32:57 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 25 Mar 2008 - 15:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive