NMath User's Guide

TOC | Previous | Next | Index

11.2 Computing Discrete Wavelet Transforms (.NET, C#, CSharp, VB, Visual Basic, F#)

As with Fourier analysis (Chapter 10), there are three basic steps to filtering signals using wavelets:

Decompose the signal using the DWT.

Filter the signal in the wavelet space using thresholding.

Invert the filtered signal to reconstruct the original, now filtered signal, using the inverse DWT.

The filtering of signals using wavelets is based on the idea that as the DWT decomposes the signal into details and approximation parts, at some scale the details contain mostly insignificant noise and can be removed or zeroed out using thresholding without affecting the signal.

In NMath, classes FloatDWT and DoubleDWT perform discrete wavelet transforms. Both derive from the DiscreteWaveletTransform abstract base class. DWT classes support both single step forward and reverse DWTs and multilevel signal deconstruction and reconstruction.

Instances of DWT types are constructed from signal data and a wavelet instance (Section 11.1). For example:

Code Example – C# DWT

var data = new DoubleVector( 26, new RandGenNormal( 1.0, 1.0 ) );
var wavelet = new DoubleWavelet( Wavelet.Wavelets.D2 );
var dwt = new DoubleDWT( data.DataBlock.Data, wavelet );

Code Example – VB DWT

Dim Data As New DoubleVector(26, New RandGenNormal(1.0, 1.0))
Dim WaveletInstance As New DoubleWavelet(Wavelet.Wavelets.D2)
Dim DWT As New DoubleDWT(Data.DataBlock.Data, waveletInstance)

An edge management mode can also be specified using values from the DiscreteWaveletTransform.WaveletMode enum. The default value is WaveletMode.PeriodicPadding.

Single Step DWT

For convenience, DWT classes provide DWT() and IDWT() methods for performing single-step forward and reverse DWTs. For example, this code performs a single-step deconstruction and reconstruction.

Code Example – C# Single-Step DWT

// Decompose signal with DWT
double[] approx;
double[] details;
dwt.DWT( data.DataBlock.Data, out approx, out details );

// Rebuild the signal
double[] signal = dwt.IDWT( approx, details );

Code Example – VB Single-Step DWT

' Decompose signal with DWT
Dim Approx() As Double
Dim Details() As Double
DWT.DWT(Data.DataBlock.Data, Approx, Details)

' Rebuild the signal
Dim Signal As Double() = DWT.IDWT(Approx, Details)

Multilevel DWT

The Decompose() method performs a multilevel discrete wavelet decomposition at a specified level. For instance:

Code Example – C# Multilevel DWT

dwt.Decompose( 5 );

Code Example – VB Multilevel DWT


MaximumDecompLevel() provides the maximum number of DWT decompositions possible based on the signal and wavelet lengths. CurrentDecompLevel() provides the current maximum level to which this signal has been decomposed.

The Reconstruct() method performs a multilevel discrete wavelet reconstruction at a specified level. A signal decomposition must be first completed. If no level is specified, a complete reconstruction is performed. For example, this code rebuilds the signal to level 2:

Code Example – C# Multilevel DWT

double[] reconstructedData2 = dwt.Reconstruct( 2 );

Code Example – VB Multilevel DWT

Dim ReconstructedData2() As Double = DWT.Reconstruct(2)

This code rebuilds the signal to level 1—the original (filtered) signal.

Code Example – C# Multilevel DWT

double[] reconstructedData1 = dwt.Reconstruct();

Code Example – VB Multilevel DWT

Dim ReconstructedData1() As Double = DWT.Reconstruct()

Accessing the Coefficients

After a signal decomposition is completed, the coefficient vectors can be accessed. The WaveletCoefficients() method takes the wavelet coefficient type, either details or approximation, and the detail level desired, starting with level 1 and continuing to the maximum level of decomposition completed (similar to MATLAB's wrcoef function). Depending on the length of the wavelet and signal vector the approximations may have an extra element at the end of the vector due to the IDWT.

Code Example – C# Wavelet Coefficients

var approx = dwt.WaveletCoefficients( 
  DiscreteWaveletTransform.WaveletCoefficientType.Details, 2 );

Code Example – VB Wavelet Coefficients

Dim Approx() As Double = DWT.WaveletCoefficients(
  DiscreteWaveletTransform.WaveletCoefficientType.Details, 2)

Threshold Calculations

ComputeThreshold() finds a single threshold for a given thresholding method and decomposition level. Four different thresholding methods are supported: Universal, UniversalMAD, Sure, and Hybrid (also known as SureShrink).

For example, this code computes the Universal threshold at level 1:

Code Example – C# Wavelet Threshold Calculation

double lambdaU = dwt.ComputeThreshold( 
  DiscreteWaveletTransform.ThresholdMethod.Universal, 1 );

Code Example – VB Wavelet Threshold Calculation

Dim LambdaU As Double = DWT.ComputeThreshold(
  DiscreteWaveletTransform.ThresholdMethod.Universal, 1)


NMath supports details thresholding at any level.

ThresholdAllLevels() thresholds all levels of detail in the current signal decomposition. The method accepts a thresholding policy from the DiscreteWaveletTransform.ThresholdPolicy enum, and a vector of threshold values, with the first value applied to level 1, the second applied to level 2, and so on. The length of the threshold vector must be at least the depth of the current decomposition as indicated by CurrentDecompLevel().

For example, this code thresholds all detail levels using the same threshold with a Soft policy:

Code Example – C# Wavelet Thresholding

  new double[] { lambdaU, lambdaU, lambdaU, lambdaU, lambdaU } );

Code Example – VB Wavelet Thresholding

  New Double() {LambdaU, LambdaU, LambdaU, LambdaU, LambdaU})

ThresholdLevel() thresholds the specified details level in the current signal decomposition.