C# SWT Example

← All NMath Code Examples

 

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

using CenterSpace.NMath.Core;

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

    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 arent 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 SWT of a signal using the Harr wavelet.

      // Test input signal
      var signal = new DoubleVector( "1, 2, 3, 56, 40, 8, 2, 40, 16" );

      // Build the class 
      DoubleSWT swt = new DoubleSWT( signal.ToArray(), new DoubleWavelet( DoubleWavelet.Wavelets.Harr ) );

      // Find the maximum level of decomposition for this signal and wavelet pair
      var maximumDecompLevel = swt.MaximumDecompLevel();

      // Using the SWT now decompose the signal to the maximum level, all of the detail and approximations are returned in a Tuple<>
      var detailsAndApproximations = swt.Decompose( maximumDecompLevel );

      // The SWT approximations at level 2
      var approxLevel2 = swt.WaveletCoefficients( DiscreteWaveletTransform.WaveletCoefficientType.Approximation, 2 );

      // The SWT details at level 2
      var detailsLevel2 = swt.WaveletCoefficients( DiscreteWaveletTransform.WaveletCoefficientType.Details, 2 );

      // Reconstruct the original signal
      var rsignal = swt.Reconstruct();

      Console.WriteLine();
      Console.WriteLine( "SWT signal decomposition and reconstruction example using the Harr wavelet." );
      Console.WriteLine( String.Format( "Original Signal: {0}", signal.ToString( "#.##" ) ) );
      Console.WriteLine( String.Format( "SWT Approximation level 2: {0}", new DoubleVector( approxLevel2 ).ToString( "#.##" ) ) );
      Console.WriteLine( String.Format( "SWT Details level 2: {0}", new DoubleVector( detailsLevel2 ).ToString( "#.##" ) ) );
      Console.WriteLine( String.Format( "ISWT, Reconstructed signal: {0}", new DoubleVector( rsignal ).ToString( "#.##" ) ) );

      #endregion      

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

← All NMath Code Examples
Top