是否有可能得到“二维卷积”就像matlab“CONV2”函数bu ilnumerics ?怎么做? ilnumerics可以像matlab一样操作矩阵,同样方便。所以如果我有matlab代码,我可以用ilnumeris做同样的事情,但是在mathlab中的函数“CONV2”被称为“内置函数”。顺便说一句,我的矩阵可能是1660×521大小。 这是matlab中“CONV2”的帮助文档。
% 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.
答案 0 :(得分:1)
是的,当然有可能!如果您查看Matlab的文档,您将看到'conv2'使用哪种算法。它被称为“空间形式的二维卷积方程”。您可以轻松地在ILNumerics中实现这一点。以下代码实现了简单的形式化方程式:
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];
}
}
}
}
请注意,这不是最有效的实施方式。结果与其他实现完全相同。
我们正在开发信号处理工具箱,它将更有效地实现这些功能。
使用ILNumerics Array Visualizer,您可以轻松地以文本和可视方式检查结果,并响应数组的即时更改: