R/Util.R

Defines functions randomSignal

Documented in randomSignal

library(tuneR)

#' Generate a random signal
#'
#' @description
#' This function generate a random signal using the noise function of the
#' `tuneR` package.
#'
#' @param n the length of the signal to generate
#' @param model the type of signal to generate
#' @param alpha The power for the power law noise model
#'  (defaults are 1 for pink and 1.5 for red noise) \eqn{(1/f)^alpha}.
#'
#' @details
#' Signal generated by these function can be built using different models. By
#' default this is a \emph{white noise}, but one can also used
#' \emph{pink, red or power} models.
#'
#' @return a vector
#'
#' @export
#' @importFrom tuneR noise
#'
#' @examples
#' x <- randomSignal(1000)
#' \donttest{plot(x,type='l')}
randomSignal <- function(n, model="white", alpha=1) {

  # forge the signal
  if (model == "power")
    x <- noise(kind = model, duration = n, alpha = alpha, stereo = FALSE)
  else
    x <- noise(kind = model, duration = n, stereo = FALSE)
  x <- x@left

  # eop
  return(x)
}
jcaude/MSEntropy documentation built on May 21, 2021, 7:31 p.m.