# FFT of a pure sine wave is not a single peak

For fun, I computed the FFT of a pure sine wave. I chose chose the sample length to be an even multiple of the signal period, so that I don't see windowing-effects. Here is the Matlab code that I used:

% Signal properties
f_signal = 1000;

% Sampling properties
f_sample = 4000;
T_total = 100;

% Calculate signal vector
t = 0:1/f_sample:T_total;
N = length(t);
V = sin(2*pi*f_signal*t);

% Calculate FFT
f_spectrum = (0:N-1)/N*f_sample;
V_spectrum = abs(fft(V))/(N/2);

% Plot
figure(2)
semilogy(f_spectrum, V_spectrum)
set(gca, 'XLim', [f_signal-5, f_signal+5])
xlabel('Frequency in Hz')
ylabel('Normalized amplitude')


But instead of a single peak, I get the following result: This strikes me as odd for two reasons:

1. The effect is really huge. The center peak has an amplitude of 0.9 instead of 1, and the next bin to the right has an amplitude of 0.3 instead of 0.
2. While I would expect that limited numerical precision broadens my peak a bit, I am quite surprised that the effect is so silky smooth. I always thought of rounding errors as "noise" of the least significant digit. Noise usually leads to jarry spectra.

I observed that if I choose a slightly lower frequency (999.9975) then the effect is minimal. What is going on here?

• I believe you've discovered leakage phenomena, my friend. Welcome to digital domain! – jojek Apr 25 '14 at 19:26
• The latter part of this answer describes essentially the result that you found and the cause thereof. – Dilip Sarwate Apr 25 '14 at 22:07

Just as I wanted to click the "ask" button, I realized that I had run into a fencepost problem.

My time vector is too long by 1 point. This means that the first and last point of my voltage vector are the same, which in turn means that my periodic signal has a phase shift as it repeats. This also explains why the phenomenon went (almost) away as I decreased the frequency.

If I instead define my time vector properly as

t = 0:1/f_sample:T_total-1/f_sample;


then I get the result that I expect: A single peak and a tiny bit of noise around it. • one reason i keep bitching at TMW (about 2 decades ago i was even conversing with Cleve Moler about this) about the hard-coded "count from 1" indexing, is because of fencepost problems. – robert bristow-johnson Apr 26 '14 at 16:00