yoloswag420
yoloswag420

Reputation: 33

Matlab FFT for gaussian function

I am trying to obtain gaussian curve by using matlab's fft. The problem is that in one case my attemp to reduce noise by dividing F=dt.*F./exp(-1i.*nu.*T/2) is not working (img 1) and in the second case if I am trying to take absolute value of fft result's I dont have decent scale in graph (img 2).

N=512;
T=10;
dt=T/(N-1);
t=linspace(-5,5,N);
f=exp(-t.^2);
F=fft(f);

F1=F(1:N/2+1);
F2=F(N/2+1:N);
F=[F2,F1];

dnu=(N-1)/(N*T);
nuNyq=1/(2*dt);
nu=-nuNyq+dnu*(0:N);
F=dt.*F;
%F=dt.*F./exp(-1i.*nu.*T/2);


y=linspace(-5,5,N);
F2=pi.^(1/2).*exp(-y.^2/4);

hold on
plot(y,F2); 
%plot(nu,real(F),'r');
plot(nu,abs(F),'r');
legend('analiticFT','FFT')
xlim([-5 5])
hold off

img 1 img. 1

img2 img. 2

Upvotes: 3

Views: 4523

Answers (1)

SleuthEye
SleuthEye

Reputation: 14579

It seems the scaling in your formula for the analytic Fourier Transform is not quite correct. According to this Fourier Transform table on Wikipedia, the transform of the continuous time-domain signal

y\left(t\right) = e^{-a t^2}

is

Y\left(f\right) = \sqrt{\frac{\pi}{a}} \cdot e^{-\frac{\left(\pi f\right)^2}{a}}

where in your case a=1. Correspondingly, you should compare the FFT of the time domain signal

t=linspace(-5,5,N);
f=exp(-t.^2);

with the analytic Fourier Transform

F2 = sqrt(pi)*exp(-(pi*y).^2);

So, plotting the comparison with:

hold off;
plot(y,F2); 
hold on;
plot(nu,abs(F),'r');
legend('analiticFT','FFT')
xlim([-5 5])

yields:

enter image description here

Now that we have establish a proper basis for comparison, we can look at why you are getting the oscillations in img 1. Simply put, the reference Gaussian pulse f=exp(-t.^2); you have generated has a peak at t=0. The corresponding "zero" discrete time instant is naturally the first index in the array (index 1). However in your array, this peak appears at index N/2. Under the Shift theorem, this causes an additional exp(-pi*j*k) term in the frequency domain, responsible for the oscillations you are seeing. To fix this, you should shift back your Gaussian pulse with ifftshift:

F=fftshift(fft(ifftshift(f)));

Upvotes: 6

Related Questions