%% Juan Negreira; May 2011
% Division of Engineering Acoustics
% Department of Construction Sciences
% Lunds Tekniska Högskola (LTH), Sweden

%% Description
%Calculates the discrete fourier transform of the timedomain signal y(t) 
%Y:amplitude of the frequency components 
%f:frequencies[Hz]
%Only the unique points are returned ie. f lies in 0 <= f <= Fs/2

%% Function

function [Y,f] = fft_function(t,y)

%Number of points in input data
NFFT=length(y);	

%Nyquist frequency
Fn=1/(t(2)-t(1))/2; 	

%Absolute value of the FRF
FFTY=abs(fft(y));

NumUniquePts=ceil((NFFT+1)/2);

% fft symmetric, throw away second half
FFTY=FFTY(1:NumUniquePts);

% Take magnitude of Y
Y=abs(FFTY);   

% Multiply by 2 to take into account the fact that we
% threw out second half of FFTY above
Y=Y*2;

% Account for endpoint uniqueness
Y(1)=Y(1)/2;   

% We know NFFT is even
Y(length(Y))=Y(length(Y))/2;  

% Scale the FFT so that it is not a function of the length of y.
Y=Y/length(y);  

%Frequencies
f=(0:NumUniquePts-1)*2*Fn/NFFT;

