Asked by Jeff Fischer
on 15 May 2011

If I create a simple sin(2*pi*.1*t) or same for a cos, and let t=[-50:50], and transform it, I get spikes in both the real and imaginary outputs. But Fourier transform theory says I should get only real components for Cosine and only Imaginary components for sine. Why is Matlab different?

Answer by Teja Muppirala
on 16 May 2011

Accepted Answer

There is absolutely nothing strange here.The discrete fourier transform assumes PERIODIC SIGNALS.

In your case:

t=[-50:50];

y = sin(2*pi*.1*t) ;

plot(t,y);

Y = fft(y);

you are not really taking the fourier transform of a single sine wave. The first element y(1) is zero, and the last element y(101) is zero, and therefore the periodic extension [ y y y y ...] has two consecutive zeros in it. When you take the fourier transform of that signal, that causes a whole bunch of other frequency components to come in.

If you do this however,

t=[-50:49];

y = sin(2*pi*.1*t) ;

plot(t,y);

Y = fft(y);

Then [y y y y y ...] is indeed a pure single periodic sinusoid, and you see that you get exactly the answer you were hoping for (a single frequency component with sine as purely imaginary, and cosine as purely real). Another example:

figure,

t=[0:0.01:1];

y = cos(2*pi*10*t) ;

Y = fft(y);

plot(real(Y));

hold all

t=[0:0.01:0.99];

y = cos(2*pi*10*t) ;

Y = fft(y);

plot(real(Y),'r');

legend({'An "almost" periodic cosine wave' 'A periodic cosine wave'})

EDIT:

Again, the answer lies in the fact that the discrete fourier transform assumes periodic signals.

Question: When you type this,

t=[-50:49];

y = sin(2*pi*.1*t) ;

Y=fft(y,2000);

What are you really taking the discrete fourier transform of? Answer: You are taking the fourier transform of this infinitely extended periodic signal [y zeros(1,1900) y zeros(1,1900) y zeros(1,1900) ...] .

Plot it:

plot([y zeros(1,1900) y zeros(1,1900) y zeros(1,1900)]);

Is this a sine wave? Clearly it is not. This curve has a bunch of components in it, some of which are real and some of which are imaginary. And when you plot the FFT, you are infact seeing all of those components.

Compare again:

figure

t=[0:0.01:19.99];

y = sin(2*pi*10*t);

Y = fft(y,2000);

plot(real(Y));

hold all

t=[0:0.01:0.99];

y = sin(2*pi*10*t) ;

Y = fft(y,2000);

plot(real(Y));

hold all

t=[0:0.01:19.99];

y = sin(2*pi*10*t);

y(101:end) = 0;

Y = fft(y);

plot(real(Y),'r:');

legend({'Real part of FFT of a sine wave' 'Real part of FFT of something that''s not a sine wave', 'The same Real part FFT of something that''s not a sine wave'})

Jeff Fischer
on 20 May 2011

Thanks for trying but that isn't quite the answer.

Do the same thing with Y=fft(y,2000) and you will see just as significant a impulse in the real as the imaginary plane.

Teja Muppirala
on 20 May 2011

Read the appended portion above.

Kiran Anginthaya
on 27 Nov 2018

Very insightful answer, thanks!

Sign in to comment.

Answer by Andrew Newell
on 15 May 2011

I don't think it is a round off error. If you run this code:

n = 1000;

t=linspace(0,2,n);

x=sin(2*pi*t);

y = fft(x);

x2 = ifft(y);

you'll find that x and x2 agree within machine precision. I think the spikes occur because this is a discrete Fourier transform, not the integral.

EDIT: To go a bit further: the Fourier integral transforms are F(cos(2*pi*k0*t)) = 0.5*(Dirac(k+k0)+Dirac(k-k_0)) F(sin(2*pi*k0*t)) = 0.5*1i*(Dirac(k+k0)-Dirac(k-k_0)). Now if we choose some random value for k0, and do the discrete Fourier transform of the cosine over [-k0,k0],

n = 1000;

k0 = rand(1);

t=linspace(-1,1,n);

x=cos(2*pi*k0*t);

y = fft(x);

plot(t/k0,real(y),t/k0,imag(y))

We find two positive spikes in the real component at t=-k0 and t=k0 and a much smaller one for the imaginary component. If we do the same for the sin,

t=linspace(-1,1,n);

x=sin(2*pi*k0*t);

y = fft(x);

figure

plot(t/k0,real(y),t/k0,imag(y))

we get a negative and positive spike in the imaginary component and much smaller spikes in the real component - much as predicted for the integrals.

Jeff Fischer
on 15 May 2011

Try doing it with a longer FFT y=fft(x,2000)

Andrew Newell
on 16 May 2011

Sign in to comment.

Answer by Daniel Shub
on 15 May 2011

Jeff Fischer
on 15 May 2011

Read through the blog, thanks. But he didn't seem to cover this.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Juan Manriquez (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/7475-fft-result-does-not-jive-with-theory-for-basic-sine-and-cosine#comment_458457

Sign in to comment.