Huge difference between the result of fft function Matlab and analytical Fourier transform of the same function

81 views (last 30 days)
Hey,
I am trying to find a way to obtain the numerical fourier transform of a function (it is not a signal and I only want to obtain the numerical fourier transform of a function). For a test code, I tried to see what is the result of fft matlab for a Gaussian function and compared it with the analytical fourier transform of this Gaussian. I have attached the plot of both results. Why the amplitude of fft result is that huge compared to analytical result? I can use fftshift to shift the result of fft to center but still the issue of amplitudes are there. I am wondering isn't it because it is a Gaussian function and so not periodic over time? Does fft only works if the function we have is spanned from -infinity to +infinity (basically a signal)? or we can use it if we want ot calculate the fourier transform of a function which spans from t-1 to t-2?
  2 Comments
Paul
Paul on 11 Sep 2022
Edited: Paul on 11 Sep 2022
If the code is posted, I'm sure we can sort it out.
Also, by definiion the DFT (as computed by fft) can only be applied for signals that have have finite duration, so NOT from -inf to inf. If the signal of interest is of infinite duration, then fft can be used if we're willing to live with a windowed version of the signal, with the windowing fucntion being zero where the underlying signal is small.
Not sure what distinction is being made between signals and functions. Mabye you're using the term "function" to mean a finite duration signal? I've not heard of that nomenclature, but maybe it's used.
Shaily_T
Shaily_T on 11 Sep 2022
Thanks for your response! I mean, my function starts at a certain point and ends at another certain point in time. For example, if we have a sin(2*pi*t) function, it is a periodic function that spans over the whole time range but a Gaussian function will start at a certain point and end at another certain point. My question is, can fft be used for both cases? By your second response, do you mean what I have done and shown in the code now? Multiplying fftshift by ts. Because now, I can get a similar result to the analytical form.
Here is my code for a Gaussian function.
I also have confusion which I appreciate your help for. I checked the result of fftshift by incorporating your guidance for multiplying it by ts, and its result is the same as using the direct definition of the fourier transform by allying trapz function. However, my question is, why is the result of fft before implementing fftshift strange? I have attached the plot. Indeed it shows two peaks in the frequency domain, which I’m pretty sure is not the case for the fourier transform of a Gaussian. I have seen some explanations, but still, I don’t understand why.
%% test for fourier of simple function
N = 5E4;
ts = 0.1;
t = (-N/2:N/2-1)*ts;
f = exp(-pi*(t.^2));
% plot(t,f)
deltaf = 1/(N*ts);
w = ((-N/2:N/2-1))*deltaf;
fourierf = exp(-pi*w.^2);
plot(w,fourierf)
fft_f = fft(f);
shift_fft_f = ts*fftshift(fft_f);
plot(w,abs(shift_fft_f))

Sign in to comment.

Accepted Answer

Paul
Paul on 11 Sep 2022
Can't say for sure w/o seeing the code, but I supsect the fftshifted curve will be close to the blue curve if you mulitply the fftshifted result by the sampling period.
  6 Comments
Paul
Paul on 15 Sep 2022
Q2: Correct. The output of an N-point FFT are frequency domains samples with indepedent variable n = 0:(N-1) (the samples are zero outside this range thought not shown explicitly). The sample values n = 0:(N-1) can also be mapped to frequencies by multiplying n by 2*pi/N (rad/sample), or 1/N (cycles/sample), or 2*pi/N/Ts (rad/sec), or 1/N/Ts (Hz) depending on whatever is preferred. After applying fftshift, the independent variable is n = -N/2:(N/2 -1) for N even and n = ( (N-1)/2 ) : ( (N-1)/2 ) for N odd. However, keep in mind that if doing phase adjustment that the "frequency," w, used in exp(1j*n0*w) must be in rad (aka rad/sample)
Q3:Correct. Translation in the time domain doesn't affect the magnitude of the Fourier transform. However, keep in mind that magnitude and phase don't have clear meaning for signals with FTs that include Dirac deltas. But even in this case, the time translation still only results in a multiplication by a complex exponential of the FT of the unshifted signal.
syms t w
f(t) = cos(t);
F(w) = fourier(f(t))
F(w) = 
% show that the FT of the shifted signal is an exp*FT of the unshifted
% signal
simplify(fourier(f(t-0.5)) - exp(-1j*sym(0.5)*w)*F(w))
ans = 
0
Also, for the N-point DFT, instead of using the explicit phase adjustment for an N-point signal that doesn't start at n = 0 (as done above) we can use a circular shift on the input to fft. For example, suppose we have a signal with N = 8 values
rng(100)
N = 8;
x = rand(1,N);
that correspond to discrete-time values n of
n = -17:10;
If we want to use the DFT to get N samples of the DTFT we can do either
w = (0:(N-1))/N*2*pi;
X1 = fft(x).*exp(-1j*w*n(1));
or
X2 = fft(circshift(x,n(1)));
% show equal results
max(abs(X2-X1))
ans = 6.7642e-15
I suspect the latter is more numerically robust.

Sign in to comment.

More Answers (1)

David Goodmanson
David Goodmanson on 11 Sep 2022
Edited: David Goodmanson on 11 Sep 2022
Hi ST,
Your frequency grid runs from -5 to 5, which for an fft is -fs/2 to fs/2 (fs being the sampling frequency), so fs = 10. The sampling interval delta_t = 1/fs = 1/10. It appears that you are trying to approximate a continuous Riemann integral
Int g(t) e^(-2pi i f t) dt
by using the fft. That approximation is a sum over indices (done by the fft) times the width of the intervals.
Sum (stuff) * delta_t
So if you multiply by 1/10, that takes the fft result down by exactly the right amount.
  1 Comment
Shaily_T
Shaily_T on 11 Sep 2022
Hi DG,
Thanks for your nice explanation! so, fft function indeed do a summation over the indices. Is that correct?
Also, If possible could you please comment on my latter questions as well? I mean this part:"I also have confusion which I appreciate your help for. I checked the result of fftshift by incorporating your guidance for multiplying it by ts, and its result is the same as using the direct definition of the fourier transform by allying trapz function. However, my question is, why is the result of fft before implementing fftshift strange? I have attached the plot. Indeed it shows two peaks in the frequency domain, which I’m pretty sure is not the case for the fourier transform of a Gaussian. I have seen some explanations, but still, I don’t understand why."
Thank you so much!

Sign in to comment.

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!