MATLAB Answers

0

Matlab analytical FT and FFT comparison

Ongun 님이 질문을 제출함. 28 Mar 2015
최근 활동 Youssef Khmou 님이 편집함. 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

로그인 to comment.

제품

답변 수: 1

Answer by Youssef Khmou
on 29 Mar 2015
Edited by 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

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.
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.
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.

로그인 to comment.



Translated by