【问题标题】:When and how to do zero-padding for a discrete convolution?何时以及如何对离散卷积进行零填充?
【发布时间】:2019-03-21 03:13:34
【问题描述】:

我想对两个一维向量进行离散卷积。这些向量对应于作为频率函数的强度数据。我的目标是将一个强度向量 B 与其自身进行卷积,然后将结果与原始向量 B 进行卷积,依此类推,每次将结果与原始向量 B 进行卷积。我想要最终结果与原始向量 B 的长度相同。

我开始尝试将 IDL 中的代码修改为 MATLAB。代码的相关部分为:

for i=1,10 do begin
if i lt 2 then A=B else A=Rt
n=i+1
Rt=convol(A,B,s,center=0,/edge_zero)
...
endfor

我已经用 MATLAB 重写了

for i = 2:11
    if i < 3
        A = B; % indices start at 0, not 1
    else
        A = Rt;
    end
    n = i + 1;

    % Scale by 1/s
    Rt = (1/s).*conv(A,B);
    ...
end

但我不确定如何合并使用edge_zero 选项的零填充。在 IDL 中,卷积计算向量边缘的元素值,就好像向量用零填充一样。 MATLAB 中卷积函数的可选第三个选项包括选项 'same',它返回与 conv(u,v) 的 u 大小相同的卷积的中心部分,但这似乎不是正确的方法关于这个问题。如何在 MATLAB 中进行类似的零填充?

【问题讨论】:

  • MATLAB 始终使用零填充进行卷积。为什么你说“相同”不是正确的方法?你得到不同的结果吗?

标签: matlab convolution idl-programming-language


【解决方案1】:

这是我的博士研究所需的代码,我知道它可以正确填充零。希望对你有帮助。

function conv_out=F_convolve_FFT(f,g,dw,flipFlag),
 if(nargin<4), flipFlag==0; end;

 % length of function f to be convolved, initialization of conv_out, and padding p
 N=length(f); conv_out=zeros(1,N); p=zeros(1,N);

 % uncomment if working with tensor f,g's of rank 3 or greater
 % f=F_reduce_rank(f); g=F_reduce_rank(g);

 % padding. also, this was commented out: EN=length(fp);
 fp = [f p]; gp = [g p];

 % if performing convolution of the form c(w) = int(f(wp)g(w+wp),wp) = int(f(w-wp)gbar(wp),wp) due to reverse-order domain on substitution
 if(flipFlag==1), gp=fliplr(gp); end;

 % perform the convolution. You do NOT need to multiply the invocation of "F_convolve_FFT(f,g,dw,flipFlag)" in your program by "dx", the finite-element.
 c1 = ifft(fft(fp).*fft(gp))*dw;

 % if performing "reverse" convolution, an additional circshift is necessary to offset the padding
 if(flipFlag==1), c1=circshift(c1',N)'; end;

 % offset the padding by dNm
 if(mod(N,2)==0), dNm=N/2; elseif(mod(N,2)==1), dNm=(N-1)/2; end;

 % padding. also, this was commented out: EN=length(fp);
 conv_out(:)=c1(dNm+1:dNm+N);

return

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 2017-11-14
    相关资源
    最近更新 更多