何时以及如何对离散卷积进行零填充?

时间:2019-03-21 03:13:35

标签: matlab convolution idl-programming-language

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

我从IDL中尝试修改为MA​​TLAB的代码开始。该代码的相关部分为:

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',该选项返回卷积的中心部分,其大小与u的conv(u,v)相同,但这似乎不是正确的方法关于这个问题。如何在MATLAB中进行类似的零填充?

1 个答案:

答案 0 :(得分:0)

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

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