Computes the discrete wavelet transform (DWT) of spectra and returns its coefficients as features: a compressed representation of the spectra for modeling, with far fewer variables than channels.
Arguments
- x
A numeric matrix or data frame of spectra, one per row, or a numeric vector (one spectrum). Their names are the wavelengths.
- wavelet
The wavelet:
"haar","d4"(default),"d6","d8"or"la8".- level
The number of levels of the decomposition. Default is 3.
- coefficients
The coefficients returned:
"approximation"(default) or"all".
Value
A matrix with one row per spectrum and one column per coefficient,
named A<level>_<i> for the approximation and D<j>_<i> for the
details of level j. When the names of x are wavelengths, its
attribute "wavelength" gives the wavelength at the center of each
coefficient.
Details
The DWT decomposes a spectrum, level by level, into approximation coefficients (the smooth part, at half the resolution of the previous level) and detail coefficients (the fine structure removed at that level), with the pyramid algorithm of Mallat (1989) and an orthonormal wavelet:
"haar": the Haar wavelet (2 coefficients);"d4","d6","d8": Daubechies' extremal phase wavelets with 4, 6 and 8 coefficients;"la8": Daubechies' least asymmetric wavelet with 8 coefficients (symlet 4), whose nearly symmetric shape suits spectral lines.
The coefficients kept are
"approximation"(default): the approximation atlevel, a smoothed version of the spectrum with about \(2^{-level}\) times as many values;"all": the approximation atleveland the details of every level, the complete transform (as many values as channels, after padding).
The spectrum is extended by reflection at its end to a length divisible
by \(2^{level}\), and the transform is periodic. The transform is
orthonormal, so the sum of squares of all the coefficients equals that of
the (padded) spectrum. As features for a model, the approximation keeps
the broad shape of the spectrum and drops the noise; to keep only the
most informative coefficients, step_wavelet() can select those with the
largest variance in the training data (Trygg and Wold, 1998).
Each coefficient is a weighted sum of a window of channels, which widens
with the level, and is placed at the center of that window: the centroid
of the weights for the approximation, their center of energy for the
details. The window of a coefficient is not centered on its block of
\(2^{level}\) channels (about 37 channels later for "la8" at level
4). When the names of the channels are wavelengths, the wavelengths at
these centers are returned as the attribute "wavelength", so that the
approximation can be plotted against the wavelength and the coefficients
kept by step_wavelet() traced back to the spectrum. The approximation at
level is \(2^{level/2}\) times the local mean of the spectrum. The last
coefficients, whose window extends past the last channel (the padding,
then the start of the spectrum), are placed past the end of the
wavelength axis, extended with the spacing of its last channels.
References
Mallat, S.G. (1989). A theory for multiresolution signal decomposition: the wavelet representation. IEEE Transactions on Pattern Analysis and Machine Intelligence, 11(7):674-693.
Daubechies, I. (1992). Ten Lectures on Wavelets. SIAM, Philadelphia.
Trygg, J., Wold, S. (1998). PLS regression on wavelet compressed NIR spectra. Chemometrics and Intelligent Laboratory Systems, 42(1-2):209-220.
Examples
data(forageLIBS)
spectra <- forageLIBS[1:4, -(1:14)]
approx <- wavelet_features(spectra, wavelet = "la8", level = 4)
dim(approx) # 7152 channels -> 447 coefficients
#> [1] 4 447
head(attr(approx, "wavelength")) # the wavelength of each coefficient
#> [1] 203.1792 204.5673 205.8950 207.2589 208.6327 209.9410
