% -----------------------------------------------------------------------------
% FILE      : regressTest.m
% USUAGE    : test fft from length 2 to length 32768
% AUTHOR    : duweitao@cuc.edu.cn
% VERSION   : 0.1
% REVISE    : initial version
% DATE      : 2008.July.7th
% -----------------------------------------------------------------------------
fprintf(1, '# r2fft regress Testbench RUN\n');
fprintf(1, '# Compile the tagget file ');
mex   r2fftMex.c  cfft.c
fprintf(1, ' DONE\n');
errMax = 5E-5;
ampMax = 5E5;

fprintf(1, '# errMax setting is %e \n', errMax);
fprintf(1, '# ampMax setting is %e \n', ampMax);

for lv = 1:15
  len = 2 ^ lv;
  % complex test
  matOut = zeros(len,1);
  cOut   = zeros(len,1);
  xRe = rand(len,1)-0.5;
  xIm = rand(len,1)-0.5;
  amp = rand(1,1)*ampMax;
  x = amp*(xRe + j*xIm);
  matOut(:,1) = fft(x);
  cOut(:,1)  = r2fftMex(x);
  diff = matOut - cOut;
  err = sum(abs(diff))/len;
  if(err > errMax)
    fprintf(1, '# Complex test, ERR len = %d\n', len);
    return;
  else
    fprintf(1, '# Complex test, len = %-6d PASS. ', len); 
  end
  % real test
  matOut = zeros(len,1);
  cOut   = zeros(len,1);
  xRe = rand(len,1)-0.5;
  amp = rand(1,1)*1000;
  x = amp*(xRe);
  matOut(:,1) = fft(x);
  cOut(:,1)  = r2fftMex(x);
  diff = matOut - cOut;
  err = sum(abs(diff))/len;
  if(err > errMax)
    fprintf(1, '# Real test, ERR len = %d\n', len);
    return;
  else
    fprintf(1, '# Real test, len = %-6d PASS.\n', len); 
  end

end % for lv
fprintf(1, '# ALL PASS, TEST DONE.\n');
