MATLAB Answers

Matlab analytical FT and FFT comparison

47 views (last 30 days)
Ongun
Ongun on 28 Mar 2015
Edited: Youssef Khmou on 29 Mar 2015
My aim is to compare the analytical Fourier transform of a function with the FFT of the same function using Matlab routines and correct normalization coefficients. In order to evaluate the analytical FT I used the function fourier and for FFT I use the function fft while normalizing with respect to the length of the vector. Later I plot the FFT till the Nyquist frequency and also plot the results of the analytical FT on the same plot but their centers and height does not match. I am not that familiar with FFTs and I would really appreciate it if someone pointed me in the right direction. You can check the working code snippet below:
close all;
clear all;
clc;
N = 100;
dt = 0.01;
Fs = 1/dt;
F = zeros(N,1);
n0 = 50;
for n = 1:N
F(n) = exp(-((n-n0)*dt)^2*pi^2);
end
G_cmp = fft(F)/N;
f_arr = Fs/2*linspace(0,1,N/2+1);
syms t f
F_an = exp(-(t-n0*dt)^2*pi^2);
G_an = fourier(F_an, t, f);
f1 = figure;
p1 = plot(dt*(1:N), F);
saveas(f1, 'pulse_time.png');
f2 = figure;
p2 = plot(f_arr, 2*abs(G_cmp(1:N/2+1)), f_arr, 2*abs(subs(G_an, 'f', f_arr)));
saveas(f2, 'pulse_freq.png');

  0 Comments

Sign in to comment.

Answers (1)

Youssef  Khmou
Youssef Khmou on 29 Mar 2015
Edited: Youssef Khmou on 29 Mar 2015
numerical fft requires a shift if we want to visualize the spectrum with both negative and positive frequencies, scaling problem is not yet solved, however try the following version, the theoretical transformation is calculated using 2*pi*f instead of f :
close all;
clear all;
clc;
N = 100;
dt = 0.01;
Fs = 1/dt;
F = zeros(N,1);
n0 = 50;
for n = 1:N
F(n) = exp(-((n-n0)*dt)^2*(pi^2));
end
G_cmp = fft(F)/((N));
f_arr = Fs/2*linspace(-1,1,N);
syms t f
F_an = exp(-(t-n0*dt)^2*(pi^2));
G_an = fourier(F_an, t, 2*pi*f);
f1 = figure;
subplot(1,2,1)
p1 = plot(dt*(1:N), F);
%saveas(f1, 'pulse_time.png');
%f2 = figure;
subplot(1,2,2)
p2 = plot(f_arr, fftshift(2*abs(G_cmp)),'-+', f_arr, 2*abs(subs(G_an, 'f', f_arr)),'r--');
%saveas(f2, 'pulse_freq.png');

  3 Comments

Ongun
Ongun on 29 Mar 2015
Well thanks for the answer it looks much better. I only plot the positive frequencies by the way, so I think I do not have to use the fftshift function.
Ongun
Ongun on 29 Mar 2015
By the way my function is too complex for the fourier to evaluate in one step, so I think should better use the convolution theorem. It consists of the multiplication of an exponential and sine.
Youssef  Khmou
Youssef Khmou on 29 Mar 2015
you are welcome, i have written an fft code previously, here is the implementation :
%==========================================================================
% function z=Fast_Fourier_Transform(x,nfft)
%
% N=length(x);
% z=zeros(1,nfft);
% Sum=0;
% for k=1:nfft
% for jj=1:N
% Sum=Sum+x(jj)*exp(-2*pi*j*(jj-1)*(k-1)/nfft);
% end
% z(k)=Sum;
% Sum=0;% Reset
% end
however for large vector, it is time consuming.

Sign in to comment.

Sign in to answer this question.

Products