F# Correlation Example

← All NMath Code Examples

 

#light

namespace CenterSpace.NMath.Examples.FSharp

open System

open CenterSpace.NMath.Core
open CenterSpace.NMath.Matrix

  /// <summary>
  /// A .NET example in F# demonstrating the features of the correlation classes.
  /// </summary>
module CorrelationExample =
     
      //
      // Correlation of a Harr wavelet w/ data.
      //

      // Define a harr wavelet and some signal data.
      let harr_kernel = new DoubleVector(1.0, 1.0, -1.0, -1.0)
      let data = new DoubleVector(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0)


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

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

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

      printfn "Double precision 1D correlation of Harr wavelet computed."
      printfn "-----------------------------------------"
      printfn "Kernel = %s" (harr_kernel.ToString())
      printfn "Data = %s" (data.ToString())
      printfn "Correlation = %s" (result.ToString())
      printfn "Correlation trimmed to areas with full kernel overlap = %s" (result_trimmed.ToString())
      printfn ""

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

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

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

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

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

      printfn "Complex float precision 1D correlation with Hermitian kernel was computed."
      printfn "------------------------------------------------"
      printfn "Hermitian kernel = %s" (hermitian_kernel.ToString())
      printfn "Signal = %s" (signal.ToString())
      printfn "Correlation = %s" (corr_result.ToString())
      printfn "Complex-conjugate of convolution = %s" (conv_result.ToString())
      printfn ""

      printfn "Press Enter Key"
      Console.Read() |> ignore
← All NMath Code Examples
Top