# Teresa updated May 25th 2018
#' @importFrom assertthat assert_that
#' @importFrom magrittr %>% subtract add
#' @importFrom purrr as_vector discard partial reduce
#'
library("magrittr")
library("purrr")
library("assertthat")
first_min_ <- function(signal_slice) {
x <-
abs(signal_slice)
is_lower_than_before <-
x[-c(1, length(x))] <= x[-c(length(x) - 1, length(x))]
is_lower_than_after <-
x[-c(1, length(x))] <= x[-c(1, 2)]
localmin <-
is_lower_than_before & is_lower_than_after
index <-
which(localmin != 0)[1]
}
#' Search for the onset of a wave using the derivative method
#'
#' TODO: Add details, perhaps better description
#'
#' @param peak_onset An integer. The index of the peak
#' where the onset will be searched around.
#' @param signal_slice A vector of wavelet signal slice
#' which is a search window for wave onset searching.
#' @param K A constant for defining amplitude threshold.
#'
#' @export
#'
wavelet_onset_search <- function(peak_onset, signal_slice, K) {
amp_peak_onset <-
signal_slice[length(signal_slice)] %>%
abs()
# abs(tail(signal_slice, 1))
threshold <-
amp_peak_onset * K
slice_inverse <-
signal_slice[(length(signal_slice) - 1):1]
index1 <-
which(abs(slice_inverse) < threshold)[1]
index2 <-
first_min_(slice_inverse)
onset <-
if ((is.na(index1)) & (is.na(index2))) {
peak_onset - length(signal_slice) + 1
} else if (is.na(index1)) {
peak_onset - index2
} else if (is.na(index2)) {
peak_onset - index1
} else {
peak_onset - min(index1, index2)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.