VB Correlation Example

← All NMath Code Examples

 

Imports System
Imports System.Text

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Core.Examples.VisualBasic

  ' .NET example in Visual Basic showing how to use the correlation classes.
  Module CorrelationExample

    Sub Main()

      '
      ' Correlation of a Harr wavelet with data.
      '

      ' Define a harr wavelet and some signal data.
      Dim harr_kernel As New DoubleVector(1, 1, -1, -1)
      Dim data As New DoubleVector(1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1)

      ' Create the correlation class.
      Dim corr As New Double1DCorrelation(harr_kernel, data.Length)

      ' Compute the correlation.
      Dim result As DoubleVector = corr.Correlate(data)

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

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

      '
      ' Correlation with a complex Hermitian kernel
      '

      ' Define a Hermitian wavelet and some signal data.
      Dim hermitian_kernel As New FloatComplexVector("(1,1)(2,1)(3,0)(2,-1)(1,-1)")
      Dim signal = New FloatComplexVector(1, 2, 3, 4, 5, 6)

      ' Create the correlation class.
      Dim zcorr As New FloatComplex1DCorrelation(hermitian_kernel, signal.Length)

      ' Compute the correlation.
      Dim corr_result As FloatComplexVector = zcorr.Correlate(signal)

      ' Create and compute the convolution for comparison
      Dim Zconv As New FloatComplex1DConvolution(hermitian_kernel, signal.Length)
      Dim conv_result As FloatComplexVector = NMathFunctions.Conj(Zconv.Convolve(signal))

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

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

    End Sub
  End Module
End Namespace


← All NMath Code Examples
Top