find_peaks: Find peak(s) in a vector

find_peaksR Documentation

Find peak(s) in a vector

Description

A simple algorithm to find local maxima/minima in sequential data

Usage

find_peaks(x, m = 3, order = "d", na.rm = TRUE)

Arguments

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

Details

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.

Author(s)

stas g

Examples

    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)
}

freysimon/TigeR documentation built on April 18, 2024, 4:57 p.m.