R/nmr_baseline_threshold.R

Defines functions nmr_baseline_threshold

Documented in nmr_baseline_threshold

#' Threshold estimation for peak detection
#'
#' Estimates the threshold value for peak detection on an [nmr_dataset_1D] object.
#'    This is performed computing the mean and the standard deviation of each spectrum
#'    beyond 9.5 ppm. The threshold is then averaged of means and adding 3 times the
#'    mean of the standard deviations
#'
#' @family peak detection functions
#' @family nmr_dataset_1D functions
#' @param nmr_dataset An [nmr_dataset_1D].
#' @return Numerical. A threshold value in intensity below that no peak is detected.
#' @export
#' @examples 
#' dir_to_demo_dataset <- system.file("dataset-demo", package = "AlpsNMR")
#' dataset <- nmr_read_samples_dir(dir_to_demo_dataset)
#' dataset_1D <- nmr_interpolate_1D(dataset, axis = c(min = -0.5, max = 10, by = 2.3E-4))
#' bl_threshold <- nmr_baseline_threshold(dataset_1D)
#' 

nmr_baseline_threshold <- function(nmr_dataset) {
    range_noise_ppm = c(9.5, 10)
    threshold_ind = which((nmr_dataset$axis > range_noise_ppm[1]) |
                                                    (nmr_dataset$axis < range_noise_ppm[2]))
    cent <- mean(apply(nmr_dataset$data_1r[, threshold_ind], 2, mean))
    disp <-
        3 * mean(apply(nmr_dataset$data_1r[, threshold_ind], 2, stats::sd))
    baselineThresh <- cent + disp
    baselineThresh
}

Try the AlpsNMR package in your browser

Any scripts or data that you put into this service are public.

AlpsNMR documentation built on April 1, 2021, 6:02 p.m.