C# Correlation Example

← All NMath Code Examples

 

using System;
using System.Text;

using CenterSpace.NMath.Core;

namespace CenterSpace.NMath.Core.Examples.CSharp
{
  /// <summary>
  /// A .NET examples in C# demonstrating the use the correlation classes.
  /// </summary>
  class CorrelationExample
  {

    static void Main( string[] args )
    {

      # region correlation of a Harr wavelet and data.

      //
      // Correlation of a Harr wavelet with data.
      //

      // Define a harr wavelet and some signal data.
      var harr_kernel = new DoubleVector( 1, 1, -1, -1 );
      var data = new DoubleVector( 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 );


      // Create the correlation class.
      var corr = new Double1DCorrelation( harr_kernel, data.Length );

      // Compute the correlation.
      DoubleVector result = corr.Correlate( data );

      // Remove edge effects - trim result to areas with full kernel overlap
      DoubleVector result_trimmed = corr.TrimConvolution( result, CorrelationBase.Windowing.FullKernelOverlap );

      Console.WriteLine();

      Console.WriteLine( "Double precision 1D correlation computed." );
      Console.WriteLine( "-----------------------------------------\n" );
      Console.Write( "Kernel = " );
      Console.WriteLine( harr_kernel.ToString() );
      Console.Write( "Data =   " );
      Console.WriteLine( data.ToString() + "\n" );
      Console.WriteLine( "Correlation = " );
      Console.WriteLine( result.ToString() );
      Console.WriteLine( "Correlation trimmed to areas with full kernel overlap = " );
      Console.WriteLine( result_trimmed.ToString() );

      #endregion

      #region correlation with a complex Hermitian kernel

      //
      // Correlation with a complex Hermitian kernel
      //

      // Define a Hermitian wavelet and some signal data.
      var hermitian_kernel = new FloatComplexVector( "(1,1)(2,1)(3,0)(2,-1)(1,-1)" );
      var signal = new FloatComplexVector( 1, 2, 3, 4, 5, 6 );

      // Create the correlation class.
      var zcorr = new FloatComplex1DCorrelation( hermitian_kernel, signal.Length );

      // Compute the correlation.
      FloatComplexVector corr_result = zcorr.Correlate( signal );

      // Create and compute the convolution for comparison
      var zconv = new FloatComplex1DConvolution( hermitian_kernel, signal.Length );
      FloatComplexVector conv_result = NMathFunctions.Conj( zconv.Convolve( signal ) );

      Console.WriteLine();
      Console.WriteLine();
      Console.WriteLine( "Complex float precision 1D correlation computed." );
      Console.WriteLine( "------------------------------------------------\n" );
      Console.Write( "Hermitian kernel = " );
      Console.WriteLine( hermitian_kernel.ToString() );
      Console.Write( "Signal =           " );
      Console.WriteLine( signal.ToString() + "\n" );
      Console.WriteLine( "Correlation = " );
      Console.WriteLine( corr_result.ToString() );
      Console.WriteLine( "Complex-conjugate of convolution = " );
      Console.WriteLine( conv_result.ToString() );

      #endregion

      Console.WriteLine();
      Console.WriteLine( "Finished. Press enter key to exit." );
      Console.Read();
    }
  }
}

← All NMath Code Examples
Top