https://github.com/cran/simecol
Tip revision: 0c77049145e96bc6d5b981efbb4a3c871fa2c5a7 authored by Thomas Petzoldt on 29 February 2012, 00:00:00 UTC
version 0.8-2
version 0.8-2
Tip revision: 0c77049
peaks.R
## utility function to detect peaks in (deterministic or smoothed) time series
peaks <- function(x, y=NULL, mode="maxmin") {
if (!mode %in% c("max", "min", "maxmin")) stop("unknown mode:", mode)
xy <- xy.coords(x,y)
x <- xy$x
y <- xy$y
l <- length(y)
ym1 <- c(y[-1], y[l])
yp1 <- c(y[1], y[-l])
if (mode == "min") {
xx <- x[y < ym1 & y < yp1]
yy <- y[y < ym1 & y < yp1]
} else if (mode == "max") {
xx <- x[y > ym1 & y > yp1]
yy <- y[y > ym1 & y > yp1]
} else {
xx <- x[y > ym1 & y > yp1 | y < ym1 & y < yp1]
yy <- y[y > ym1 & y > yp1 | y < ym1 & y < yp1]
}
list(x=xx, y=yy)
}