# A Hardware-Oriented Algorithm for Real-Time Music Key Signature Recognition
## 2 Materials and Methods  
### Theoretical Foundations  
In general, a musical piece comprises tones belonging to a group of 12 pitch classes, namely: C, C\(\sharp\)/D\(\flat\), D, D\(\sharp\)/E\(\flat\), E, F, F\(\sharp\)/G\(\flat\), G, G\(\sharp\)/A\(\flat\), A, A\(\sharp\)/B\(\flat\), and B. Let us assume that x represents the set of multiplicities corresponding to each of the pitch classes, depicting the total distribution of a given fragment of music (1):  
\[x=\left\{x_{C},\ x_{C\sharp/D\flat},\ x_{D\sharp/E\flat},\ x_{E},\ x_{F},\ x_{F \sharp/G\flat},\ x_{G},\ x_{G\sharp/A\flat},\ x_{A},\ x_{A\sharp/B\flat},\ x_{B}\right\} \tag{1}\]  
Knowing the multiplicities of sounds representing the particular pitch classes, one can calculate the normalized multiplicities using the following formula [28]:  
\[k_{i}\ =\frac{x_{i}}{x_{max}} \tag{2}\]  
where \(i\in[\)C, C\(\sharp/D\flat\), D, D\(\sharp/E\flat\), E, F, F\(\sharp/G\flat\), G, G\(\sharp/A\flat\), A, A\(\sharp/B\flat\), B\(]\) and  
\[x_{max}=max\ \left\{x_{C},\ x_{C\sharp/D\flat},\ x_{D\sharp/E\flat},\ x_{E},\ x_{F },\ x_{F\sharp/G\flat},\ x_{G},\ x_{G\sharp/A\flat},\ x_{A},\ x_{A\sharp/B\flat}, \ x_{B}\right\}\]  
By sorting the obtained normalized multiplicities in accordance with the succession of the pitch classes inscribed in the circle of fifths, starting from A and assuming a positive angle of rotation, the following vector of normalized multiplicities can be defined (3) [27]:  
\[\mathbf{K}=[k_{A}\ k_{D}\ k_{G}\ k_{C}\ k_{F}\ k_{B\flat}\ k_{E\flat}\ k_{A \flat}\ k_{D\flat}\ k_{F\sharp}\ k_{B}\ k_{E}] \tag{3}\]  
**Definition 1**. : _The music signature is a set of twelve vectors \(\left\{\overset{\rightarrow}{k_{i}}\ :i=A,\ D,\ G,\ \ldots,\ E\right\}\) whose polar coordinates \((r_{i},\ \phi_{i})\) are determined using the following assumptions [28]:_  
* _the length of each vector is equal to the normalized multiplicity of a given pitch class, i.e.,_ \(r_{i}=\left|\overset{\rightarrow}{k_{i}}\right|=k_{i}\)_,_
* _the direction of the vector is determined with the following relationship:_ \(\phi_{i}=j\cdot 30^{\circ}\) _, where_ \(j=0|_{i=A},\ j=1|_{i=D},\ \text{and so on}\)_._  
**Example 1**. : _Creation of a music signature._  
Let us create a music signature for the fragment of the musical piece shown in Figure 1 (music notation obtained from the MIDI files does not contain any key signatures). The following is the list of multiplicities corresponding to the characteristic sounds inscribed in the circle of fifths, obtained for the considered fragment of music: \(x_{A}=8\), \(x_{D}=10\), \(x_{G}=6\), \(x_{C}=0\), \(x_{F}=0\), \(x_{B\flat}=1\), \(x_{E\flat}=0\), \(x_{A\flat}=0\), \(x_{D\flat}=4\), \(x_{F\sharp}=4\), \(x_{B}=1\), and \(x_{E}=5\). One can easily notice that the maximum value in this series is associated with the sound D, i.e., \(x_{max}=x_{D}=10\); therefore, vector K is defined by the following coordinates: \(k_{A}=0.8\), \(k_{D}=1\), \(k_{G}=0.6\), \(k_{C}=0\), \(k_{F}=0\), \(k_{B\flat}=0.1\), \(k_{E\flat}=0\), \(k_{A\flat}=0\), \(k_{D\flat}=0.4\), \(k_{F\sharp}=0.4\), \(k_{B}=0.1\), and \(k_{E}=0.5\), and can be expressed as \(\mathbf{K}=[0.8\ 1.0\ 0.6\ 0\ 0\ 0.1\ 0\ 0\ 0.4\ 0.4\ 0.1\ 0.5]\). For example, note A appears once in the first bar, twice in the second bar, three times in the third bar, and twice in the fourth bar, which results in: \(x_{A}=8\). The successive multiplicities of the other pitch classes are determined in the same way. Figure 2 illustrates the music signature obtained for the analyzed fragment of the musical piece. Figure 1: Music notation representing the analyzed fragment of an exemplary piece of music. As illustrated in Figure 2, the music signature can be expressed geometrically. Let us develop this idea a bit further and assume that \(Y\to Z\) denotes the directed axis of the circle of fifths, which connects two opposite symbols from the twelve sounds of the chromatic scale, i.e., \(Y\), \(Z\in\{\)C, G, D, A, E, B, F, D, E, A, B, F, F\(\}\).