Skip to content

Convolution & Cross Correlation

Sambit Paul edited this page Oct 26, 2024 · 12 revisions

Real 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]

Fast Convolution

Fast Convolution (which is real convolution but done using Fourier method) 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.fastConvolve(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: [1+2j, 2+2j, 3-3j, 4+1j, 5-4j]
Kernel used: [1+2j, 2+4j, 5-2j, 4+7j]
Code
mode = "full";
Complex[] sg = UtilMethods.matToComplex(complexSignal);
Complex[] krn = UtilMethods.matToComplex(complexKernel);
ComplexConvolution c1 = new ComplexConvolution(sg, krn);
double[][] out = UtilMethods.complexTo2D(c1.convolve(mode));
Output: [-3+4j, -8+14j, 14+23j, 24+36j, 20+25j, 81+18j, 26+2j, 48+19j]

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]

Fast 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.fastCrossCorrelate(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); //Or use cc.fastCrossCorrelate(mode)
Output: [0, 18, 90, 74, 52, 77, 110, 247, 110, 77, 52, 74, 90, 18, 0]
Clone this wiki locally