https://github.com/cran/simecol
Tip revision: 554acdb1a658be1164358afb7001c5c50ce6dfed authored by Thomas Petzoldt on 28 November 2008, 00:00:00 UTC
version 0.6-1
version 0.6-1
Tip revision: 554acdb
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)
}