2D circular convolution Vs convolution FFT [Matlab/Octave/Python]

痞子三分冷 提交于 2019-12-03 08:58:09

You need to zero-pad one variable with:

  • As many zero-columns as the number of columns of other variable minus one.
  • As many zero-rows as the number of rows of the other variable minus one.

In Matlab, it would look in the following way:

% 1D
x = [5 6 8 2 5]; 
y = [6 -1 3 5 1];
x1 = [x zeros(1,size(x,2))];
y1 = [y zeros(1,size(y,2))];
c1 = ifft(fft(x1).*fft(y1));
c2 = conv(x,y,'full');

% 2D 
X = [1 2 3;4 5 6; 7 8 9];
Y = [-1 1];
X1 = [X zeros(size(X,1),size(Y,2)-1);zeros(size(Y,1)-1,size(X,2)+size(Y,2)-1)];
Y1 = zeros(size(X1));    Y1(1:size(Y,1),1:size(Y,2)) = Y;
c1 = ifft2(fft2(X1).*fft2(Y1));
c2 = conv2(X,Y,'full'); 

In order to clarify the convolution, look also at this picture:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!