Error in convolution of two gaussians using FFT and IFFT
2 views (last 30 days)
Show older comments
I am trying two convolve two gaussian functions with the form of f(x)=a*exp(-(x-b)^2/2*c^2). a is the height, b is the position of the curve's center, and c controls the width of the "bell" shape.
To perform a convolution, you can take the inverse fourier transform of two functions that undergo a fourier transform and are then multiplied: F^-1(F(f1(x))*F(f2(x)))
My code is as follows:
xstep=-10:0.05:25;
c = 5/(2*sqrt(2*log(2)));
c2 = .2/(2*sqrt(2*log(2)));
y1 = exp(-(xstep-5).^2/(2*c^2)); %actual
y2 = .5*exp(-(xstep).^2/(2*c2^2)); %instrument response
Fy1 = fft(y1);
Fy2 = fft(y2);
con = ifft(Fy1.*Fy2); % This is my convolution
figure(2)
hold on
plot(xstep,y1)
plot(xstep,y2,'r')
plot(xstep,con,'--k')
axis([-10,25,0,2])
hold off
This produces a function that is shifted and much larger than it should be. The correct results should appear as such:
test = (1/(sqrt(2*pi*(c^2+c2^2))))*exp((-(xstep-(5+3)).^2)/(2*(c^2+c2^2)));
plot(xstep,test)
0 Comments
Answers (2)
SK
on 11 Oct 2014
xstep starts from -10, so the peak of y1 is actually at 15 and the peak of y2 is at 10 (fft() doesn't know about your x-axis). The convolution has a peak at 10+15 = 25. But the x-axis on your plot starts at -10, ie: 0 is -10, so 25 will be 15. Hence your graph.
0 Comments
Matt J
on 11 Oct 2014
Edited: Matt J
on 11 Oct 2014
You forgot to weight your (discrete) convolution by the sampling interval
con = ifft(Fy1.*Fy2)*0.05;
which partly account for the scaling. You also haven't weighted y1 and y2 by 1/sqrt(2*pi*c^2) and1/sqrt(2*pi*c2^2). So there is no reason I can see that the multiplier in con will be (1/(sqrt(2*pi*(c^2+c2^2)))).
The shifts are fine, since shifts in convolution are addiitive. Since y1 is shifted by 10 from the beginning of the array and y2 is shifted by 15 from the beginning of the array, then con will be shifted by 10+15=25 from the beginning of the array, which is what your plots show.
2 Comments
See Also
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!