Function that returns a subset of an R object with observations corresponding to local maxima.
Usage
peaks(
x,
span,
global.threshold,
local.threshold,
local.reference,
threshold.range,
strict,
na.rm,
...
)
# Default S3 method
peaks(
x,
span = NA,
global.threshold = NA,
local.threshold = NA,
local.reference = NA,
threshold.range = NA,
strict = NA,
na.rm = FALSE,
...
)
# S3 method for class 'numeric'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
...
)
# S3 method for class 'data.frame'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
x.var.name = NULL,
y.var.name = NULL,
var.name = y.var.name,
refine.wl = FALSE,
method = "spline",
...
)
# S3 method for class 'generic_spct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
var.name = NULL,
refine.wl = FALSE,
method = "spline",
...
)
# S3 method for class 'source_spct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
unit.out = getOption("photobiology.radiation.unit", default = "energy"),
refine.wl = FALSE,
method = "spline",
...
)
# S3 method for class 'response_spct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
unit.out = getOption("photobiology.radiation.unit", default = "energy"),
refine.wl = FALSE,
method = "spline",
...
)
# S3 method for class 'filter_spct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
filter.qty = getOption("photobiology.filter.qty", default = "transmittance"),
refine.wl = FALSE,
method = "spline",
...
)
# S3 method for class 'reflector_spct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
refine.wl = FALSE,
method = "spline",
...
)
# S3 method for class 'solute_spct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
refine.wl = FALSE,
method = "spline",
...
)
# S3 method for class 'cps_spct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
var.name = "cps",
refine.wl = FALSE,
method = "spline",
...
)
# S3 method for class 'raw_spct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
var.name = "counts",
refine.wl = FALSE,
method = "spline",
...
)
# S3 method for class 'generic_mspct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
var.name = NULL,
refine.wl = FALSE,
method = "spline",
...,
.parallel = FALSE,
.paropts = NULL
)
# S3 method for class 'source_mspct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
unit.out = getOption("photobiology.radiation.unit", default = "energy"),
refine.wl = FALSE,
method = "spline",
...,
.parallel = FALSE,
.paropts = NULL
)
# S3 method for class 'response_mspct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
unit.out = getOption("photobiology.radiation.unit", default = "energy"),
refine.wl = FALSE,
method = "spline",
...,
.parallel = FALSE,
.paropts = NULL
)
# S3 method for class 'filter_mspct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
filter.qty = getOption("photobiology.filter.qty", default = "transmittance"),
refine.wl = FALSE,
method = "spline",
...,
.parallel = FALSE,
.paropts = NULL
)
# S3 method for class 'reflector_mspct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
refine.wl = FALSE,
method = "spline",
...,
.parallel = FALSE,
.paropts = NULL
)
# S3 method for class 'solute_mspct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
refine.wl = FALSE,
method = "spline",
...,
.parallel = FALSE,
.paropts = NULL
)
# S3 method for class 'cps_mspct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
var.name = "cps",
refine.wl = FALSE,
method = "spline",
...,
.parallel = FALSE,
.paropts = NULL
)
# S3 method for class 'raw_mspct'
peaks(
x,
span = 5,
global.threshold = NULL,
local.threshold = NULL,
local.reference = "median",
threshold.range = NULL,
strict = FALSE,
na.rm = FALSE,
var.name = "counts",
refine.wl = FALSE,
method = "spline",
...,
.parallel = FALSE,
.paropts = NULL
)Arguments
- x
numeric vector.
- 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.- 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.- 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.- ...
ignored
- var.name, x.var.name, y.var.name
character Name of column where to look for peaks.
- refine.wl
logical Flag indicating if peak location should be refined by fitting a function.
- method
character String with the name of a method. Currently only spline interpolation is implemented.
- unit.out
character One of "energy" or "photon"
- filter.qty
character One of "transmittance" or "absorbance"
- .parallel
if TRUE, apply function in parallel, using parallel backend provided by foreach
- .paropts
a list of additional options passed into the foreach function when parallel computation is enabled. This is important if (for example) your code relies on external data or packages: use the .export and .packages arguments to supply them so that all cluster nodes have the correct environment set up for computing.
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_peaks(),
find_spikes(),
get_peaks(),
replace_bad_pixs(),
spikes(),
valleys(),
wls_at_target()
Examples
# default span = 5
peaks(sun.spct)
#> Object: source_spct [86 x 2]
#> Wavelength range 281.84615-798 nm, step 0.9230769-30.84615 nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 86 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 282. 0
#> 2 283. 0
#> 3 284. 0
#> 4 285. 0
#> 5 286. 0
#> 6 286. 0
#> 7 287. 0
#> 8 288. 0
#> 9 289. 0
#> 10 290. 0
#> # ℹ 76 more rows
# global maximum
peaks(sun.spct, span = NULL)
#> Object: source_spct [1 x 2]
#> Wavelength range 451-451 nm, step NA nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 1 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 451 0.820
peaks(sun.spct, span = NULL)$w.length
#> [1] 451
# fitted peak wavelength
peaks(sun.spct, span = NULL, refine.wl = TRUE)
#> Object: source_spct [1 x 2]
#> Wavelength range 451.02685-451.02685 nm, step NA nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 1 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 451. 0.821
peaks(sun.spct, span = NULL, refine.wl = TRUE)$w.length
#> [1] 451.0269
# a wider window
peaks(sun.spct, span = 51)
#> Object: source_spct [3 x 2]
#> Wavelength range 451-747 nm, step 44-252 nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 3 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 451 0.820
#> 2 495 0.790
#> 3 747 0.503
# global threshold relative to the range of s.e.irrad values
peaks(sun.spct, global.threshold = 0.7)
#> Object: source_spct [44 x 2]
#> Wavelength range 402-674 nm, step 3-17 nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 44 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 402 0.650
#> 2 409 0.662
#> 3 412 0.674
#> 4 416 0.676
#> 5 421 0.670
#> 6 426 0.639
#> 7 436 0.734
#> 8 442 0.719
#> 9 451 0.820
#> 10 457 0.798
#> # ℹ 34 more rows
peaks(sun.spct, global.threshold = -0.3)
#> Object: source_spct [11 x 2]
#> Wavelength range 281.84615-321 nm, step 0.9230769-30.84615 nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 11 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 282. 0
#> 2 283. 0
#> 3 284. 0
#> 4 285. 0
#> 5 286. 0
#> 6 286. 0
#> 7 287. 0
#> 8 288. 0
#> 9 289. 0
#> 10 290. 0
#> 11 321 0.182
# global threshold in actual s.e.irrad values
peaks(sun.spct, global.threshold = 0.7, threshold.range = c(0, 1))
#> Object: source_spct [22 x 2]
#> Wavelength range 436-555 nm, step 3-11 nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 22 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 436 0.734
#> 2 442 0.719
#> 3 451 0.820
#> 4 457 0.798
#> 5 461 0.771
#> 6 468 0.767
#> 7 472 0.769
#> 8 475 0.772
#> 9 478 0.787
#> 10 482 0.783
#> # ℹ 12 more rows
# local threshold relative to the range of s.e.irrad values
peaks(sun.spct, local.threshold = 0.1)
#> Object: source_spct [2 x 2]
#> Wavelength range 378-395 nm, step 17 nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 2 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 378 0.497
#> 2 395 0.470
# local threshold in actual s.e.irrad values
peaks(sun.spct, local.threshold = 0.1, threshold.range = c(0, 1))
#> Object: source_spct [1 x 2]
#> Wavelength range 395-395 nm, step NA nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 1 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 395 0.470
# local threshold relative to the range of s.e.irrad values, using window
# median instead of window minimum
peaks(sun.spct, local.threshold = 0.05, local.reference = "median")
#> Object: source_spct [9 x 2]
#> Wavelength range 360-525 nm, step 4-41 nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 9 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 360 0.371
#> 2 366 0.449
#> 3 378 0.497
#> 4 391 0.482
#> 5 395 0.470
#> 6 436 0.734
#> 7 451 0.820
#> 8 490 0.773
#> 9 525 0.738
# minimum, the default.
peaks(sun.spct, local.threshold = 0.05, local.reference = "farthest")
#> Object: source_spct [34 x 2]
#> Wavelength range 330-734 nm, step 3-186 nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 34 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 330 0.330
#> 2 335 0.313
#> 3 340 0.335
#> 4 343 0.338
#> 5 350 0.345
#> 6 354 0.376
#> 7 360 0.371
#> 8 366 0.449
#> 9 370 0.439
#> 10 378 0.497
#> # ℹ 24 more rows
peaks(sun.spct)
#> Object: source_spct [86 x 2]
#> Wavelength range 281.84615-798 nm, step 0.9230769-30.84615 nm
#> Label: sunlight, simulated
#> Measured on 2010-06-22 09:51:00 UTC
#> Measured at 60.20911 N, 24.96474 E; Kumpula, Helsinki, FI
#> Variables:
#> w.length: Wavelength [nm]
#> s.e.irrad: Spectral energy irradiance [W m-2 nm-1]
#> --
#> # A tibble: 86 × 2
#> w.length s.e.irrad
#> <dbl> <dbl>
#> 1 282. 0
#> 2 283. 0
#> 3 284. 0
#> 4 285. 0
#> 5 286. 0
#> 6 286. 0
#> 7 287. 0
#> 8 288. 0
#> 9 289. 0
#> 10 290. 0
#> # ℹ 76 more rows
