Fs=2000; % sampling frequency
Fn=Fs/2; % Nyquist frequency
t=0:1/Fs:1; % time vector sampled at Fs Hz,
% length of 1 second
x = sin(2*pi*t*100)+sin(2*pi*t*200)+sin(2*pi*t*300)...
+sin(2*pi*t*400)+sin(2*pi*t*500)+ sin(2*pi*t*600)...
+sin(2*pi*t*700)+sin(2*pi*t*800) +sin(2*pi*t*900) ; %Signal
NFFT=2.^(ceil(log(length(x))/log(2)));% Next highest power of 2
% greater than length(x).
FFTX=fft(x,NFFT); % Take FFT, padding with zeros.
% length(FFTX)==NFFT
NumUniquePts = ceil((NFFT+1)/2);
FFTX=FFTX(1:NumUniquePts); % FFT is symmetric, throw away
% second half
MX=abs(FFTX); % Take magnitude of X
MX=MX*2; % Multiply by 2 to take into
% account the fact that we
% threw out second half of
%FFTX above
MX(1)=MX(1)/2; % Account for endpoint
% uniqueness
MX(length(MX))=MX(length(MX))/2; % We know NFFT is even
MX=MX/length(x); % Scale the FFT so that it is
% not a function of the length
% of x.
f=(0:NumUniquePts-1)*2*Fn/NFFT;
plot(f,MX);