Home > v_mfiles > v_frq2midi.m

# v_frq2midi

## PURPOSE

V_FRQ2MIDI Convert frequencies to musical note numbers [N,T]=(F)

## SYNOPSIS

function [n,t]=v_frq2midi(f)

## DESCRIPTION

```V_FRQ2MIDI Convert frequencies to musical note numbers [N,T]=(F)
notes are numbered in semitones with middle C being 60
Note 69 (the A above middle C) has a frequency of 440 Hz.
These note numbers are used by MIDI. Note numbers are not necessarily
integers.

t is a text representation of the note in which
C4# denotes C sharp in octave 4. Octave 4 goes
from middle C up to the B above middle C. For the white
notes on the piano, the third character is a space.

Negative frequencies are equivalent to positive frequencies
except that flats will be used instead of sharps. Thus
C4# would become D4-

see MIDI2FRQ for the inverse transform```

## CROSS-REFERENCE INFORMATION

This function calls:
This function is called by:

## SOURCE CODE

```0001 function [n,t]=v_frq2midi(f)
0002 %V_FRQ2MIDI Convert frequencies to musical note numbers [N,T]=(F)
0003 % notes are numbered in semitones with middle C being 60
0004 % Note 69 (the A above middle C) has a frequency of 440 Hz.
0005 % These note numbers are used by MIDI. Note numbers are not necessarily
0006 % integers.
0007 %
0008 % t is a text representation of the note in which
0009 % C4# denotes C sharp in octave 4. Octave 4 goes
0010 % from middle C up to the B above middle C. For the white
0011 % notes on the piano, the third character is a space.
0012 %
0013 % Negative frequencies are equivalent to positive frequencies
0014 % except that flats will be used instead of sharps. Thus
0015 % C4# would become D4-
0016 %
0017 % see MIDI2FRQ for the inverse transform
0018
0019
0020
0021
0022 %      Copyright (C) Mike Brookes 1998
0023 %      Version: \$Id: v_frq2midi.m 10865 2018-09-21 17:22:45Z dmb \$
0024 %
0025 %   VOICEBOX is a MATLAB toolbox for speech processing.
0027 %
0028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0029 %   This program is free software; you can redistribute it and/or modify
0031 %   the Free Software Foundation; either version 2 of the License, or
0032 %   (at your option) any later version.
0033 %
0034 %   This program is distributed in the hope that it will be useful,
0035 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0036 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0037 %   GNU General Public License for more details.
0038 %
0039 %   You can obtain a copy of the GNU General Public License from
0040 %   http://www.gnu.org/copyleft/gpl.html or by writing to
0041 %   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
0042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0043
0044 n=(69+12*log(abs(f)/440)/log(2));
0045 if nargout > 1
0046   m=round(n(:));
0047   o=floor(m/12)-1;
0048   m=m-12*o+6*sign(f(:))-5;
0049   a=('CDDEEFGGAABBCCDDEFFGGAAB')';
0050   b=(' - -  - - -  # #  # # # ')';
0051   t=setstr([a(m) mod(o,10)+'0' b(m)]);
0052 end```