function [halfSpec, fVals] = realFFT(y, fs)

% Real FFT
%
% Purpose:  This function computes the positive half spectrum for a real input signal 
%
% Use: halfSpec = realFFt(y);
%       compute the spectrum
%   [halfSpec, fVals] = realFFt(y);
%       compute the spectrum and normalized frequencies fs = 1
%   [halfSpec, fVals] = realFFT(y,fs)
%       compute the spectrum and the frequency vector
%
% Input parameters: 
%           y: real input signal
%           fs: sampling frequency
%
% Output parameters: 
%       	halfSpec: the positive half spectrum scaled 1/n
%       	fVals: frequency vector
%
% Description and algorithms: 
%           1.) Check input variables
%           2.) Compute full spectrum using fft function
%           3.) Extract half spectrum
%           4.) Scale values
%           5.) Compute frequency vector
%
% Name: Philipp Schwarz
% Matriculation number: 12001552
% Date: 30.06.2022
% History: Original

%% Check inputs
if ~isreal( y )
    error("This function is only valid for real data.");
end;

if (nargin == 1)
    fs=1;
end;
%% Compute FFT

fullSpec = fft( y );

%% Extract the positive half spectrum

n = length( y );
% Length of the half spectrum

m = round( n / 2 );
halfSpec = fullSpec(1:m);

%% Scale the FFT to deliver normalized amplitudes

halfSpec = 2 * halfSpec / n;

halfSpec(1) = halfSpec(1) / 2;

%% Compute the frequency vector

if (nargout == 2)
    fVals = fs*(0:(m-1))/n;
end;


