DADiSP Worksheet Functions > Function Categories > Fourier Transforms and Signal Processing > CONV2D

 

CONV2D

Purpose:

Computes the 2D convolution of two arrays.

Syntax:

CONV2D(array1, array2, row1, col1, row2, col2, "shape")

 

array1

-

An array.

array2

-

An array.

row1

-

Optional. An integer, the start row in array 1. Defaults to 1

col1

-

Optional. An integer, the start column in array 1. Defaults to 1

row2

-

Optional. An integer, the end row in array 1. Defaults to the number of rows.

col2

-

Optional. An integer, the end column in array 1. Defaults to the number of columns.

"shape"

-

Optional. A string, the output shape flag. Valid options:

"full"

:

full convolution output (default)

"same"

:

output is a section of the same size as array1

"valid"

:

output only includes values not computed with zero-padding

Returns:

An array, the convolved result.

Example:

W1: {{4, 8, 2},

     {1, 1, 3}, 

     {3, 2, 2}} 

 

W2 contains a filter kernel table:

 

W2: {{1, 3},

     {3, 2}} 

 

conv2d(W1, W2) ==

 

{{ 4, 20, 26,  6},

 {13, 36, 28, 13}, 

 { 6, 16, 19, 12}, 

 { 9, 12, 10,  4}} 

 

conv2d(W1, W2, 2, 2, 4, 4) ==

 

 {{36, 28, 13, 0},

  {16, 19, 12, 0},

  {12, 10,  4, 0},

  { 0,  0,  0, 0}}

Example:

W1: ravel(1..24, 6)

W2: ravel(1..9, 3);

W3: conv2d(W1, W2)

W4: conv2d(W1, W2, "same")

W5: conv2d(W1, W2, "valid")

 

W1 == {{1,  7, 13, 19},

       {2,  8, 14, 20},

       {3,  9, 15, 21},

       {4, 10, 16, 22},

       {5, 11, 17, 23},

       {6, 12, 18, 24}}

 

W2 == {{1, 4, 7},

       {2, 5, 8},

       {3, 6, 9}}

 

W3 == {{ 1,  11,  48, 120, 167, 133},

       { 4,  35, 129, 291, 377, 292},

       {10,  74, 246, 516, 632, 478},

       {16,  95, 291, 561, 671, 502},

       {22, 116, 336, 606, 710, 526},

       {28, 137, 381, 651, 749, 550},

       {27, 117, 306, 504, 555, 399},

       {18,  72, 180, 288, 306, 216}}

 

W4 == {{ 35, 129, 291, 377},

       { 74, 246, 516, 632},

       { 95, 291, 561, 671},

       {116, 336, 606, 710},

       {137, 381, 651, 749},

       {117, 306, 504, 555}}

 

W5 == {{246, 516},

       {291, 561},

       {336, 606},

       {381, 651}}

 

W3 contains the full convolution of arrays in W1 and W2 and has size 8 x 6.

 

W4 contains the 6 x 4 centered section of the full convolution with the same shape as W1 shown in red.

 

      {{ 1,  11,  48, 120, 167, 133},

       { 4,  35, 129, 291, 377, 292},

       {10,  74, 246, 516, 632, 478},

       {16,  95, 291, 561, 671, 502},

       {22, 116, 336, 606, 710, 526},

       {28, 137, 381, 651, 749, 550},

       {27, 117, 306, 504, 555, 399},

       {18,  72, 180, 288, 306, 216}}

 

W5 contains the 4 x 2 centered section of the full convolution computed without zero-padding shown in red.

 

      {{ 1,  11,  48, 120, 167, 133},

       { 4,  35, 129, 291, 377, 292},

       {10,  74, 246, 516, 632, 478},

       {16,  95, 291, 561, 671, 502},

       {22, 116, 336, 606, 710, 526},

       {28, 137, 381, 651, 749, 550},

       {27, 117, 306, 504, 555, 399},

       {18,  72, 180, 288, 306, 216}}

Remarks:

CONV2D performs the two-dimensional linear convolution in the time domain. For an image I[x, y] and a kernel function f[x, y], the 2D linear convolution c[x, y] is defined as:

 

 

Values outside the dimensions of an array are assumed to be zero.

 

For the full convolution, the output size is:

 

 numrows(array1) + numrow(array2) - 1  x  numcols(array1) + numcols(array2) - 1

 

If shape is "same", the output array has the same size as array1. This option is useful to force the output to the same size as the input for image processing applications.

 

If shape is "valid", the output size is:

 

numrows(array1) - numrows(array2) + 1 x numcols(array1) + numcols(array2) + 1

 

if the size of array1 is larger than array2, otherwise an empty array is returned. This option returns the section of the full convolution where both arrays fully overlap during the convolution computation such that no outside the boundary assumed zero values are used.

 

See FCONV2D for a frequency domain implementation of 2D convolution.

 

See IMCONV for image convolution that handles individual RGB components.

See Also:

CONV

FCONV2D

FFT2

IMCONV

IMINTERP

NONLIN2D

SOBEL