MATLAB: How to evaluate a convolution integral by fast fourier transform

convolution integralfftwrap-aroundzero padding

I need to evaluate a convolution integral by fft. Therefore, I have read somewhere in a paper to first zero-pad two multiplying functions and wrap around one of them. Then, an element-by-element multiplication and inverse transforming back to the spacial domain and then removing the elements corresponding to the added zeros will solve the problem. To give it a try, I started with a simple example. Consider f(x)=1 (for -1<x<1) and 0 (elsewhere). Also, g(x)=1-x.^2 (for -1<x<1) and 0 (elsewhere). I have calculated the convolution integral analytically to compare with my code results. However, my code does not provide acceptable results especially on boundaries. Also, I don't think I know and understand zero-padding and wrapping around order. Can anyone help me with this, please? Here is the simple code I have written:
clc
clear all
% the analytical result
d=[…
-0.6667
-0.2678
0.0868
0.3970
0.6630
0.8846
1.0619
1.1948
1.2835
1.3278
1.3278
1.2835
1.1948
1.0619
0.8846
0.6630
0.3970
0.0868
-0.2678
-0.6667];
hold on
plot(linspace(-1,1,20)',d,'o')
N=pow2(10);
dx=2/N;
x=linspace(-1,1,N);
x=x';
f=[1-x.^2;zeros(N,1)];
Ff=fft(f);
g=[ones(N,1);zeros(N,1)];
Fg=fft(g);
FG=Ff.*Fg;
fg=2/N*ifft(FG);
plot(x,fg(N/2+1:3/2*N))

Best Answer

Correct zero-padding to use the FFT for 1-D or 2-D convolutions is implemented in my FEX submission 2-D convolution using the FFT.
Related Question