find_peaks | R Documentation |
A simple algorithm to find local maxima/minima in sequential data
find_peaks(x, m = 3, order = "d", na.rm = TRUE)
x |
a numerical vector |
m |
integer. How many points on either side must be smaller than the peak. |
order |
character. Order data along their data values. Accepts 'i' and 'd' for increasing / decreasing, respectively, and 'n' for no order. |
na.rm |
logical. Should na values be removed before the calculation |
The function takes an ordered sequence (vector) of values x and a number m and returns a vector of indices of local peaks in x. A (local) peak is defined as a point such that m points either side of it has a lower or equal value to it.
Thus, m can be used adjust the sensitivity of the peak detection procedure: larger m will result in fewer peaks, whilst smaller values of m will result in more peaks found.
stas g
set.seed(321)
w <- abs(rnorm(1000))
w[sample(1 : 1000, 25)] <- rpois(25, 5)
w[sample(1 : 1000, 25)] <- rpois(25, 10)
par(mfrow = c(2, 2))
for(k in c(10, 20, 50, 250)){
p <- find_peaks(w, m = k)
ind <- rep(1, length(w))
ind[p] <- 2
plot(w, type = 'l', main = paste0('m = ', k))
points(p, w[p], col = 'red', pch = 19)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.