MATLAB: FFT gives different amplitudes for different freqeuncies

fft fourier transform

I have been working with FFTs for some time now and I just noticed this while I was trying to reproduce some results from simulations. The amplitude of the peak recovered with FFT changes with the the freqeuncy itself. Also, I noticed that different sampling frequencies on the same signal frequency produce a similar effect (with sampling freqeucny always higher than Nyquist). Lastly, I noticed that with sampling points around powers of two the effect is exxagerrated, with the delta function receovered by FFT being significantly broadened. WHy is this happenining?
Here is the code that I am using. Any help is appreciated!
sampling_points=2000;
f_s=3000; % Hz sampling frequency
time=(1:sampling_points)/f_s;
A=2;
for ii=1:100 %loop to produce spectra with different frequencies
freq(ii)=ii*1+10;
spectrum=A*cos(2*pi*freq(ii)*time); %make the spectrum
FFT_of_spectrum=fft(spectrum,[],2);
FFT_abs=2*abs(FFT_of_spectrum)/sampling_points; % rescale the fft
FFT_abs=FFT_abs(1:sampling_points/2);
FFT_abs(2:end-1)=FFT_abs(2:end-1);
peak(ii)=max(FFT_abs); % find the height of the peak
end
figure('name','FFT of time domain spectrum')
plot(FFT_abs);
xlabel('Frequency (Hz)');
ylabel('Amplitude');
figure('name','FFT of spectrum in depth domain')
plot(freq,peak)
xlabel('Frequency (Hz)');
ylabel('Amplitude of peak');

Best Answer

Hi Fabio,
Good observation. The reason for this is when there are an exact integer number of cycles in the time record, you have a periodic trig function and the fft gives the peak that you expect. When there are not an exact integer number of cycles in the time record, the function is still periodic in the time record (by definition of the fft), but the function being repeated is a trig function that's lopped off at the end. That function requires frequencies other than the fundamental to define it, which takes away from the amplitude of the fundamental and spreads the peak in the frequency domain.
For a time record of length T, the number of cycles n = fT, and here
T = N/fs = 2/3 sec
f = ii+10 (in Hz)
and
n = (ii+10)*2/3
Then n is an integer and you get a full peak a third of the time, when ii+10 is divisible by 3.
Related Question