function [dftdata,df] = computefft(data,dt)
% COMPUTEFFT A local definition for the FFT function.
%   COMPUTEFFT(DATA) performs the fft of the data assuming unit spacing and 
%   returns the complex coefficients. The data are first mean-zeroed before the
%   frequency spectrum is computed.
%
%   COMPUTEFFT(DATA,DT) performs the fft of the data with sampling increment DT 
%   in seconds.
%
%   [DFTDATA,DF] = COMPUTEFFT(...) returns the frequency step, DF.
%
%   Input
%       DATA  | m x n | double | m is # of samples and n is # of channels
%       DT    | 1 x 1 | double | optional: time step between samples in seconds
%
%   Output
%       DFTDATA | m/2 x n | double | complex frequency components
%       DF      | 1 x 1   | double | frequency step in Hz
%   
%   Example        
%       computefftex; % Run the example script
%
%   Reference
%
%   See also FFT,COMPUTEFFTEX.

% Created by: Jesse Norris
%             Kevin P. Granata Musculoskeletal Biomechanics Laboratory
%             http://jesse.musculoskeletaldynamics.com
% Last Revision: 13 June 2006

  [nSamples nChannels] = size(data); % Number of samples & channels  

  df  = 1/(dt*(nSamples-1));         % Compute the frequency step    

  nFrequencies = ceil(nSamples/2);   % Truncate to Nyquist frequency  

  dftdata = zeros(nFrequencies,nChannels); % Initialize matrix for output   

  for ind = 1:nChannels
      dft = 2/nSamples*fft(data(:,ind)-mean(data(:,ind))); % Complex coeffs
      dftdata(:,ind) = dft(1:nFrequencies); % Truncate to Nyquist freq
  end

end % END computefft