【问题标题】:Is there a way to perform column-wise convolution in MATLAB using conv()?有没有办法使用 conv() 在 MATLAB 中执行按列卷积?
【发布时间】:2020-01-14 23:55:38
【问题描述】:

我有两个具有列数相同的二维矩阵,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


    【解决方案1】:

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

    A = 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));
    

    请注意,由于浮点数值精度,此结果与使用forconv 获得的结果之间可能存在细微差别,大约为eps。特别是,如果您的输入是实数,则结果可能有一个(非常小的)虚部,因此您可能希望将real 应用于结果。

    【讨论】:

      【解决方案2】:

      如果你想使用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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2015-01-15
        • 2018-04-02
        • 1970-01-01
        相关资源
        最近更新 更多