gpt4 book ai didi

c# - ILNumerics如何实现二维卷积?

转载 作者:太空宇宙 更新时间:2023-11-03 20:34:19 26 4
gpt4 key购买 nike

是否有可能像 matlab“CONV2”函数一样获得“二维卷积”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.

最佳答案

是的,当然可以!如果您查看 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,您可以轻松地以文本和视觉方式检查结果,并且它会响应数组的即时更改: enter image description here

关于c# - ILNumerics如何实现二维卷积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29191146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com