An algorithm to find local extrema in a vector

May 3, 2009

(This article was first published on [R] tricks, and kindly contributed to R-bloggers)

I spend some time looking for an algorithm to find local extrema in a vector (time series). The solution I used is to “walk” through the vector by step larger than 1, in order to retain only one value even when the values are very noisy (see the picture at the end of the post).

It goes like this :

findpeaks <- function(vec,bw=1,x.coo=c(1:length(vec)))
	pos.x.max <- NULL
	pos.y.max <- NULL
	pos.x.min <- NULL
	pos.y.min <- NULL 	for(i in 1:(length(vec)-1)) 	{ 		if((i+1+bw)>length(vec)){
                sup.stop <- length(vec)}else{sup.stop <- i+1+bw
		if((i-bw)<1){inf.stop <- 1}else{inf.stop <- i-bw}
		subset.sup <- vec[(i+1):sup.stop]
		subset.inf <- vec[inf.stop:(i-1)]

		is.max   <- sum(subset.inf > vec[i]) == 0
		is.nomin <- sum(subset.sup > vec[i]) == 0

		no.max   <- sum(subset.inf > vec[i]) == length(subset.inf)
		no.nomin <- sum(subset.sup > vec[i]) == length(subset.sup)

		if(is.max & is.nomin){
			pos.x.max <- c(pos.x.max,x.coo[i])
			pos.y.max <- c(pos.y.max,vec[i])
		if(no.max & no.nomin){
			pos.x.min <- c(pos.x.min,x.coo[i])
			pos.y.min <- c(pos.y.min,vec[i])


To leave a comment for the author, please follow the link and comment on their blog: [R] tricks. offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.