I am trying to get the Fourier transform of a sinc function but I don't know why fft()
(fast Fourier transform) is not working as it works for a basic sine function.
Here is my code, the plot produced is empty:
function fourier_transform
Ts= 1/50000;
t = 0:Ts:.5-Ts;
% p = sin(40*t);
m = sin(100*t)./(100*t);
f = fft(m);
plot(t,abs(f));
end
CodePudding user response:
m(1)
is computed by sin(0)/0
, which equals NaN
.
NaN
means "Not a Number", and indicates an error situation. Any computation you do with NaN
will propagate that NaN
, to warn you that there was an error somewhere along the way when computing. For example, sum(m)
equals NaN
. Since each output element of the FFT involves computations with each input of m
, all of those output elements will be NaN
as well.
Your f
is all NaN
, and plot
will just skip the NaN
points, and so produces no output.
You can fix your computation in one of two ways:
Avoid the division by zero by dividing by
100*t 1e-9
, the very small value added will prevent a zero but otherwise not affect other results.m = sin(100*t)./(100*t 1e-9);
Know that
m(1)
involves a division by zero and fix it.m = sin(100*t)./(100*t); m(1) = 1;