I have discovered a method to measure the similarity of two successive musical notes which I wanted to share with a question:
It is known in music theory that two successive pitches $a,b$ which sound “good” or “nice” if some ratio $B/A$ is “simple”. The notion of simplicity has not been defined precisely, and I will give a possible notion here:
Let $\alpha = 12^{\frac{1}{12}}$, $p_1 = \alpha^{k_1},p_2=\alpha^{k_2}$ where $0 \le k_1,k_2 \le 127$ are the midi pitches. We define the similarity between $p_1$ and $p_2$ to be: $$K_p(k_1,k_2) = \frac{\gcd(a,b)^2}{ab}$$ where $a = $ numerator of a rational approximation of $\alpha^{k_1-k_2}$ and $b = $ denominator of a rational approximation of $\alpha^{k_1-k_2}$. We argue that this similarity could capture when two pitches have a "simple" ratio and hence will sound "nice" together or when played in successive order. We look at the following matrix:
$$ \left(\begin{array}{rrrrrrrrrrrr} 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 \\ 1 & \frac{16}{15} & \frac{9}{8} & \frac{6}{5} & \frac{5}{4} & \frac{4}{3} & \frac{17}{12} & \frac{3}{2} & \frac{8}{5} & \frac{5}{3} & \frac{16}{9} & \frac{15}{8} \\ 1 & \frac{1}{240} & \frac{1}{72} & \frac{1}{30} & \frac{1}{20} & \frac{1}{12} & \frac{1}{204} & \frac{1}{6} & \frac{1}{40} & \frac{1}{15} & \frac{1}{144} & \frac{1}{120} \end{array}\right) $$
In the first row is the pitch difference $k_1-k_2$. In the second row is a rational approximation of $\alpha^{k_1-k_2}$. In the last row is the similarity measure $K_p(k_1,k_2)$ where $k_2=0$. This similarity measure goes from $0$ to $1$. A larger value indicates a larger similarity. We sort the matrix above by the third row, similarity measure, to look how the rational approximation changes:
$$ \left(\begin{array}{rrrrrrrrrrrr} 0 & 7 & 5 & 9 & 4 & 3 & 8 & 2 & 11 & 10 & 6 & 1 \\ 1 & \frac{3}{2} & \frac{4}{3} & \frac{5}{3} & \frac{5}{4} & \frac{6}{5} & \frac{8}{5} & \frac{9}{8} & \frac{15}{8} & \frac{16}{9} & \frac{17}{12} & \frac{16}{15} \\ 1 & \frac{1}{6} & \frac{1}{12} & \frac{1}{15} & \frac{1}{20} & \frac{1}{30} & \frac{1}{40} & \frac{1}{72} & \frac{1}{120} & \frac{1}{144} & \frac{1}{204} & \frac{1}{240} \end{array}\right) $$
We see in the matrix above that a perfect $7$-th is more "consonant" by the above definition than the perfect $5$-th. The most "dissonant" is a half-tone difference realised by $1$ being the last number in the first row.
Since a pitch alone does not describe a note, we have also defined similarity measures for duration, volume and if it is a rest or not:
Herefore we make use of the Jaccard-similarity of two intervals:
$$J(A,B) = \frac{\mu(A \cap B)}{\mu(A \cup B)}$$
where $A = [0,a],B = [0,b]$ are closed intervals and $a,b>0$ and $\mu([x,y]) = y-x$.
Using $J$ we define the duration similarity:
$$K_d(d_1,d_2) = J([0,d_1],[0,d_2])$$
for two durations $d_1,d_2$ given as multiple of quarter notes. And similarily we define the volume similarity as :
$$K_v(v_1,v_2) = J([0,v_1],[0,v_2])$$
for $0 \le v_1,v_2 \le 127$ giving the volumes in midi notation. For rests we take the similarty $=0$ if one is not a rest and the other is, or $=1$ if both are no rests or both are rests.
Having two notes $n_1 = (p_1,d_1,v_1,r_1),n_2 = (p_2,d_2,v_2,r_2)$ we define a similarity between them as:
$$K(n_1,n_2) = \alpha_p K_P(p_1,p_2) +\alpha_d K_d(d_1,d_2) + \alpha_v K_v(v_1,v_2) + \alpha_r K_r(r_1,r_2)$$
where $\alpha_p+\alpha_d+\alpha_v+\alpha_r=1$ and $0<\alpha_x<1$ are weights.
The mathematical properties of this similarity measure are also nice and can be proven. We can use this similarity measure to define a distance between two notes:
$$d(n_1,n_2) = \sqrt{2(1-K(n_1,n_2))}$$
This has the advantage of using the nearest neighbors algorithm in generating music. To capture similarities between fixed length sequences of notes, one could define the sum of the similarites:
$$K_S((n_1,\cdots,n_s),(N_1,\cdots,N_s)) = \frac{1}{s}\sum_{i=1}^s K(n_i,N_i)$$
This could be useful for measuring consonance of two melodies or so. The algorithm we propose starts with a single note for a voice and keeps adding nearest neighbor notes sorted by distance, with the last note in a sequence of neighbors, becoming again the first note etc.
Here is an example done for two pianos with the method described above:
youtube: https://www.youtube.com/watch?v=UjQWw-eWtZQ
bandcamp: https://musescore1983.bandcamp.com/track/knn-for-two-pianos-and-four-seeds
audio & score: https://musescore1983.gumroad.com/l/eVHvR
Question: Are there other positive definite kernels over the natural numbers, which can capture the perceived similarity / simple ratios?