C# DWT Example

← All NMath Code Examples

 

using System;
using System.Globalization;
using System.Threading;
using System.Text;

using CenterSpace.NMath.Core;

namespace CenterSpace.NMath.Core.Examples.CSharp
{
  /// <summary>
  /// A .NET example in C# showing how to use the Discrete Wavelet Transform (DWT) classes.
  /// </summary>
  class DWTExample
  {

    static void Main( string[] args )
    {
      // 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();

      #region DWT of a signal using the Harr wavelet.

      // Do a simple DWT on a signal
      var data = new DoubleVector( 12, new RandGenNormal( 0.0, 2.0 ) );

      // Choose wavelet
      var wavelet = new DoubleWavelet( Wavelet.Wavelets.Harr );

      // Build DWT object
      var dwt = new DoubleDWT( wavelet );

      // 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 );

      // Print results
      Console.WriteLine();
      Console.WriteLine( "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( "#.##" ) ) );

      #endregion

      #region 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
      wavelet = new DoubleWavelet( Wavelet.Wavelets.D2 );

      // Build DWT object
      dwt = new DoubleDWT( data.DataBlock.Data, wavelet );

      // Decompose signal with DWT to level 5
      dwt.Decompose( 3 );

      // Find Universal threshold
      double lambdaU = 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
      double[] reconstructedData2 = dwt.Reconstruct( 2 );

      // Rebuild the signal to level 1 - the original (filtered) signal.
      double[] reconstructedData1 = 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( "#.##" ) );

      #endregion


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

← All NMath Code Examples
Top