function [note_name] = note_labels(freq)
% function [note_name] = note_labels(freq);
% Returns note name given a frequency
% The frequency variable is matched against a list of frequencies
% that is generated by taking a basis frequency of 110 Hz (a1) and
% computing all higher notes using the formula 110 * 2^(note_number/12)

% Written 08/08/96 by Petr Janata
% 04/09/05 PJ A1=55 Hz -- American notenames

name_list = ['a  '
             'ash'
             'b  '
             'c  '
             'csh'
             'd  '
             'dsh'
             'e  '
             'f  '
             'fsh'
             'g  '
             'gsh'];

% Create the index of notes

the_notes = 27.5*2 .^ ((0:108)/12);  % Generate 8 octaves of notes

diffs = abs(the_notes-freq);
note_index = find(fix(diffs*1000)/1000 <= 1E-3);

if isempty(note_index)
  sprintf('Could not match frequency (%3.3f) to note name\n', freq)
  note_name = '';
  return
end

octave_offset = fix((note_index-1)/12)+1;
range_str = int2str(octave_offset);
 
note_offset = rem((note_index-1),12)+1;

note_name = [deblank(name_list(note_offset,:)) range_str];

return

