Error in convolution of two gaussians using FFT and IFFT
조회 수: 5 (최근 30일)
이전 댓글 표시
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
답변 (2개)
SK
2014년 10월 11일
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
Matt J
2014년 10월 11일
편집: Matt J
2014년 10월 11일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!