//---------------------------------------
// go_fft.sci
// calculate fft on scilab
// ver : 1.0
//---------------------------------------

function [amp, frequency]=go_fft(input_data, fs, win_sel, out_form)

printf("===== go_fft =====\n");
printf(" window   = %s\n", win_sel);
printf(" out form = %s\n", out_form);

//get input length
input_length = length(input_data);

printf(" input_length = %d\n", input_length);

//output length
out_length = input_length/2;

//windowing
if win_sel == 'hn'
  window_adjust_coef = 0.5;
elseif win_sel == 'hm'
  window_adjust_coef = 0.54;
else
  window_adjust_coef = 1;
end

win_l = window(win_sel, input_length);

printf(" window_adjust_coef = %f\n",window_adjust_coef);
  
if win_sel ~= 're'  
  input_windowing = input_data .* win_l;
else
  input_windowing = input_data;
end


//fft process
res_cmp = fft(input_windowing);
//res_cmp = fft(input_data);
res_abs = abs(res_cmp)*(2/input_length);
res_lnr = res_abs/window_adjust_coef;

//convert dB unit
res_db = 20 * log10(res_lnr);

if out_form == 'dB'  
  result = res_db;
elseif out_form == 'lnr'
  result = res_lnr;
elseif out_form == 'abs'
  result = res_abs;
elseif out_form == 'cmp'
  result = res_cmp;
else
  result = res_db;
end

amp = result(1:out_length);

//generate frequency 
freq_points = [0:1:out_length-1];
frequency = (fs/input_length) * freq_points; 

printf("==================");

endfunction


