R/smooth.R

Defines functions smooth

Documented in smooth

#' Low pass filter a time series
#'
#' This function is used to low pass filter (smooth) a regularly-sampled time series.
#' @param x The signal to be filtered. It can be multi-channel with a signal in each column, e.g., an acceleration matrix. The number of samples (i.e., the number of rows in x) must be larger than the filter length, n.
#' @param n The smoothing parameter - use a larger number to smooth more. n must be greater than 1. Signal components above 1/n of the Nyquist frequency are filtered out.
#' @return The input signal has the first and fifth harmonic. Applying the low-pass filter removes most of the fifth harmonic so the output appears as a sinewave except for the first few samples which are affected by the filter startup transient. Smooth uses fir_nodelay to perform the filtering and so introduces no delay.
#' @export
#' @examples
#' y1 <- sin((2 * pi * 0.05) %*% t(c(1:100))) + cos((2 * pi * 0.25) %*% t(c(1:100)))
#' x1 = c(1:length(y1))
#' plot(x = x1, y = y1)
#' y2 <- smooth(x1, n = 4)
#' x2 = c(1:length(y2))
#' plot(x = x2, y = y2)
#'
smooth <- function(x, n) {
  y <- vector(mode = "numeric", length = 0)
  # input checks-----------------------------------------------------------
  if (missing(n)) {
    stop("An input for the smoothing parameter, n, is required.")
  }
  nf <- 6 * n
  fp <- 1 / n
  y <- fir_nodelay(x, nf, fp)
  return(y)
}

Try the tagtools package in your browser

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

tagtools documentation built on June 28, 2024, 5:07 p.m.