MATLAB Answers

Matlab analytical FT and FFT comparison

104 ビュー (過去 30 日間)
Ongun
Ongun 2015 年 3 月 28 日
編集済み: Youssef Khmou 2015 年 3 月 29 日
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 件)

Youssef  Khmou
Youssef Khmou 2015 年 3 月 29 日
編集済み: Youssef Khmou 2015 年 3 月 29 日
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 件のコメント

Ongun
Ongun 2015 年 3 月 29 日
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 2015 年 3 月 29 日
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 2015 年 3 月 29 日
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