aphalo / photobiology

Package ‘photobiology’ defines a system of classes for storing spectral data and accompanying methods and operators. This is the core of a suite of R packages for photobiological calculations.
4 stars 1 forks source link

HMFW() a function to compute the "half-maximum full-width" of peaks in spectra #36

Open aphalo opened 5 days ago

aphalo commented 5 days ago

This function is rather easy to implement with existing functions. A first cut at the code for a single peak is below. This could be made an S3 method for generic_spct but probably not needed, while support for multiple peaks in a single spectrum much more important.

HMFW <-
  function(
    x,
    span = NULL,
    ignore_threshold = 0,
    strict = TRUE,
    na.rm = FALSE,
    refine.wl = FALSE,
    method = "spline",
    ...) {
    stopifnot(is.any_spct(x))
    peaks.found <- peaks(x = x,
                         ignore_threshold = ignore_threshold,
                         span = span, 
                         strict = strict,
                         na.rm = na.rm)
    if (nrow(peaks.found) == 0) {
      return(NA_real_)
    } else if (nrow(peaks.found) == 1) {
      wavelengths <- wls_at_target(x = x,
                                   target = "0.5max",
                                   interpolate = refine.wl,
                                   na.rm = na.rm)[["w.length"]]
      if (length(wavelengths) == 2) {
        return(diff(wavelengths))
      } else {
        return(NA_real_)
      }
    } else {
      message("Not implemented yet for multiple peaks")
    }
  }