4

is it possible to get "Two dimensional convolution" just like matlab "CONV2" function bu ilnumerics? and how to do it? ilnumerics can operate matrix like matlab as the same convinience. so if I have matlab code ,I can do the same with ilnumeris ,but the funcion "CONV2" in mathlab is so called "Built-in function" . by the way , my matrix may be 1660×521 size. here is the help document of "CONV2" in matlab.

% CONV2 Two dimensional convolution.

%   C = CONV2(A, B) performs the 2-D convolution of matrices A and B.

%   If [ma,na] = size(A), [mb,nb] = size(B), and [mc,nc] = size(C), then
   mc = max([ma+mb-1,ma,mb]) and nc = max([na+nb-1,na,nb]).

%   C = CONV2(H1, H2, A) first convolves each column of A with the vector
%   H1 and then convolves each row of the result with the vector H2.  If
%   n1 = length(H1), n2 = length(H2), and [mc,nc] = size(C) then
%   mc = max([ma+n1-1,ma,n1]) and nc = max([na+n2-1,na,n2]).
%   CONV2(H1, H2, A) is equivalent to CONV2(H1(:)*H2(:).', A) up to
%   round-off.
%
%   C = CONV2(..., SHAPE) returns a subsection of the 2-D
%   convolution with size specified by SHAPE:
%     'full'  - (default) returns the full 2-D convolution,
%     'same'  - returns the central part of the convolution
%               that is the same size as A.
%     'valid' - returns only those parts of the convolution
%               that are computed without the zero-padded edges.
%               size(C) = max([ma-max(0,mb-1),na-max(0,nb-1)],0).
%
%   See also CONV, CONVN, FILTER2 and, in the Signal Processing
%   Toolbox, XCORR2.
Dmitry
  • 13,797
  • 6
  • 32
  • 48
07012220
  • 85
  • 1
  • 7

1 Answers1

2

Yes, of course it is possible! If you check the Matlab's documentation, you will see which algorithm 'conv2' uses. It is called 'two-dimensional convolution equation in spatial form'. You can realize this in ILNumerics easily. The following code implements the straightforward formal equation:

ILArray<double> A = new double[,] { { 1, 4, 7 }, { 2, 5, 8 }, { 3, 6, 9 } };
ILArray<double> B = new double[,] { { 1, 5, 9, 13 }, { 2, 6, 10, 14 }, { 3, 7, 11, 15 }, { 4, 8, 12, 16 } };

// calc the size
int ma = A.S[0]; int na = A.S[1];
int mb = B.S[0]; int nb = B.S[1];

int mc = Math.Max(ma + mb - 1, Math.Max(ma, mb));
int nc = Math.Max(na + nb - 1, Math.Max(na, nb));
ILArray<double> C = ILMath.zeros(mc, nc);

for (int n1 = 0; n1 < mc; n1++)
{
    for (int n2 = 0; n2 < nc; n2++)
    {
        for (int k1 = 0; k1 < ma; k1++)
        {
            int bm = n1 - k1;

            // Make sure the outside of the boundaries 
            // are checked! 
            if (bm < 0 || bm >= mb)
            {
                continue;
            }

            for (int k2 = 0; k2 < na; k2++)
            {
                int bn = n2 - k2;

                // Here as well
                if (bn < 0 || bn >= nb)
                {
                    continue;
                }

                // If it is a fit - calculate and add
                C[n1, n2] = C[n1, n2] + A[k1, k2] * B[bm, bn];
            }
        }
    }
}

Note that this is not the most efficient way of the implementation. The result is exactly same as in other implementations.

We are working on the Signal Processing Toolbox, which will provide a more efficient implementation of such functions.

With ILNumerics Array Visualizer you can easily check the result, both in textual and visual way, and it responds to immediate changes of the array: enter image description here

kurtyka
  • 81
  • 8
  • thanks. mayby implement it by FFT will be more efficient for large size matrix. – 07012220 Mar 24 '15 at 00:49
  • 1
    Please vote the answer if it was useful for you, and also mark the question answered iin case you got the proper answer. – kurtyka Mar 25 '15 at 13:47