是否可以使用conv()在MATLAB中执行按列卷积?

问题描述 投票:1回答:2

我有两个2D矩阵,它们的相同列数AB。我想对这两个矩阵的对应列进行卷积,并将结果存储到一个名为result的新矩阵中。假设result具有适当的尺寸,我当前的方法是:

for i = 1 : size( A, 2 ) % number of columns
    result(:,i) = conv( A(:,i), B(:,i) );
end

是否有一种避免使用conv()或直接使用conv2()的循环的方法?

matlab matrix convolution
2个回答
0
投票

如果要使用conv功能,可以尝试conv(A_i,B_i, 'full'),但您还可以使用下面的代码进行卷积,例如对于列卷积convIt(A,B,1)和行卷积convIt(A,B,2)

function C = convIt(A,B,dim)
% the code is equivalent to running conv(A_i,B_i, 'full') in matlab
% (where A_i and B_i are columns (dim=1) or rows (dim=2) of A,B)
% and then stack the results together

if 1==dim || nargin<3 % default
  A = [A;zeros(size(A))];
  B = [B;zeros(size(B))];
elseif 2==dim
  A = [A,zeros(size(A))];
  B = [B,zeros(size(B))];
end
C = ifft(fft(A,[],dim).*fft(B,[],dim),[],dim);
if 1==dim || nargin<3 % default
  C = C(1:end-1,:);
elseif 2==dim
  C = C(:,1:end-1);
end

0
投票

您可以使用(圆形)卷积和DFT之间的关系,并利用fftfft不同,可以沿指定的维度工作的事实:

conv2

注意,由于浮点数值精度,在此结果与通过conv2A = rand(5,7); B = rand(4,7); % example matrices. Same number of columns s = size(A,1)+size(B,1)-1; % number of rows of result result = ifft(fft(A,s,1).*fft(B,s,1)); 获得的结果之间可能会以eps的数量级略有差异。特别是,如果您的输入是真实的,则结果可能具有(很小的)虚部,因此您可能需要将eps应用于结果。

© www.soinside.com 2019 - 2024. All rights reserved.