# Why MATLAB fft cos makes imaginary parts?

The cos fourier transform has no imaginary parts, but in this code it has imaginary parts that little big.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% figure(7);

fs=128;

t=-4*pi:1/fs:4*pi-1/fs;

x=1*cos(0.5*t);

X=fft(x);

N=length(x);

n=0:N-1;

f=n*fs/N;

phs=angle(fftshift(X));

subplot(2,1,1); plot(f,abs(fftshift(X))/N,'LineWidth',1.5);

title('FFT'); ylabel('Magnitude'); xlabel('Frequency(Hz)');

subplot(2,1,2); plot(f,phs*180/pi,'-o'); xlabel('Frequency(Hz)');

ylabel('Phase(Degree)');

figure(8);

Re=real(X); Im=imag(X);

subplot(2,1,1); stem(f,Re); ylabel('Re(G(f))'); xlabel('Frequency(Hz)');

subplot(2,1,2); stem(f,Im); ylabel('Im(G(f))'); xlabel('Frequency(Hz)'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

If I make time range a multiple of 0.5, the results of imaginary parts has very little value that is e-14~15.

Let me know why this problem happened...

Those $$10^{-15}$$ error terms after the FFT are typical roundoff errors... Apart from that, you should properly frame the cosine into a fully periodic length, as it seems you did, then you can expect the imaginary part to be as close to zero as the numerical format allows. Note that the accumulated FFT roundoff error grows with the square root of the length N of the FFT block; so the longer the FFT block, the larger will be the accumulated FFT error.

The following line of OCTAVE / MATLAB code shows the effect of frame length (aka aperture size) on the FFT computation:

L = 16   ; period of cosine
N = 128  ; frame length of sample block (integer multiple of L)
figure,stem(imag(fft(cos(2*pi*[0:N-1]./L),N))); title('imaginary part of the FFT');


Now let us change the period of the cosine so that the frame does not include an exact number of periods:

L = 15  ; period of cosine
N = 128  ; frame length of sample block (non-integer multiple of L)
figure,stem(imag(fft(cos(2*pi*[0:N-1]./L),N))); title('imaginary part of the FFT');


Now if your result display more deviations than those found on the first plot, then your frame length is probably the problem. If you, on the other hand, observe very close to zero (probably randomly distributed) values as in the first plot, then that's probably due to the numerical roundoff errors involved in the FFT computation.

• I appreciate your answer. If you don't mind, can you tell me why I have to properly frame the cosine into a fully periodic length please? I wanna know that.
– Park
Jul 12, 2018 at 12:10
• it's an inherent assumption of DFT (the transform that you compute via FFT) that finite length input and output signals are periodically extending to infinity, as it would be the case in DFS (discrete Fourier series). So if your cosine sample block do not contain proper number of samples, there will be a jump at the block boundaries eliminating the perfect continuity assumption. Hence the computed result will not belong to the ideal continuous cosine but a jumpy one. Jul 12, 2018 at 12:33
• Is the proper number of samples means integer to total sample number? And what is the 'jump' means? Can you explain more detail please?
– Park
Jul 12, 2018 at 12:59
• Proper means integer number of periods. Jump means, first and last samples of the signal block do not constitude a natural flow of the analytic signal given by a formula. For example if your signal is $\cos(2\pi n/16)$ and your 64 sample frame is given by $n = [0:63]$ then the first sample is $x[0]=\cos(0)=1$ and the last sample is $x[63]=\cos(2 \pi 63/16)$ now, the next block begins at index $n=64$ where the signal samples is $x[64] = \cos(2 \pi 64/16) = \cos(8\pi) = 1 = x[0]$. As you can see, the first sample of the next block is actually the first sample of the current block = No jump. Jul 12, 2018 at 14:06
• "And what is the 'jump' means?" -- Plot some non-integer number of cycles -- 1 1/2 cycles will be dramatic. Now plot that same 1 1/2 cycles, followed by the same thing. The signal will have a discontinuity, going from -1 to +1, at the point where you've mashed the partial cycles together. If you imagine that extended to infinity -- that's what the FFT is assuming. Jul 22, 2021 at 15:13