function note = f2note(freq);

% convert frequency to note name.
% frequency is rounded to nearest note according to mean scale
%	note = f2note(freq);
%       note : note name (ex. 'eis3', 'a5')
%       freq : frequency in Hz
%    ex.) f2note(440) return 'a4'
%
%   see also note2f

if nargin < 1
  help f2note;
  return;
end

notename = ['ais'; 'b  '; 'c  '; 'cis'; 'd  '; 'dis'; 'e  '; 'f  '; 'fis'; 'g  '; 'gis'; 'a  '];

% a4 を基準とした音程
fnum = round(12*log(freq/440)/log(2));
if(fnum < 0)
  fnum = 0;
end

notechar = notename(rem(fnum+11, 12)+1,:);
if(notechar(2) == ' ')
  notechr = notechar(1);
else
  notechr = notechar;
end

notec = 4 + floor((fnum+11)/12);

note = sprintf('%s%d', notechr, notec);

