matlablogarithmexponentialpitch-detection

MATLAB semilog scaling


I'm new to MATLAB, and am working on a program that deals with frequency of the human voice based on microphone input. The biggest problem I am running into is that musical notes (what I am dealing with in this project) increase in frequency exponentially, about 1.059463^x for each semitone in the musical scale.
In the program I am dealing with, I need to both scale the graph so the detected frequency is close to the note number it corresponds to as well as scale the data so that I can work with the note numbers in terms of notes and musical cents so the frequency graph can be converted easily to MIDI data.
The only other option I have found has been to create a library of frequencies for the recorded frequencies to be compared to, but that is unnecessarily complicated and time consuming.
So, in essence, I am trying to scale the data so that A2, with a frequency of 110Hz, would correspond to its note number 45. Is there a way to do this?


Solution

  • I think this does what you want:

    f = 110; %// frequency in Hz
    n = log10(f/440)/log10(2)*12+69; %// 440 Hz (note A4) is note 69
    note = round(n);
    cents = round((n-note)*100);
    

    Examples: f = 110 gives

    note =
        45
    cents =
         0
    

    f = 345 gives

    note =
        65
    cents =
       -21
    

    in accordance with this reference and this converter.