annotate aim-mat/tools/fre2note.m @ 4:537f939baef0 tip

various bug fixes and changed copyright message
author Stefan Bleeck <bleeck@gmail.com>
date Tue, 16 Aug 2011 14:37:17 +0100
parents 20ada0af3d7d
children
rev   line source
tomwalters@0 1 % support file for 'aim-mat'
tomwalters@0 2 %
bleeck@3 3 % (c) 2011, University of Southampton
bleeck@3 4 % Maintained by Stefan Bleeck (bleeck@gmail.com)
bleeck@3 5 % download of current version is on the soundsoftware site:
bleeck@3 6 % http://code.soundsoftware.ac.uk/projects/aimmat
bleeck@3 7 % documentation and everything is on http://www.acousticscale.org
bleeck@3 8
tomwalters@0 9
tomwalters@0 10 function [str,oct,nr_note]=fre2note(fre)
tomwalters@0 11
tomwalters@0 12 % translates the fre to the nearest real note in a-g
tomwalters@0 13
tomwalters@0 14 if fre==0
tomwalters@0 15 str='no';
tomwalters@0 16 oct=0;
tomwalters@0 17 nr_note=-1;
tomwalters@0 18 return
tomwalters@0 19 end
tomwalters@0 20
tomwalters@0 21 note_names=['A ';'A#';'B ';'C ';'C#';'D ';'D#';'E ';'F ';'F#';'G ';'G#'];
tomwalters@0 22 lowest_note=27.5; % Hz =A1 440 Hz= a4
tomwalters@0 23
tomwalters@0 24 for i=1:length(fre)
tomwalters@0 25 if fre(i)>0
tomwalters@0 26 cent(i)=fre2cent(lowest_note,fre(i));
tomwalters@0 27 % how many octaves?
tomwalters@0 28 nr_octs(i)=round(cent(i)/1200)-1;
tomwalters@0 29 oct(i)=nr_octs(i)+1;
tomwalters@0 30
tomwalters@0 31 % how many tones?
tomwalters@0 32 cento(i)=mod(cent(i),1200)/100;
tomwalters@0 33 nr_note(i)=round(cento(i)+1);
tomwalters@0 34 if nr_note(i)>12
tomwalters@0 35 nr_note(i)=1;
tomwalters@0 36 end
tomwalters@0 37 % nr_note(find(nr_note>12))=1;
tomwalters@0 38 else
tomwalters@0 39 nr_note(i)=0;
tomwalters@0 40 end
tomwalters@0 41 end
tomwalters@0 42 % compansate for that the octave changes at C
tomwalters@0 43 for i=1:length(fre)
tomwalters@0 44 if nr_note(i) > 3 && nr_note(i) < 7
tomwalters@0 45 nr_octs(i)=nr_octs(i)+1;
tomwalters@0 46 end
tomwalters@0 47 end
tomwalters@0 48
tomwalters@0 49 for i=1:length(fre)
tomwalters@0 50 if nr_note(i)==0
tomwalters@0 51 str{i}=['00'];
tomwalters@0 52 elseif strcmp(note_names(nr_note(i),2),' ')
tomwalters@0 53 if nr_octs(i)<0
tomwalters@0 54 str{i}=[note_names(nr_note(i)) '0'];
tomwalters@0 55 else
tomwalters@0 56 str{i}=[note_names(nr_note(i)) num2str(nr_octs(i))+1];
tomwalters@0 57 end
tomwalters@0 58 else
tomwalters@0 59 if nr_octs(i)<0
tomwalters@0 60 str{i}=[note_names(nr_note(i),1:2) '0'];
tomwalters@0 61 else
tomwalters@0 62 str{i}=[note_names(nr_note(i),1:2) num2str(nr_octs(i))+1];
tomwalters@0 63 end
tomwalters@0 64 end
tomwalters@0 65 end