image thumbnail
from Piano notes recognition research by Maxim Vedenyov
try to define notes from its sound

n=note2number(sm)
function n=note2number(sm)
% converts note symbol sm to number n

switch lower(sm(1)) % check first symbol - note
    case 'a'
        n=0;
    case 'b'
        n=2;
    case 'c'
        n=3;
    case 'd'
        n=5;
    case 'e'
        n=7;
    case 'f'
        n=8;
    case 'g'
        n=10;
    otherwise
        error(['unknown note sign: ' sm(1)] );
end

if length(sm)>1 % if more then one letter
    % get octave number if any:
    on=str2num(sm(2)); % str2num givs empty if it was not number
    if isempty(on) % if empty then octave number was not specified then it is 4
        on=4;
    end
    n=n+(on-4)*12; % update note number
    
    % sharp or flat:
    switch sm(end)
        case '#'
            n=n+1;
        case 'b'
            n=n-1;
    end
else
    on=4; % if octave number was not specified then it is 4
    n=n+(on-4)*12; % update note number
end

Contact us at files@mathworks.com