library(rwavelet) knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "inst/doc/readme_img/" )
Wavelet Analysis
Install the devtools package if you haven't already.
install.packages("devtools")
To install the development package, type the following at the R command line:
devtools::install_github("fabnavarro/rwavelet") library(rwavelet)
To install the CRAN version of the package, type the following:
install.packages("rwavelet")
To obtain the complete list of package functions, simply type
help(package = "rwavelet")
Here is an example of denoising of an experimental nuclear magnetic resonance (NMR) spectrum. We start by loading the data:
data("RaphNMR") Y <- RaphNMR n <- length(Y) t <- seq(0, 1, length = n)
Then we specify the coarse decomposition scale $j_0$, the wavelets we want to use (here, Symmlet with 6 vanishing moments) and we perform a fast wavelet transform to get the noisy wavelet coefficients (Ywd):
j0 <- 0 J <- log2(n) qmf <- MakeONFilter('Symmlet', 6) Ywd <- FWT_PO(Y, j0, qmf) Ywnoise <- Ywd
We estimate $\sigma$ the standard deviation of the noise using the maximum absolute deviation (with only the finest scale coefficients). We apply a hard thresholding rule (with a universal threshold) to the coefficient estimators and obtain the estimator by applying an inverse transform:
hatsigma <- MAD(Ywd[(2^(J-1)+1):2^J]) lambda <- sqrt(2*log(n))*hatsigma Ywd[(2^(j0)+1):n] <- HardThresh(Ywd[(2^(j0)+1):n], lambda) fhat <- IWT_PO(Ywd, j0, qmf)
Finally, we plot the resulting estimator:
par(mfrow=c(2,2), mgp = c(1.2, 0.5, 0), tcl = -0.2, mar = .1 + c(2.5,2.5,1,1), oma = c(0,0,0,0)) plot(t,Y,xlab="", ylab="", main="Observations") plot(t,Y,xlab="", ylab="", main="Observations and Estimator") matlines(t, fhat, lwd=2, col="blue", lty=1) plot(Ywnoise, ylim=c(-20, 20), xlab="", ylab="", main = "Noisy Coefficients") matlines(rep(lambda, n), lwd=2,col="red",lty=1) matlines(-rep(lambda, n), lwd=2,col="red",lty=1) plot(Ywd, ylim=c(-20,20), xlab="", ylab="", main = "Estimated Coefficients")
See the package vignette or documentation for more details. You could also build and see the vignette associated with the package using the following lines of code
devtools::install_github("fabnavarro/rwavelet", build_vignettes = TRUE) library(rwavelet)
Then, to view the vignette
vignette("rwaveletvignette")
citation("rwavelet")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.