VB DWT Example

← All NMath Code Examples

 

Imports System
Imports System.Globalization
Imports System.Threading
Imports System.Text

Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Analysis

Namespace CenterSpace.NMath.Core.Examples.VisualBasic

  ' A .NET example in Visual Basic showing how to use the Discrete Wavelet Transform (DWT) classes.
  Module DWTExample

    Sub Main()

      ' This section shows to use various configuration settings. All of these settings can
      ' also be set via configuration file or by environment variable. More on this at: 
      ' http://www.centerspace.net/blog/nmath/nmath-configuration/

      ' For NMath to continue to work after your evaluation period, you must set your license 
      ' key. You will receive a license key after purchase. 
      ' More here:
      ' http://www.centerspace.net/blog/nmath/setting-the-nmath-license-key/
      ' NMathConfiguration.LicenseKey = "<key>"

      ' This will start a log file that you can use to ensure that your configuration is 
      ' correct. This can be especially useful
      ' for deployment. Please turn this off when you are convinced everything is 
      ' working. If you aren't sure, please send the resulting log file to 
      ' support@centerspace.net. Please note that this directory can be relative or absolute.
      ' NMathConfiguration.LogLocation = "<dir>"
      
      ' NMath loads native, optimized libraries at runtime. There is a one-time cost to 
      ' doing so. To take control of when this happens, use Init(). If your program calls 
      ' Init() successfully, then your configuration is definitely correct.
      ' NMathConfiguration.Init()
	  

      Console.WriteLine()

      ' DWT of a signal using the Harr wavelet.

      ' Do a simple DWT on a signal
      Dim Data As New DoubleVector(12, New RandGenNormal(0.0, 2.0))

      ' Choose wavelet
      Dim WaveletInstance As New DoubleWavelet(Wavelet.Wavelets.Harr)

      ' Build DWT object
      Dim DWT As New DoubleDWT(WaveletInstance)

      ' 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)

      ' Print results
      Console.WriteLine()
      Console.WriteLine("A DWT signal decomposition and reconstruction example using the Harr wavelet.")
      Console.WriteLine(String.Format("Original Signal: {0}", Data.ToString("#.##")))
      Console.WriteLine(String.Format("DWT Approximation: {0}", New DoubleVector(Approx).ToString("#.##")))
      Console.WriteLine(String.Format("DWT Details: {0}", New DoubleVector(Details).ToString("#.##")))
      Console.WriteLine(String.Format("IDWT Reconstructed signal: {0}", New DoubleVector(Signal).ToString("#.##")))

      ' DWT using a Daubeachies wavelet, then thresholding, and finally reconstucting the signal.

      ' An example to filter a signal using the DWT.
      ' Build random signal data
      Data = New DoubleVector(26, New RandGenNormal(1.0, 1.0))

      ' Choose wavelet
      WaveletInstance = New DoubleWavelet(Wavelet.Wavelets.D2)

      ' Build DWT object
      DWT = New DoubleDWT(Data.DataBlock.Data, WaveletInstance)

      ' Decompose signal with DWT to level 5
      DWT.Decompose(3)

      ' Find Universal threshold
      Dim LambdaU As Double = DWT.ComputeThreshold(DoubleDWT.ThresholdMethod.Universal, 1)

      ' Threshold all detail levels with lambdaU
      DWT.ThresholdAllLevels(DoubleDWT.ThresholdPolicy.Soft, New Double() {LambdaU, LambdaU, LambdaU, LambdaU, LambdaU})

      ' Rebuild signal to level 2
      Dim ReconstructedData2() As Double = DWT.Reconstruct(2)

      ' Rebuild the signal to level 1 - the original (filtered) signal.
      Dim ReconstructedData1() As Double = DWT.Reconstruct()

      ' Print results
      Console.WriteLine()
      Console.WriteLine("A DWT signal thresholding and reconstruction example using a Daubeachies wavelet.")
      Console.WriteLine(String.Format("Original Signal: "))
      Console.WriteLine(" {0}", Data.ToString("#.##"))
      Console.WriteLine()
      Console.WriteLine(String.Format("IDWT Reconstructed signal: "))
      Console.WriteLine(" {0}", New DoubleVector(ReconstructedData1).ToString("#.##"))

      Console.WriteLine()
      Console.WriteLine("Finished. Press Enter Key.")
      Console.Read()

    End Sub
  End Module
End Namespace

← All NMath Code Examples
Top