Skip to content

Convolution & Cross Correlation

Sambit Paul edited this page Dec 24, 2021 · 12 revisions

Convolution

Convolution works in 3 modes:

  1. Full: This returns the convolution at each point of overlap between kernel and signal.
  2. Same: This returns the convolution such that it maintains the same size as the original signal.
  3. Valid: This returns the convolution for the point where the kernel and the signal overlap completely.

The parameters for this filter are as follows:

  • Mode of Operation ⇨ Can be "full", "same", "valid"
  • Kernel ⇨ 1-D Array the signal is convolved with

Signal used: [2, 8, 0, 4, 1, 9, 9, 0]
Kernel used: [1, 3, 1, 3]
Code
String mode = "full"; //Can be "valid", "same"
Convolution con = new Convolution(signal, kernel);
double[] out = con.convolve(mode);
Output: [2, 14, 26, 18, 37, 16, 49, 39, 36, 27, 0]

Complex Convolution

Convolution works in 3 modes:

  1. Full: This returns the convolution at each point of overlap between kernel and signal.
  2. Same: This returns the convolution such that it maintains the same size as the original signal.
  3. Valid: This returns the convolution for the point where the kernel and the signal overlap completely.

The parameters for this filter are as follows:

  • Mode of Operation ⇨ Can be "full", "same", "valid"
  • Kernel ⇨ 1-D Array of the signal is convolved with. Can be complex or double.

Signal used: [2.2+2.2j, 2.3-6.7j, 15.9+4.94j, 10.7-23.88j, 16.45-3.01j]
Kernel used: [1, 0, 1, 0.5]
Code
mode = "same";
Complex[] sg = UtilMethods.matToComplex(this.complexSignal);
Complex[] krn = UtilMethods.matToComplex(this.complexKernel);
ComplexConvolution c1 = new ComplexConvolution(sg, krn);
double[][] out = UtilMethods.complexTo2D(c1.convolve(mode));
Output: [-1.6+2.4j, 2.2+2.2j, 3.9-9.1j, 14.5+1.54j, 5.7-15.88j]

Cross-Correlation

Works in the exact same way as convolve.

The parameters for this filter are as follows:

  • Mode of Operation ⇨ Can be "full", "same", "valid"
  • Kernel ⇨ 1-D Array the signal is cross-correlated with

Signal used: [2, 8, 0, 4, 1, 9, 9, 0]
Kernel used: [1, 3, 1, 3]
Code
String mode = "full"; //Can be "valid", "same"
CrossCorrelation cc = new CrossCorrelation(signal, kernel);
double[] out = cc.crossCorrelate(mode);
Output: [6, 26, 14, 38, 15, 40, 43, 37, 36, 9, 0]

Autocorrelation

Performs autocorrelation using the cross-correlation process.


Signal used: [2, 8, 0, 4, 1, 9, 9, 0]
Code
CrossCorrelation cc = new CrossCorrelation(signal);
double[] out = cc.crossCorrelate(mode);
Output: [0, 18, 90, 74, 52, 77, 110, 247, 110, 77, 52, 74, 90, 18, 0]
Clone this wiki locally