R/reflectSignal.R

Defines functions reflectSignal

Documented in reflectSignal

#' Reflects the signal and extends it to a power of 2 if necessary
#'
#' Reflecting the signal in wavelet applications allows the signal to be periodic and usually lessens boundary bias. If the original signal 
#' has a length which is a power of 2, this function adds a reflection of the original signal to itself. Otherwise this function extends
#' the original signal to have length of a power of 2 by reflecting an appropriate number of observation points, and then reflecting the entire modified signal.
#'  
#' @param x an \code{m} by \code{n} matrix of signals
#' @return an \code{n}-vector containing the indices of the smoothed output corresponding to the original signal \code{x}. Also substitutes the original signal with the reflected one to be used as input.
reflectSignal <- function(x){
  n = dim(x)[2]
  J = log2(n)
  if((J%%1)==0){#if J is an integer, i.e. n is a power of 2
    eval.parent(substitute(x<-cbind(x,x[,n:1])))
    return(1:n)
  }else{
    n.ext=2^ceiling(J)
    lnum=round((n.ext-n)/2)
    rnum=n.ext-n-lnum
    if(lnum==0){
      x.lmir=NULL
    }else{
      if (dim(x)[1]==1){
        x.lmir=x[lnum:1]
      }else{
        x.lmir=x[,lnum:1]
      }
    }
    if(rnum==0){
      x.rmir=NULL
    }else{
      if (dim(x)[1]==1){
        x.rmir=x[n:(n-rnum+1)]
      }else{
        x.rmir=x[,n:(n-rnum+1)]
      }
    }
    if (dim(x)[1]==1){
      x.ini=c(x.lmir,x,x.rmir)
      x.mir=x.ini[n.ext:1]
      eval.parent(substitute(x<-c(x.ini,x.mir))) 
    }else{
      x.ini=cbind(x.lmir,x,x.rmir)
      x.mir=x.ini[,n.ext:1]
      eval.parent(substitute(x<-cbind(x.ini,x.mir))) 
    }
    return((lnum+1):(lnum+n))
  }
}
shimlab/HMTree documentation built on May 29, 2019, 9:25 p.m.