function retval = FFTransform(y, invFFT)
% usage: retval = FFTransform(y, invFFT)
% description: This function accepts input vector y of length
% 2^p  to and outputs the DFT Y of y using the FFT algorithm
% if invFFT is 0, otherwise returns the inverse DFT of y.

% programmer: t. shores
% last update: 2/14/16
 
% Octave and Matlab compatible file
y = y(:); % turn y into a column
N = length(y);
p = log2(N);
if (p ~= round(p))
  error('Input vector length %d is not a power of 2', N);
end
omegaN = exp(-pi*i/N).^(0:N-1).'; % all the omegas we need
Y = y(FFTsort(p)); % initial Y at bottom level
if (invFFT ~= 0)
  Y = conj(Y);
end
stride = 1;
for l=1:p
  omegal = omegaN(1:N/stride:N);
  stride = 2*stride;
  for j=1:stride:N
    evens = Y(j:j+stride/2-1);
    odds = omegal.*Y(j+stride/2:j+stride-1);
    Y(j:j+stride/2-1) = evens + odds;
    Y(j+stride/2:j+stride-1) = evens - odds;
  end
end
if (invFFT)
  retval = conj(Y);
else
  retval = Y/N;
end
