简体   繁体   English

2D循环卷积Vs卷积FFT [Matlab / Octave / Python]

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

I am trying to understand the FTT and convolution (cross-correlation) theory and for that reason I have created the following code to understand it. 我试图理解FTT和卷积(互相关)理论,因此我创建了以下代码来理解它。 The code is Matlab/Octave, however I could also do it in Python. 代码是Matlab / Octave,但我也可以在Python中完成。

In 1D: 在1D:

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

 c1 =   30   31   57   47   87   47   33   27    5 
 c2 =   30   31   57   47   87   47   33   27    5

In 2D: 在2D中:

 X=[1 2 3;4 5 6; 7 8 9]
 y=[-1 1];
 conv1 = conv2(x,y)
 conv1 =
        24    53    89    29    21
        96   140   197    65    42
       168   227   305   101    63

Here is where I find the problem, padding a matrix and a vector? 这是我发现问题的地方,填充矩阵和向量? How should I do it? 我该怎么办? I could pad x with zeros around? 我可以用零填充x吗? or just on one side? 还是只是一边? and what about y ? 和什么y I know that the length of the convolution should be M+L-1 when x and y are vectors, but what about when they are matrices? 我知道当xy是向量时,卷积的长度应该是M+L-1 ,但是当它们是矩阵时呢? How could I continue my example here? 我怎么能在这里继续我的榜样?

You need to zero-pad one variable with: 您需要使用以下内容对一个变量进行零填充:

  • As many zero-columns as the number of columns of other variable minus one. 与其他变量的列数减1一样多的零列。
  • As many zero-rows as the number of rows of the other variable minus one. 与其他变量的行数减1一样多的零行。

在此输入图像描述

In Matlab, it would look in the following way: 在Matlab中,它将以下列方式查找:

% 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: 为了澄清卷积,请看这张图:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM