R/wavelet_tansformation.R

Defines functions wavelet_tansformation

wavelet_tansformation = function(UnivariateData, Filter = "Haar",
                                 Boundary = "periodic", NWLevels = 5, Fast = TRUE){
  # DESCRIPTION
  # This function decomposes a time series in its wavelet and smooth
  # coefficients using the redundant Haar wavelet transform.
  #
  # INPUT
  # UnivariateData[1:n]           Numerical vector with n time series values.
  #
  # OPTIONAL
  # Aggregation[1:Scales]         Numerical vector carrying numbers whose index
  #                               is associated with the wavelet level. The
  #                               numbers indicate the number of values used for
  #                               aggregation from the original time series.
  #
  # OUTPUT
  # UnivariateData[1:n]               Numerical vector with n time series values.
  # WaveletCoefficients[Scales, n]    Numerical matrix with wavelet coefficients.
  # SmoothCoefficients[Scales, n]     Numerical matrix with smooth coefficients.
  # Scales                            Number of wavelet levels.
  #
  # DETAILS
  #
  #
  # Author: QS, 02/2021
  if(!is.vector(UnivariateData)){
    message("Data must be of type vector!")
    return()
  }

  NumObs = length(UnivariateData)

  if(Filter == "Haar"){
    L = 2
    g = c(0.5, 0.5)
  }else if (Filter == "Ricker"){
    L = 3
    g = c(0.1, 0.4, 0.4, 0.1)
  }else{
    stop("Please select a wavelet filter.")
  }
  g         = g/sum(g)
  h         = wt.filter.qmf(g, inverse = TRUE)
  wt.filter = new("wt.filter", L = L, h = h, g = g,
                   wt.class = "Morlet", wt.name = "Ricker", transform = "modwt")
  tmpRes    = modwt(UnivariateData, filter = wt.filter, n.levels = NWLevels,
                    boundary = Boundary, fast = Fast)

  WaveletCoefficients = matrix(0, nrow = NWLevels, ncol = NumObs)
  for(i in 1:NWLevels){
    WaveletCoefficients[i,] = tmpRes@W[[i]]
  }
  SmoothCoefficients = matrix(0, nrow = NWLevels, ncol = NumObs)
  for(i in 1:NWLevels){
    SmoothCoefficients[i,] = tmpRes@V[[i]]
  }

  return(list("UnivariateData"      = UnivariateData,
              "WaveletCoefficients" = WaveletCoefficients,
              "SmoothCoefficients"  = SmoothCoefficients,
              "Boundary"            = Boundary,
              "Scales"              = NWLevels))
}

Try the mrf package in your browser

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

mrf documentation built on April 17, 2026, 1:07 a.m.