VB.NET Convolution Example

[TOC]

Imports System
Imports System.Globalization
Imports System.Threading
Imports System.Text

Imports CenterSpace.NMath.Core

Namespace CenterSpace.NMath.Core.Examples.VisualBasic

  '  .NET example in VB.NET showing how to use the convolution classes.
  Module ConvolutionExample

    Sub Main()

      ' Simple example to compute a moving average filter with convolution

      ' Create some random signal data.
      Dim Rand As RandomNumberGenerator = New RandGenMTwist(4230987)
      Dim Data As New DoubleVector(500, Rand)

      ' Create a simple averaging kernel.
      Dim Kernel As New DoubleVector(0.2, 0.2, 0.2, 0.2, 0.2)

      ' Create the convolution class.
      Dim Conv As New Double1DConvolution(Kernel, Data.Length)

      ' Compute the convolution.
      Dim smoothed_data As DoubleVector = Conv.Convolve(Data)

      Console.WriteLine()
      Console.WriteLine()
      Console.WriteLine("Double precision 1D convolution computed.")
      Console.WriteLine("-----------------------------------------" & Environment.NewLine)

      ' Simple example to compute a single precision convolution

      ' Create some random signal data.
      Dim DataF As New FloatVector(1, 2, 3, 7, 2, 1)

      ' Create the averaging kernel
      Dim KernelF() As Single = {0.2F, 0.2F, 0.2F, 0.2F, 0.2F}

      ' Create the convolution class and compute the convolution
      ' When only the kernel and data length is specified in this constructor, the 
      ' convolution results are truncated to match the length of the input data.
      ' This is typically what most users need.
      Dim ConvF As New Float1DConvolution(New FloatVector(KernelF), DataF.Length)

      ' using the convolution length allows up to exactly size the output data buffer.
      Dim Smootheddataf As New FloatVector(ConvF.Length)

      ' Execute the convolution
      ConvF.Convolve(DataF, Smootheddataf)

      ' Trim the results of the convolution where the kernel fullly overlapped the data set.
      ' NOTE: This trim just makes a new view into the convolution result, no data is copied.
      Dim trimmedkernel_smootheddataf As FloatVector = ConvF.TrimConvolution(Smootheddataf, ConvolutionBase.Windowing.FullKernelOverlap)

      ' Trim the results to the center of the convolution that matches the input data size.
      Dim trimmeddata_smootheddataf As FloatVector = ConvF.TrimConvolution(Smootheddataf, ConvolutionBase.Windowing.CenterWindow)

      ' Write out results
      Console.WriteLine("Convolution results showing smoothed data.")
      Console.WriteLine("Input data = ")
      Console.WriteLine(DataF.ToString())

      Console.WriteLine("Convolution kernel = ")
      Console.WriteLine(New FloatVector(KernelF).ToString())

      Console.WriteLine("Convolved data = ")
      Console.WriteLine(Smootheddataf.ToString())

      Console.WriteLine("Trimmed smooth data to fully overlapping kernel = ")
      Console.WriteLine(trimmedkernel_smootheddataf.ToString())

      Console.WriteLine("Trimmed smooth data centered on data length = ")
      Console.WriteLine(trimmeddata_smootheddataf.ToString())

      Console.WriteLine("-----------------------------------" + Environment.NewLine)

      ' Simple example to compute a complex double precision convolution with real data.

      ' Create some real signal data and complex kernel
      ' Naturally the data could be complex, and the following code would be unchanged.
      Dim kernelz As New DoubleComplexVector("(1,1) (2,2) (3,3) (4,4)")
      Dim Dataz As New DoubleComplexVector("(1,0) (2,0) (3,0) (4,0) (5,0) (6,0)")

      ' Create the convolution class and compute the convolution
      ' When only the kernel and data length is specified in this constructor, the 
      ' convolution results are truncated to match the length of the input data.
      ' This is typically what most users need, with the convolution edge effects removed.
      Dim Convz As New DoubleComplex1DConvolution(kernelz, Dataz.Length)
      Dim smootheddataz As DoubleComplexVector = Convz.Convolve(Dataz)

      ' Write out results
      Console.WriteLine("Complex convolution results with a complex kernel and real data.")
      Console.WriteLine("Input data = ")
      Console.WriteLine(Dataz.ToString())

      Console.WriteLine("Complex convolution kernel = ")
      Console.WriteLine(kernelz.ToString())

      Console.WriteLine("Convolved data = ")
      Console.WriteLine(smootheddataz.ToString())

      Console.WriteLine("-----------------------------------" + Environment.NewLine)

      ' Simple example to compute a single precision convolution with full length output
      ' and a strided kernel.

      ' Create some random signal data.
      Dim datafs() As Single = {1, 2, 3, 7, 2, 1}

      ' Create the strided averaging kernel
      Dim kernelfs() As Single = {0.2F, -1.0F, 0.2F, -1.0F, 0.2F, -1.0F, 0.2F, -1.0F, 0.2F}

      ' Create the convolution class and compute the convolution
      ' using this constructor we can specific an offset and strided kernel, the length
      ' of the data, and the output length.  To find the full length of the
      ' convolution data, use the Float1DConvolution.OutputLength property as seen below.
      ' (This may be shorter than the length specified in this constructor.)
      Dim Convfs As New Float1DConvolution(kernelfs, 0, 2, datafs.Length)

      ' Do the convolution and get the generated result.
      Dim smootheddatafs() As Single = Convfs.Convolve(datafs)

      ' Write out results
      Console.WriteLine("Convolution results with a 2-strided kernel.")
      Console.WriteLine("Input data = ")
      Console.Write("[ ")
      Dim I As Integer
      For I = 0 To datafs.Length - 1
        Console.Write("{0,2:0.#} ", datafs(I))
      Next I
      Console.WriteLine("]")

      Console.WriteLine("Strided convolution kernel = ")
      Console.Write("[ ")
      For I = 0 To kernelfs.Length - 1
        Console.Write("{0,2:0.#} ", kernelfs(I))
      Next I
      Console.WriteLine("]")

      Console.WriteLine("Convolved data = ")
      Console.Write("[ ")
      For I = 0 To Convfs.Length - 1
        Console.Write("{0,2:0.#} ", smootheddatafs(I))
      Next I
      Console.WriteLine("]")

      Console.WriteLine("-----------------------------------" + Environment.NewLine)

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

    End Sub

  End Module

End Namespace

[TOC]