R/find.peak.R

Defines functions find.peak

Documented in find.peak

#' find peak with curve fitting
#'
#' @param x
#' @param mode
#' @param config
#'
#' @return
#' @export
#'
#' @examples
find.peak <- function(x, mode = 'fit.smooth', config) {
    # Ver 3.4.1
    curve <- fit.curve(x, mode = mode, config)
    if (is.null(curve)) {
        cat(sprintf('Curve fit failed, no peak found!'))
        return(NULL)
    }

    # Ver 3.4.1
    curve.max <- curve[curve.max.p <- get.peak(curve)]
    curve.min <- curve[curve.min.p <- get.valley(curve)]

    if(is.nothing(curve.max)|is.nothing(curve.min)) return(NULL)
    return(c(curve.max - curve.min, curve.min.p, curve.max.p))
#     poi <- floor((curve.max.p + curve.min.p) / 2)
#     voi <- curve[poi]
#     if(is.na(poi)|is.na(voi)) return(NULL) # VER 3.3.2
#     if (curve[1] < voi) {
#         # Positive Peak
#         return(c(curve.max - curve.min, curve.max.p))
#     } else if (voi < curve[1]) {
#         # Negative Peak
#         return(c(curve.max - curve.min, curve.min.p))
#     } else {
#         warning('find.peak() UNKNOWN ERROR \n')
#         return(NULL)
#     }
}
yanxianUCSB/yxhelper documentation built on April 20, 2020, 4:09 p.m.