These functions find peaks (local maxima) and valleys (local minima) in a
numeric vector, using a user selectable span or window. Global and local
size thresholds based on different criteria make it possible restrict
the returned peaks to those more prominent. A logical vector is
returned.
Usage
find_peaks(
x,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
span = 3,
strict = FALSE,
na.rm = FALSE
)
find_valleys(
x,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
span = 3,
strict = FALSE,
na.rm = FALSE
)Arguments
- x
numeric vector.
- global.threshold
numeric A value belonging to class
"AsIs"is interpreted as an absolute minimum height or depth expressed in data units. A barenumericvalue (normally between 0.0 and 1.0), is interpreted as relative tothreshold.range. In both cases it sets a global height (depth) threshold below which peaks (valleys) are ignored. A bare negativenumericvalue indicates the global height (depth) threshold below which peaks (valleys) are be ignored. Ifglobal.threshold = NULL, no threshold is applied and all peaks returned.- local.threshold
numeric A value belonging to class
"AsIs"is interpreted as an absolute minimum height (depth) expressed in data units relative to a within-window computed reference value. A barenumericvalue (normally between 0.0 and 1.0), is interpreted as expressed in units relative tothreshold.range. In both caseslocal.thresholdsets a local height (depth) threshold below which peaks (valleys) are ignored. Iflocal.threshold = NULLor ifspanspans the whole ofx, no threshold is applied.- local.reference
character One of
"median","median.log","median.sqrt","farthest","farthest.log"or"farthest.sqrt". The reference used to assess the height of the peak, either the minimum/maximum value within the window or the median of all values in the window.- threshold.range
numeric vector If of length 2 or a longer vector
range(threshold.range)is used to scale both thresholds. WithNULL, the default,range(x)is used, and with a vector of length onerange(threshold.range, x)is used, i.e., the range is expanded.- span
odd positive integer A peak is defined as an element in a sequence which is greater than all other elements within a moving window of width
spancentred at that element. The default value is 5, meaning that a peak is taller than its four nearest neighbours.span = NULLextends the span to the whole length ofx.- strict
logical flag: if
TRUE, an element must be strictly greater than all other values in its window to be considered a peak.- na.rm
logical indicating whether
NAvalues should be stripped before searching for peaks.
Value
A vector of logical values of the same length as x. Values
that are TRUE correspond to local peaks in vector x and can be used
to extract the rows corresponding to peaks from a data frame.
Details
As find_valleys,
peaks and valleys
call find_peaks to search for peaks and
valleys, this explanation applies to the four functions. It also applies to
stat_peaks and
stat_valleys. Function
find_peaks is a wrapper built onto function
peaks from splus2R, adds support for peak
height thresholds and handles span = NULL and non-finite (including
NA) values differently than splus2R::peaks. Instead of giving an
error when na.rm = FALSE and x contains NA values,
NA values are replaced with the smallest finite value in x.
span = NULL is treated as a special case and selects max(x).
Passing strict = TRUE ensures that non-unique global and within window
maxima are ignored, and can result in no peaks being returned.
Two tests make it possible to ignore irrelevant peaks. One test
(global.threshold) is based on the absolute height of the peaks and
can be used in all cases to ignore globally low peaks. A second test
(local.threshold) is available when the window defined by `span`
does not include all observations and can be used to ignore peaks that are
not locally prominent. In this second approach the height of each peak is
compared to a summary computed from other values within the window of width
equal to span where it was found. In this second case, the reference
value used within each window containing a peak is given by the argument
passed to local.reference. Parameter threshold.range
determines how the values passed as argument to global.threshold and
local.threshold are scaled. The default, NULL uses the range
of x. Thresholds for ignoring too small peaks are applied after
peaks are searched for, and threshold values can in some cases result in no
peaks being returned.
The local.threshold argument is used as is when
local.reference is "median" or "farthest", i.e., the
same distance between peak and reference is used as cut-off irrespective of
the value of the reference. In cases when the prominence of peaks is
positively correlated with the baseline, a local.threshold that
increases together with increasing computed within window median or
farthest value applies apply a less stringent height requirement in regions
with overall low height. In this case, natural logarithm or square root
weighting can be requested with local.reference arguments
"median.log", "farthest.log", "median.sqrt", and
"farthest.sqrt" as arguments for local.reference.
While functions find_peaks and find_valleys
accept as input a numeric vector and return a logical vector,
methods peaks and valleys accept as input
different R objects, including spectra and collections of spectra and
return a subset of the object. These methods are implemented using calls to
functions find_peaks, find_valleys and
fit_peaks.
Note
The default for parameter strict is FALSE in functions
find_peaks and find_valleys, while the default
in peaks is strict = TRUE.
See also
Other peaks and valleys functions:
find_spikes(),
get_peaks(),
peaks(),
replace_bad_pixs(),
spikes(),
valleys(),
wls_at_target()
Examples
with(sun.data, which(find_peaks(s.e.irrad, span = NULL)))
#> [1] 159
with(sun.data, which(find_peaks(s.e.irrad, span = 51)))
#> [1] 159 203 455
with(sun.data, w.length[find_peaks(s.e.irrad, span = 51)])
#> [1] 451 495 747
with(sun.data, sum(find_peaks(s.e.irrad, span = NULL, strict = TRUE)))
#> [1] 1
with(sun.data, which(find_valleys(s.e.irrad, span = NULL)))
#> [1] 1
with(sun.data, which(find_valleys(s.e.irrad, span = 51)))
#> [1] 66 101 139 195 225 297 364 427 469
