MATLAB: FFT to get noise

fftnoise

Hello,
Can anybody explain how the fft is done in case of data points? I am meassuring 50Samples per second , with 50Hz for 1 second. Well, I got a Little help from an user, but I do not understand what was done in the code. the code Looks like this:
Fs = 50; %Sampling Frequency
T = 1/Fs;
L = 50; %Signal length
Y = fft(voltage);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
plot(f,P1)
I don't understand why the code Looks like this? It seems, that the fft is done for half of the signal. Can anybody give me an example please how it is done for the whole Signal?

Best Answer

The fft is done on the whole signal:
Y = fft( voltage );
but this gives a 2-sided spectrum of positive and negative frequencies. For analysis in the frequency domain people usually just want to look at the positive frequencies in the first half of the result.
doc fft
gives examples. It's always a good idea to read the help!