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

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

我想采用两个1-D向量的离散卷积。矢量对应于作为频率的函数的强度数据。我的目标是将一个强度向量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',它返回与conv(u,v)相同大小的卷积的中心部分,但这似乎不是正确的方法关于这个问题。如何在MATLAB中进行类似的零填充?

matlab convolution idl-programming-language
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.