% FFT_CORE Simple core method which computes the fft.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: Simple core method which computes the fft.
%
% CALL:        ao = fft_core(ao, type)
%
% INPUTS:      ao:   Single input analysis object
%              type: The fft type
%                    'plain' - complete non-symmetric
%                    'one'   - from zero to Nyquist
%                    'two'   - complete symmetric
%
% VERSION:     $Id: fft_core.m,v 1.7 2011/02/04 17:42:29 luigi Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function bs = fft_core(bs, type)
  
  % Which data type do we have
  switch class(bs.data)
    case {'tsdata', 'cdata', 'xydata'}
      % Check we have a sample rate
      if strcmp(class(bs.data), 'tsdata')
        fs     = bs.data.fs;
        fmin   = 0;
      else
        warning('!!! Data has no sample rate: setting to %g', 2+length(bs.data.y));
        fs     = 2+length(bs.data.y);
        fmin   = 1;
      end
      % make FFT of data
      xunits = 'Hz';
      nfft   = length(bs.data.y);
      ft     = fft(bs.data.y);
      f      = utils.math.getfftfreq(nfft,fs,type);
      switch lower(type)
        case 'plain' % get true matlab fft
          f    = reshape(f,size(ft));
        case 'one'
          ft   = ft(1:floor(nfft/2)+1);
          f    = reshape(f,size(ft));
        case 'two'
          if size(ft, 1) == 1
            ft   = fftshift(ft);
          else
            ft   = fftshift(ft);
            if rem(nfft,2) % odd number of data
              f = f.';
            else % even number of data
              f = f.';
            end
          end
        otherwise
          error('### unknown fft type.');
      end
      % Make new fsdata object
      fsd = fsdata(f, ft, fs);
      fsd.setXunits(xunits);
      fsd.setYunits(bs.data.yunits);
      % make output analysis object
      bs.data = fsd;
      % clear errors
      bs.clearErrors;
      
    otherwise
      error('### You can only fft tsdata, cdata, or xydata AOs.');
  end
  
end




