C# Correlation Example

[TOC]

using System;
using System.Text;

using CenterSpace.NMath.Core;

namespace CenterSpace.NMath.Core.Examples.CSharp
{
  /// <summary>
  /// .NET examples in C# showing how to 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 w/ data.
      //

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


      // Create the correlation class.
      Double1DCorrelation 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("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.
      FloatComplexVector hermitian_kernel = new FloatComplexVector("(1,1)(2,1)(3,0)(2,-1)(1,-1)");
      FloatComplexVector signal = new FloatComplexVector(1, 2, 3, 4, 5, 6);

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

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

      // Create and compute the convolution for comparison
      FloatComplex1DConvolution 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();
    }
  }
}

[TOC]