# Unexpected imaginary part in the fft of a zero-padded cosine

I simulated a cosine waveform $$y = \cos(\omega t)$$ and I applied the FFT algorithm to it. As expected, I have a frequency peak at $$\pm \omega$$ only in the real part, and nothing in the imaginary part. As expected, if I zero-pad the array, the peak in the real part becomes a $$\mathrm{sinc}(\omega D)$$ (where D is the length of the window). However, in the imaginary part, a sort of anti-symmetric $$\mathrm{sinc}$$ appears. I don't understand why, because I was expecting the imaginary part to stay zero since the Fourier transform of the cosine is purely real.

Why is this happening? Here a plot of the imaginary and real parts of the FFT of the described signal. In orange: FFT of the original signal. In blue: FFT of the zero-padded signal.

Here is my code, written in MATLAB language.

dt = 0.02;
fs = 1/dt;

start = 0;
stop = 1;
t = start:dt:stop-dt;

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

N = 10000;

Y = fftshift(fft(y))./length(y);
df = fs / length(y);
f = (0:(length(y)-1))*df;
f(f >= fs/2) = f(f >= fs/2) - fs;
f = fftshift(f);

figure;
subplot(2,1,1);

hold on
plot(f, real(Y));

ylabel('Re(fft)')
xlabel('f (Hz)')

subplot(2,1,2);

hold on
plot(f, imag(Y));

ylabel('Im(fft)')
xlabel('f (Hz)')


Edit: By zero-padding only to the right of my signal I am breaking the even parity of the cosine and $$y[N-n]=y[n]$$ does not hold anymore (see Hilmar's answer). Therefore, I tried to zero pad both to the left and to the right, in order to preserve the parity

N = 10000;


The result is the following and it looks weird: the imaginary part is still not zero and the real part oscillates between positive and negative values. Why this zero-padding does not work as expected?

• First, find the FT of $\cos(\omega t + \phi)$. Second, find the DFT of the time-limited sinusoid $\cos(\omega n + \phi)$ (which is an infinite-duration sinusoid multiplied by a rectangular window of finite duration). Finally, realize that zero-padding is equivalent to changing $\phi$.
– MBaz
Commented Apr 15, 2021 at 13:37

A signal that's discrete in frequency is periodic in time. By using an FFT you inherently make this assumption and the FFT calculates the spectrum of a signal that's infinitely repeated in time.

The FFT is ONLY real valued if the time domain signal has even symmetry, i.e. $$x(-t) = x(t)$$. For a discrete periodic signal of period N this becomes $$x[n] = x[N-n]$$. That holds true for a pure cosine (at least for some frequencies), but not for the zero padded version.

• With some frequencies you mean only when the sampling frequency is an integer multiple of the signal frequency? Anyway, you are correct, by zero-padding the signal I broke the parity of the cosine. This is why I tried to zero-pad with an identical array of zeros both to the left and to the right of the signal. Such zero padding should preserve the parity, but the result looks wrong. See the edited version of my question. Commented Apr 15, 2021 at 17:22
• You are still padding wrong. You time shifted and now your cosine is not centered around $t=0$ anymore. Time shifting also breaks even parity . You need to insert the zeros in the middle of the FFT frame. Commented Apr 16, 2021 at 15:29
• Thank you! This is a very helpful hint. I tried to pad the center of the array and now it looks so much better. However, I can still see an (apparently) sinusoidal oscillation in the imaginary part. The amplitude is two orders of magnitude less than the peaks in the real part, so I would like to ignore it and call it a numeric error, but the oscillations looks definitely too smooth to be just a numerical lack of precision. Commented Apr 16, 2021 at 15:41
• It's not numerical error. You are probably padding one zero too many/less. Make sure that the first and last non-zero sample are the same. Commented Apr 17, 2021 at 14:10

When you want to simulate / visualise the frequency spectrum of a continuous-time signal (such as your $$y = \sin(\omega t)$$) using a digital computer, you first sample the continuous-time signal with an appropriate sampling-rate, then you apply a window to the resulting discrete-time sequence (say $$y[n]$$ in this case) so as to get a finite length block out of an ideally infinite length sequence, and then apply a DFT/FFT transform on the windowed chunk to get the frequency-domain sequence $$Y[k]$$; and finally, properly visualise / plot the obtained complex-valued sequence $$Y[k]$$ on the monitor. All of the elements in this processing chain do have an effect on what you see on the final monitor output.

From Fourier theory, we know that the Fourier transform of multiplication of two signals (your infinite length ideal signal $$y(t)$$ and the finite length window signal) is equivalent to the convolution of the Fourier transforms of the individual signals.

Therefore, what you see at the output is not the ideal signal's spectrum, but a convolution of that with the Fourier transform of the particular window used in obtaining the chunk.

In your case, your ideal signal is a sinusoid whose Fourier transform is a pair of impulses at their frequency location. And your window (that you implicitly applied) is rectangular which has a Fourier transform of a periodic sinc waveform. The convolution of the two, therefore, yields a sinc ripple spectrum at the location of the impulses.

In your case, however, it seems that your sampling of the ideal sinusoid resulted in a rather special condition known as FFT aperture-period matching: an exactly integer number of periods of the sine wave fits into the FFT block; which creates the illusion of an infinitely long sinusoidal waveform to exist in the block of samples, eliminating the finite length window's effect on the spectrum.

Thefeore, in the first plot, all the spectrum seems to be zero (a silent zero actually) except at the sinusoidal frequencies which happens to exactly match with one of the FFT bin frequencies.

However, when you pad extra zeros to the end of the data block, you brake periodic continuity illusion and the reality comes to life showing its face atthe ripples of the sliently exsiting window Fourier transform. That's what you see in the second plot.

Relevant visual, and on FFT meaning.

If you append a zero to a perfect cosine, you increase lengths of all FFT bases (real cosines, imag sines) by 1, so none of them perfectly correlate (multiply & sum) with the original cosine. Need extra frequencies (with intermediate phases, achieved by imag in FFT) to compensate.