Melodies as Maximally Disordered Systems under Macroscopic Constraints with Musical Meaning

One of the most relevant features of musical pieces is the selection and utilization of musical elements by composers. For connecting the musical properties of a melodic line as a whole with those of its constituent elements, we propose a representation for musical intervals based on physical quantities and a statistical model based on the minimization of relative entropy. The representation contains information about the size, location in the register, and level of tonal consonance of musical intervals. The statistical model involves expected values of relevant physical quantities that can be adopted as macroscopic constraints with musical meaning. We studied the occurrences of musical intervals in 20 melodic lines from seven masterpieces of Western tonal music. We found that all melodic lines are strictly ordered in terms of the physical quantities of the representation and that the formalism is suitable for approximately reproducing the final selection of musical intervals made by the composers, as well as for describing musical features as the asymmetry in the use of ascending and descending intervals, transposition processes, and the mean dissonance of a melodic line.


Introduction
Many quantitative analyses in music have been carried out using different elements as building blocks, or "units of context", which allow the message of a musical piece to be apprehensible at different time scales [1]. Common choices for these "units of context" are single pitches (ignoring or taking into account the chroma properties [2,3]), single musical notes (i.e., pitch and rhythm values), pairs of pitches or musical intervals (either harmonic or melodic), triplets of pitches between contiguous notes, and chords [1][2][3][4][5][6][7][8][9]. In the case of musical intervals (from now on referred to as intervals), quantitative analyses frequently employ parameters that describe their psychoacoustic properties, such as the sizes of intervals (commonly measured in tones or semitones), the ratio of the fundamental frequencies of both pitches (commonly measured in units of cents), and the difference between the fundamental frequencies [10,11].
Analyses based on statistical methods can capture information about musical features, such as the style of a musical piece, the composer, and even the emotions conveyed [1,[3][4][5][6][7][8][9][12][13][14][15][16][17][18][19][20]. Several statistical analyses employ successive pitches as units of context. George Kingsley Zipf studied the frequency of occurrences of melodic intervals in masterpieces of Western music, and he reported that the frequency of occurrences of ascending and descending intervals is almost inversely proportional to their size [4]. Vos and Trost studied music from 13 great composers of Western academic music, the Beatles, and folk music, finding that the proportion between musical interval sizes is too complex to be represented by a simple exponential or power law function. They also reported an asymmetry in the use of ascending and descending intervals [5]. Gunnar Niklasson and Maria Niklasson studied the occurrences of melodic intervals as a function of their size, finding long-tailed Levy-stable distributions that they associated to a "music walk" between successive pitches, in analogy with a random walk [19].
In the framework of a network analysis, Liu, Small, and Tse studied the connectivity properties of complex networks representing the successive notes of musical pieces, finding scale-free behavior in the nodal degree for several sets of academic and popular music [8].
From the information theory perspective, there are many works devoted to analyzing, classifying, and generating music [21][22][23][24][25][26][27][28][29]. A seminal work by Cohen establishes the basic concepts for applying information theory to music [30]. In this approach, entropy has been used to measure the amount of information conveyed to a listener in a sequence of events organized in time [27]. Many works have used this concept in music, for example: Pinkerton wondered how entropy must be measured in melodies and how large it should be [21]; Youngblood [22], and Knopoff and Hutchinson [25] used entropy in order to identify musical styles. Manzara, Witten, and James used entropy to characterize the short-and long-term structures in chorales of J. S. Bach. [27]. Dubnov, Assayag, and El-Yaniv used entropy to characterize sequences of pitches in terms of their statistical source coding, and to generate aleatoric melodies [26]. Güngör Gündüz and Ufuk Gündüz studied the evolution of the entropy associated to the transitions between pitches during the progress of a melody, and they found that it increases up to a limiting value, which is smaller than the entropy of a random melody [28]. Cohen posted various criticisms about the application of information theory in music [30]. For example: Markov sources cannot generate some sequences found in music, the assumption concerning that a probability distribution corresponds to the listener's expectations is difficult to hold as the expectations change dynamically through the musical piece, the ergodicity of the source should be considered only as an approximation, the impossibility of an infinite memory capacity in the listener must be taken into account, and finally, the assumption concerning the stationarity of the source is difficult to prove [30].
With respect to the melodic interval size, David Huron carried out a study of nearly 10,000 Western musical themes, finding that the average melodic interval size is slightly smaller in pieces written in a minor mode than in those written in a major mode. This result was interpreted by the author as a relation between sadness and small values for the average melodic interval size [6]. Huron also found that themes in a minor mode have slightly lower pitches on average in comparison with major ones [14], which suggests that the sizes of intervals and their locations in the register are important for conveying musical information.
The concept of interval size captures relevant information on musical features. However, it misses information concerning the locations of intervals in the register and, hence, on the level of tonal consonance [11] and musical processes, such as the transposition [31]. Here, we propose a representation of intervals that contains information on their sizes and locations in the register. This representation utilizes the fundamental frequencies of pitches and relates the tonal consonance properties of intervals to the work carried out by the composer choosing the intervals for a piece. This formalism is employed to study melody through the analysis of 20 melodic lines from seven masterpieces of Western tonal music, including the development of a theoretical model based on the relative entropy extremalization that reproduces the main features observed in real melodic lines. This paper is organized as follows. Section 2 presents the microscopic representation of intervals and describes how to construct their macroscopic observables. Section 3 describes how to measure levels of tonal consonance using the representation proposed. Section 4 introduces macroscopic observables in melody. Sections 5 and 6 present an application to real melodic lines and a statistical model that reproduces the main experimental findings. The final section presents the conclusions.

Microscopic Representation and Macroscopic Observables of Intervals
This section presents the microscopic representation of musical intervals using physical quantities, the expected values of the relevant quantities, the mathematical description of transposition processes in this representation, and an analysis of distinguishability of intervals.

Interval Size and Its Relation to the Fundamental Frequency of Pitches
Many musical systems employ discrete sets of sounds produced by musical instruments, which are usually grouped into musical scales. Frequently, these sets of sounds are selected in such a way as to yield a large number of consonant combinations when two or more elements are produced together [10]. Pythagoras posted the first known mathematical rule to produce musical scales when he found that two sounds emitted simultaneously by vibrating strings of equal tension and density produce a pleasant sensation when the ratio between their lengths (l i /l j ) and, hence, between their fundamental frequencies ( f j / f i ) corresponds to the ratio between two small natural numbers l i /l j = f j / f i = n/m [10,32,33]. The frequency ratio is the first known parameterization of consonance in terms of physical quantities. Two well-known scales based on the Pythagoras rule are the just and the Pythagorean [10,32].
Ordering the R pitches produced by a musical instrument tuned to a particular musical scale from the lowest to the highest fundamental frequency leads to a collection of pitches The interval size L associated to a pair of pitches f i and f j is defined for many musical scales as L ≡ L( f i , f j ) = j − i. The magnitude of L determines the plain distance between pitches, and its sign is meaningful for successive pitches, distinguishing the chronological order of their appearances. Intervals with the same size L can be produced in different locations of the register. In analogy with the concept of degeneracy used in physics, this quantity can be considered as degenerated with a value that is equal to the total number of such intervals. For complex tones, such as the sounds produced by musical instruments that can be described as a superposition of several pure tones, Plomp and Levelt found that an interval with a given frequency ratio f j / f i might be more or less consonant depending on its location in the register [11]. In many musical cases, there is a one-to-one correspondence between L and f j / f i , for example, in an equal-tempered system.
The Pythagoras rule can be expressed as the frequency difference: where for the just and Pythagorean scales, the quantity (n − m)/(n + m) depends on the size of the interval L (see Figure 1). The 12-tone equal-tempered (12-TET) scale belongs to the equal tempered system and has been widely utilized in Western tonal music. This system is based on a different mathematical rule, where h is a natural number (h = 12 for the 12-TET) and f 1 is a reference frequency. In this system, the frequency ratio is given by: and an equivalent expression to (1) is: Equation (3) approximately holds for the just and Pythagorean scales, taking (n − m)/(n + m) = (2 L/b − 1)/(2 L/b + 1) and using the most common values of n and m related to each interval size L in the just and Pythagorean scales (see Supplementary Table S1) [32], then for a register with 88 pitches, the obtained fit parameters are as follows:
The expression (2 L/b − 1)/(2 L/b + 1) can be written as a linear function of L in a broad region, see Figure 1. The second-order term of the Taylor expansion around L = 0 vanishes, and the first-order term leads to (2 L/b − 1)/(2 L/b + 1) ≈ cL, with c = (ln2)/(2b).
In many musical cases, the sizes of intervals are smaller than or equal to two octaves, such as in the case of melodic intervals in typical melodic lines [31]. For the case that −24 ≤ L ≤ 24, the fit parameters are given as follows: • For the just scale, c = 2.632 × 10 −2 ± 1.52 × 10 −4 with a determination coefficient For the Pythagorean scale, c = 2.642 × 10 −2 ± 1.55 × 10 −4 with R 2 = 0.998; • For the 12-TET scale, c = 2.635 × 10 −2 ± 1.48 × 10 −4 with R 2 = 0.998. With these results, Equations (1) and (3) can be expressed as: where X = ( f j + f i )/2 is the center frequency, which provides information about the location of an interval in the register [10]. Then, f j − f i is proportional to the product of the interval size L and its corresponding location in the register X, lifting the degeneration associated to the fact that intervals of the same size might be produced in different locations of the register.

Expected Values with Musical Meaning
Let us suppose that in a musical piece, the probability associated to the frequency of occurrence of each interval of size L is known to be {p L } with ∑ L p L = 1. If the probability p L is related to simultaneous pitches, then L can be defined as |L| ≡ |L( f i , f j )| = |j − i|.
Probability distributions (PD) allow us to obtain macroscopic quantities related to specific properties of musical pieces. For example, the average magnitude of the interval size is given by: Frequently, different musical instruments have different registers. However, Equation (5) does not capture this information, for example, in a transposition process that moves a set of intervals from one part of the register to another. The expected value of the frequency difference captures information about the locations of intervals in the register: where N is the total number of intervals, is the sum of the frequency differences of intervals grouped by their size, and N L min , ..., N Lmax are the total numbers of intervals of each size L. Taking p L = N L /N as the probability of finding an interval of size L, the expected value is: where | f j − f i | L is the mean value of the frequency differences for a set of intervals of size L.
The linear approximation leads to: where L = L X L is an effective size containing information about the contribution of the average location in the register. Equation (8) can be considered as an extension of Equation (5) when the average position in the register of each type of interval size X L is taken into account. Notice that if all intervals have the same average position in the register X C , then the expected value | f j − f i | is proportional to the expected value |L| , being given by | f j − f i | ≈ 2cX C |L| . Equation (8) shows that the expected value associated to the frequency differences takes into account the mean location in the register of intervals. However, the diversity of locations in the register for the same interval does not contribute to the expression (8). A quantity that takes into account this diversity can be constructed from Equation (4) as: From the physics perspective, this quantity is proportional to the difference in the average energy densities j − i for two harmonic waves with equal amplitudes T propagating in a medium with density ρ [34]: The expected value of the quantity f 2 j − f 2 i in Equation (9) can be written as: where the term σ L 2 represents the dispersion of the intervals of size L in the register (measured as a variance) with respect to the average position X L , and L = L X 2 L + σ 2 L is an effective size that takes into account the contribution of the average location of intervals in the register as well as their dispersion. Equation (11) can be considered as an extension of Equation (8), when the contribution from the dispersion in the locations of the intervals is taken into account. In the case of just one possible location in the register for each kind of interval of size L, σ L 2 = 0. In addition, if the average positions in the register for intervals of different sizes are close to each other and they are located around the position X C , then the first-order term of the Taylor expansion around X C leads to

Transposition Process
In a transposition process, the set of probabilities {p L } remains unvaried when the location of the intervals in the register is moved from the original one X O L to a new one X N L . These locations are related as: where f O refers to any fundamental frequency in the original location, f N is the corresponding frequency in the new location, and w is the interval of the transposition. While the observable |L| remains unchanged after the transposition process, | f j − f i | changes as follows: (σ 2 Because X 2 L also scales with w 2 , in a transposition process, the ratio X 2 L /σ 2 L remains unchanged, and the expected value | f 2 j − f 2 i | scales as:

Distinguishability of Pairs of Pitches
So far, it has been shown that the quantities f j − f i and f 2 j − f 2 i distinguish between intervals of the same size in different locations in the register (Equations (4) and (9)). Figure 2 illustrates the dependence of f j − f i and f 2 j − f 2 i on the magnitude of the interval size |L| for the 12-TET scale tuned with A = 440 Hz. Considering the orders of magnitude of the values and the relative separations between branches, this figure indicates that the quantity f 2 j − f 2 i has a better resolution than f j − f i for distinguishing intervals of equal size in different locations of the register.
The distinguishability of intervals of different sizes in different locations of the register is not evident. The general problem can be formulated independently of the musical scale and the particular tuning as follows: If two pairs of different pitches { f i , f j } and { f r , f s } produce the same frequency difference or the same difference in the squares of the frequencies, then: for f j > f i (i.e., j > i) and f s > f r (i.e., s > r).
When f j < f i and f s < f r , Equation (16) can be transformed into a positive equality by changing the index order i ↔ j; s ↔ r. Then, both cases are equivalent. The solution of these equations can be formulated in terms of the frequency ratio of the fundamental frequencies, instead of the specific values of the frequencies. In many cases, as for example in the just, Pythagorean, and 12-TET musical scales, the frequency ratios α for each musical scale are known. Taking (16) can be written as: Then, for i > r and i < r, f i = α (i−r) . f r and f r = α (r−i) . f i , respectively. Therefore, Equation (17) can be written for the frequency difference as: and for the difference between the squares of the frequencies as: For the frequency difference, if at least one of the equations presented in (18) is satisfied, then there are several pairs of fundamental frequencies with the same frequency difference. For the difference in the squares of the frequencies, if at least one of the equations presented in (19) is satisfied, then several pairs of fundamental frequencies have the same difference in the squares of the frequencies. We call the set of equations given in (18) and (19) the "degeneracy equations" of musical intervals.
The number of combinations of α ratios satisfying the degeneracy equations depends on the precision in the measurement of the α ratios. Table 1 shows the number of combinations of α ratios satisfying the degeneracy equations as a function of the number of decimal places d used to measure these ratios (1 ≤ d ≤ 10). See Supplementary Note S1. Two possible situations are considered: Intervals up to two octaves, for which it is possible to interpret the quantities f j − f i and f 2 j − f 2 i as proportional to the interval sizes; and the case of all possible intervals on an 88-pitch musical instrument, such as a traditional piano.
For intervals with size L up to two octaves (L max = 24 semitones), the number of possible combinations of the α and α 2 ratios is 24 × 24 × 23 = 13,248. For intervals with sizes up to 87 semitones (corresponding to an 88-pitch musical instrument), the number of possible combinations is 87 × 87 × 86 = 650,934. Table 1. Number of combinations of the α ratios that satisfy the degeneracy Equations (18) and (19) as a function of the precision of the α ratios, given in terms of the number of decimal places d. Results for 1 ≤ d ≤ 10.

Up to 24 Semitones
Up to 87 Semitones In the 12-TET scale, the quantity f j − f i distinguishes each pair of different pitches when the degeneracy is lifted by rounding the value of the α ratio to d ≥ 5 for the 24 and 87 semitones cases. The quantity f 2 j − f 2 i lifts the degeneracy for d ≥ 4 in the case of 24 semitones, and for d ≥ 8 in the case of 87 semitones (see Table 1). In the Pythagorean scale, the degeneracy of the quantity f 2 j − f 2 i can only be lifted for the 24 semitones case, taking d ≥ 5, and the degeneracy of f j − f i cannot be lifted with up to 10 decimal places (d = 10) (see Table 1). In the just scale, the degeneracy remains up to d = 10 for both quantities and in both cases (24 and 87 semitones) (see Table 1).
In some cases, the degeneracy equations are satisfied independently of the precision used to measure the α ratios. For example, in the case of the quantity f 2 j − f 2 i for the just scale, the combination of α (s−r) = 5/3 and α (j−i) = 5/4 produces α (i−r) = 16/9, i.e., the major thirds (5/3) produce equal values to major sixths (5/4) when the lowest pitches of each of these intervals generate minor sevenths (16/9). Summarizing, whenever it is possible to lift the degeneracy, this can be done through the precision of the α ratios, which depends on the precision in the measurement of the fundamental frequencies. See Supplementary Note S2.

Connection with Tonal Consonance
This section shows the connection between the representation of musical intervals previously presented and the tonal consonance formalism.

Measuring the Dissonance Levels of Intervals
The consonance and dissonance sensations experienced by listeners are related with the perception of pleasantness or unpleasantness produced by a combination of sounds. This sensation is fundamental in music because it is present in timbre, harmony, melody, and musical tuning [35][36][37].
The frequency difference is widely used to determine the dissonance level of a pair of pure tones sounded together [11]. In addition, this difference contains information about the interval size and its corresponding location in the register. Various models have been proposed that use the frequency difference to determine the dissonance levels of pure and complex tones sounded together [35,36,[38][39][40]. One of the most recent approaches was developed by Vassilakis [39,40], which modified a model proposed by Sethares [35,38]. The model includes the dependence of the roughness on the intensity, amplitude fluctuation degree, and amplitude fluctuation rate [40]. In this model, the dissonance level δ produced by two pure tones with frequencies f i and f j and amplitudes a i and a j , respectively, is given by: .75, and s = 0.24/(0.0207 f min + 18.96).
Musical instruments produce complex tones composed of pure tones. The dissonance level D of two simultaneous complex tones with the same timbre, as in the case of a harmonic interval, can be calculated using Equation (20), taking into account the contributions of all individual dissonances δ generated from all possible combinations of pure tones in the superposition of the spectra (see Supplementary Note S3 for further details). This procedure for estimating the dissonance levels of complex tones assumes that the main contribution for the perception of the timbre comes from the spectrum [35,36,38,39], which is a reasonable assumption taking into account the fact that the timbre is strongly dependent on the spectrum and weakly dependent on the other physical parameters of the sound waves [32]. Figure 3 presents the dissonance curves for the intervals within the octave in the case of the 12-TET scale. The spectrum of each complex tone corresponds to six harmonics, with amplitudes falling at a rate of 0.88, as proposed by Sethares [35]. Explicitly, this is A n = A 0 (0.88) n , where A 0 is the amplitude of the fundamental and A n is that of the corresponding harmonic n = 1, 2, 3, 4, 5, 6.
Relation between the dissonance level D and the locations of harmonic intervals in the register X = ( f j + f i )/2 for the 12-TET scale. The spectrum of each complex tone contains six harmonics with amplitudes falling at a rate of 0.88. Each possible size L corresponds to a particular frequency ratio inside the octave in the 12-TET scale. The dissonance level has been normalized to 1 for the typical register of an 88 key piano. Figure 3 shows that the same interval of size L is less dissonant in the middle part of the register than in the lowest part, which is a well-known property of intervals [10,11].
For each interval size L inside the octave, the dissonance level depends on its corresponding location in the register X = ( f j + f i )/2. The fit to exponential functions is: with fit parameters A 1 , A 2 , A 3 , γ 1 , and γ 2 . The values for the fit parameters of each interval size, and the corresponding determination coefficients R 2 , are presented in Supplementary Table S2.
In the case of intervals larger than the octave, the chroma property of pitches states that the consonance values of these intervals can be measured by displacing the highest pitch to the next lower octave until the resulting interval is smaller than or equal to one octave [10]. With this property, the plots shown in Figure 3 can be employed to measure the tonal consonance levels of all possible interval sizes located at any part of the register.

Expected Values of the Dissonance Levels Associated to Intervals
Suppose that in a musical piece, the probability associated to the frequency of occurrence of each harmonic interval size L is known as {p L } with ∑ L p L = 1. The average dissonance associated to harmonic intervals can be defined as: where H is the total number of harmonic intervals in the musical score. Grouping by intervals of equal size, as in Equation (6), we have that: and taking p L i = N L i /N as the probability of finding an interval of size L i , the expected value of dissonance in a musical piece owing to the contribution of harmonic intervals is: If all harmonic intervals have the same timbre and D can be expressed as in Equation (21), then the average dissonance for each kind of interval size D L can be approximately obtained by expanding Equation (21) in a Taylor series around the mean position in the register (see Supplementary Note S4 for further details): The first term in Equation (25) results from the first-order approximation in the Taylor expansion, indicating that the mean location in the register for each kind of interval size X L corresponds to the most important contribution to measuring the mean dissonance. The second term in Equation (25) results from the second-order term in the expansion, indicating that the dispersion of each interval size σ 2 L is necessary to more precisely measure the mean dissonance D . To summarize, by knowing X L and the set of probabilities {p L }, it is possible to measure L, the expected value of | f j − f i | , and approximate the mean dissonance level D . On the other hand, by knowing X L , σ 2 L , and {p L }, it is possible to measure L, the expected value | f 2 j − f 2 i | , and the mean dissonance level D with greater precision.
Traditionally, consonance properties have been associated with simultaneous sounds. However, there is evidence of the perception of consonance also in the case of successive sounds [33,41,42]. A suitable reason for the production of consonance or dissonance sensations in melody is the short-term persistence of pitch generated by successive pitches, especially in auditoriums, and the persistence in the memory of the previous pitch [41,42]. It has been observed that musicians tend to transpose their knowledge about the consonance of harmonic intervals to judge melodic ones. These results were found in the case of isolated successive pitches [33]. Under these conditions, the consonance level of melodic intervals can be approximated using the level of consonance of harmonic ones.

Melody and Expected Values of Melodic Intervals
This section presents some concepts about melody and the expected values associated to the asymmetry in the use of ascending and descending intervals in melodic lines.

Concerning Melody
Melody is defined in the New Grove Dictionary of Music and Musicians as "pitched sounds arranged in musical time in accordance with given cultural conventions and constraints" [43]. A definition that encompasses music and speech was given by Aniruddh Patel as "an organized sequence of pitches that conveys a rich variety of information to a listener" [31]. Melodies tend to meander around a central pitch range, and in many cultures, an asymmetry emerges, in the sense that large melodic intervals are more likely to ascend than small ones [5,44]. Figure 4 illustrates this asymmetry with a fragment extracted from the Fugue in D major BWV 850, of The Well-Tempered Clavier, Book 1 of J. S. Bach. The melody begins and ends with the pitch D (red boxes), and the ascending jump (blue box) is compensated using small descending intervals. So far, the sign of the interval size L has not been considered, as pitches in harmonic intervals are played simultaneously. However, in the case of melody, pitches are ordered chronologically (melodic intervals). For f i = f i (t) and f j = f j (t + 1), there are three possible cases: , and if f i = f j , then L = 0 (unison). Therefore, the sign of L distinguishes the chronological order of a pair of pitches.
For the case of the quantities f j − f i and f 2 j − f 2 i , the following notation will be employed: If {t z } represents a collection of times, at each of which one pitch is played in a melody (without rests), then the quantities t symbolize melodic intervals, with the sign distinguishing between ascending ( f t+1 > f t ) and descending ( f t+1 < f t ) intervals.
The case of f j > f i and f s > f r , which corresponds to ascending intervals, was analyzed in the section on the distinguishability of pairs of pitches. The case with f j < f i and f s < f r , which corresponds to descending intervals, is completely equivalent (see Equation (16)).

Expected Values of Melodic Intervals
In the case of melody, there are three kinds of melodic intervals, ascending, descending, and unisons, and the normalization constraint may be stated asp a +p d +p u = 1, wherep a is the probability of ascending intervals,p d is the probability of descending ones, andp u is the probability of unisons. The average magnitude of the melodic interval size contains the contributions of positive, negative, and zero values of L in the sum, L ∈ [L min , L max ], and Equation (5) remains unaltered. The average magnitude of the melodic interval size taking into account the mean location in the register X L and the dispersion σ 2 L lead to the same expressions given previously (Equations (8) and (11)). However, now, these contain the contributions of the ascending, descending, and unison intervals. These expected values include the average magnitude of the melodic intervals but do not discriminate between ascending and descending intervals. The average magnitudes of ascending and descending intervals, L >0 and L <0 , respectively, can be measured by: where the ratio p i /p a (p i /p d ) refers to the probability of the occurrence of an interval of size L i in the ascending (descending) intervals of a musical piece. The asymmetry in the total number of intervals isp a −p d and the asymmetry between the average magnitudes of ascending and descending intervals can be obtained as L >0 + L <0 , where L <0 < 0. Because the existing literature reports that in many cultures, large melodic intervals are more likely to ascend than small ones and that melodies tend to meander around a central pitch range [5,44], the quantityp a −p d is expected to be negative, and the quantity L >0 + L <0 is expected to be positive, for melodic lines of several musical pieces. See Figure 4.
The asymmetry in the average magnitudes of ascending and descending intervals, taking into account the mean position in the register X L and the dispersion of the intervals σ 2 L , can be measured These expressions take the form: and: With respect to the consonance issue, assuming for practical purposes that the results found for the dissonance level of isolated melodic intervals, in the case of musicians, can be used inside a melody, the mean dissonance level associated to a melodic line could be measured using (24), taking into account the contributions of ascending and descending intervals, and melodic unisons. For the consonance analysis of melodic intervals, the sign of L is irrelevant: Only its magnitude is important. Then, Figure 3 can be utilized for ascending intervals as well as descending ones.
Up to now, we have developed a representation of musical intervals suitable for analyzing harmony as well as melody. From now on, we limit the analysis to melody.

Materials and Methods: An Application to Melodic Lines
This section shows the analysis of a set of melodic lines using the representation of musical intervals proposed, and the procedures followed to obtain their corresponding probability and cumulative distributions.

Selection of Melodic Lines
Twenty melodic lines from seven vocal and instrumental masterpieces of the Baroque and Classical periods were analyzed. The selected pieces contain melodic lines characterized by their considerable length, internal coherence, and rich variety of instruments and registers. The collection of pieces is as follows: The melodic lines of these pieces written for cello contain mainly successive pitches. In the cases of the few simultaneous pitches, the continuation of the melodic lines was assumed in the direction of the highest pitch.

Procedure to Obtain the Probability and the Cumulative Distributions
The PDs for the quantities f t+1 − f t and f 2 t+1 − f 2 t were obtained for each melodic line in order to gather information concerning the selections of melodic intervals made by the composers. The procedure for the analysis of melodic lines was as follows: • The MIDI files were generated from scores. Only successive pitches without rests between them were considered.

•
The MIDI information was transformed into frequencies using the 12-TET scale with A = 440 Hz. Supplementary Spreadsheet S1 contains the data f t and f t+1 in Hz, corresponding to the melodic intervals of each melodic line.

•
The PDs were obtained in three different cases: distinguishing between ascending and descending intervals. The complementary cumulative distribution (CCD) was also obtained. - Case 2: | f t+1 − f t | and | f 2 t+1 − f 2 t | for two different sets of intervals: Ascending and unisons, and descending and unisons. The CCD was also obtained for each set.
-Case 3: f 2 t+1 − f 2 t for the set of ascending, descending, and unison intervals together. In this case, the sign of the descending intervals was considered as negative. The reason for only using the quantity f 2 t+1 − f 2 t is the quality of the experimental fits obtained in the two previous analyses for both quantities, and even more relevantly that the distinguishability analysis shows that f 2 t+1 − f 2 t has the best resolution properties for the case of 24 semitones in the 12-TET scale (see Table 1), which is the relevant range for melodic intervals in the analyzed melodic lines. The CCD was employed for the branch of the PD that contains the ascending intervals, and the cumulative distribution (CD) was utilized for the branch that contains the descending intervals.
Some clarifications are required in order to implement the sketch described above: • Because the number of melodic intervals in the studied melodic lines is at most one order of magnitude larger than the total number of possible pairs of successive pitches generated by the same ambitus (the range between the lowest and highest pitches) of the original melodic line, the PDs were constructed using histograms, in order to capture significant probabilities. Supplementary Table S3 shows the number of intervals of each melodic line, the number of ascending intervals, descending ones, and unisons, and the corresponding ambitus.

•
As the number of possible melodic intervals for any melodic line is finite, independently of its length, the bin width in the histograms will be moderately dependent on the number of melodic intervals. This condition is satisfied by the Sturges criterion [45], and thus, this criterion was used to determine the bin width.

•
In the third case, when ascending and descending PDs were combined in the same distribution for the quantity f 2 t+1 − f 2 t , the bin width was taken as the average of those obtained separately using the Sturges criterion for ascending and descending distributions. The average bins were symmetrically located to the left and right, starting from the point f 2 t+1 − f 2 t = 0.
• In the experimental analysis, the contribution of unisons in the histograms is important for ascending intervals as well as descending ones, with different right-hand and left-hand limits at 0. In addition, if we attempt to split the unisons into the ascending and descending parts, this procedure reduces the determination coefficient R 2 of the fits for the histograms to an exponential function [46]. Hence, all unisons were included in the ascending part as well as the descending one, and then a correction of this double count was carried out in the procedure to obtain the expected values. In the histograms, the descending intervals are contained inside the bins labeled from 1 to N/2 (from left to right), and the ascending ones inside those labeled from N/2 + 1 to N (from left to right). Hence, all unisons have been taken into account inside the bin labeled N/2 as well as that labelled N/2 + 1. Notice that N is an even number.

Results and Discussion
This section shows the experimental probability and cumulative distributions of the studied melodic lines, the Shannon entropy of intervals of two successive pitches in melodic lines, a statistical model based on the minimization of the Kullback-Leibler divergence that reproduces the main features of the experimental results, and the connection between the parameters of the statistical model with the transposition processes, the asymmetry between ascending and descending intervals, and the mean dissonance level of the studied melodic lines.

Experimental Results and Analysis
For the first and the second cases, the histograms and CCD for both quantities (| f t+1 − f t | and | f 2 t+1 − f 2 t |) fit to exponential functions. Supplementary Table S4 shows, for each melodic line in the first and the second case, the determination coefficient R 2 for the fits to exponential functions in histograms and CCD. The average R 2 of the CCD is R 2 ≈ 0.99, with a standard deviation (SD) of ≈ 0.01. Usually, the cumulative probability associated to the unison in the CCD is larger than the value predicted by the exponential behavior. This is not surprising, as the value 0 is degenerated and represents more than one possible pair of pitches. For histograms, the highest R 2 is for the quantity | f 2 t+1 − f 2 t |, with ascending and descending intervals taken separately. For ascending intervals, R 2 = 0.987 with SD = 0.009, and for descending ones R 2 = 0.986 with SD = 0.016.
For the third case, with the left and right branches of the PD combined in the same histogram, the PD can be written as: where the notation ε emphasizes that these distributions are constructed over bins. Figure 5 shows a set of probability distributions of melodic intervals for the quantity ε, panel (a), and for the melodic interval size L measured in semitones, panel (b). Notice that the traditional interval size does not distinguish the register of the musical instruments.
In the case of the cumulative distributions, the CCD and CD conserve the same functional form of the PD (as the PDs are exponential): and R 2 for the fits. These PDs resemble the asymmetric Laplace PD, with different amplitudes for positive and negative branches leading to a discontinuity at the origin ( Figure 6) [47]. Figure 6. General forms of the probability and cumulative distributions P(ε) and P( f 2 t+1 − f 2 t ), respectively. In the symmetric case, P 1 = P 2 and α 1 = α 2 . Figure 7a shows the histogram of the PD for the first movement of the Partita in A minor BWV 1013, as well as the PD for the bin degeneration in the corresponding ambitus, which originates from the structure of the musical scale and represents the melodic line with the highest diversity of melodic intervals in different locations of the register. The bin degeneration PD is equivalent to that of a long random melodic line (see Supplementary Note S5 for further details). In order to explain the effect of bin degeneration, notice that the distance in Hz 2 between pairs of differences f 2 j − f 2 i for the 12-TET scale varies in such a manner that the number of differences inside an arbitrary bin ε, representing its degeneracy, decreases when | f 2 j − f 2 i | increases. The comparison between the distributions of real melodic lines and those from bin degeneration for the corresponding ambitus indicates that the scale contributes to the observed results but does not explain them. In addition, the PD for bin degeneration fits better to a power law function (R 2 = 0.963) than to an exponential function (R 2 = 0.934). Supplementary Table S6 contains the determination coefficient R 2 for the fit to a power law and an exponential function, in the case of each melodic line. The quantitative difference between the PD for a real melodic line and its corresponding random one (the bin degeneration PD) provides information on the order introduced into the system by the composer, stemming from the selection of successive pairs of pitches. A mathematical tool for comparing two PDs is provided by the Kullback-Leibler divergence, or relative entropy [48]: where p k is the PD for the real melodic line to be compared with the a priori distribution q k coming from the degeneration of the k th bin, and N is the number of bins in the ambitus with N/2 bins for each branch (ascending and descending). The PD q k has been formally related to the probability associated with the number of distinguishable subcategories in the category k, representing its degeneracy [49]. The minimization of the relative entropy under constraints is useful to describe the form of the PD, as is explained in the next section.

Shannon Entropy of Melodic Intervals in Melodic Lines
Assuming that each possible melodic interval generated from the ambitus of a melodic line corresponds to a possible state, an analysis of the evolution of the entropy of melodic intervals in the progression of the melodic line can be performed in a similar manner as in the work by G. Gündüz and U. Gündüz [28]. For the A different pitches inside the ambitus of a melodic line, the number of different melodic intervals is A 2 . Following [28], we used the Shannon entropy: where M refers to the final melodic interval appearing in the progression of the melodic line, and p m is the probability that the interval m has already appeared in the sequence. The final Shannon entropy S f is reached when M is equal to the total number of melodic intervals in the melodic line. Figure 8 illustrates the evolution of the Shannon entropy of melodic intervals in melodic lines, from now on entropy. Panel (a) shows several melodic lines. Panel (b) shows the melodic lines of the Suite No. 2 BWV 1008, and the soprano in the Missa Super Dixit Maria, with their corresponding random melodies constructed using the same ambitus. The maximum entropy S max corresponds to the maximum possible value of the entropy in a long random melodic line with the same ambitus as the original one, namely S max = log 2 (A 2 ). Figure 8a,b shows that the entropy increases with each new melodic interval in the progression until it reaches a limiting value, which is smaller than the entropy of a random melodic line with the same ambitus. Some fluctuations appear in this process. However, the entropy tends to be stabilized at the final section of the melodic line. This result is similar to the findings of G. Gündüz and U. Gündüz analyzing the entropy evolution associated to the connectivity of pitches in different melodies [28].
For each melodic line, Table 2 presents the final entropy S f , the maximum entropy reached by the melodic line S * max , and the maximum entropy generated by the ambitus of the corresponding melodic line S max .  Table 2. Final Shannon entropy of melodic intervals S f , maximum Shannon entropy of melodic intervals reached by each melodic line S * max , maximum Shannon entropy of melodic intervals generated by the ambitus of the corresponding melodic line S max , Lagrange multipliers λ 1 and λ 2 , mean dissonance level D , and mean dissonance level approximated using the Taylor expansion up to second order (Equation (25)) D * . Melodic lines marked with " " do not satisfy a linear relation between λ 1 and D .

Statistical Model for Melodic Lines: Relative Entropy Minimization under Macroscopic Constraints
From the previously presented definitions of melody [31,43] and the results in Figure 8a,b and Table 2, we infer that the composer creates a melodic line among the richest in terms of the use of melodic intervals, but in accordance with musical constraints. Because each melodic interval in the 12-TET scale corresponds to a particular value of f 2 t+1 − f 2 t (except for unisons), and the expected value of this quantity contains musical information, the work carried out by the composer can be modeled as a procedure in which the relative entropy is minimized (the closest p k to q k ) under constraints with musical meaning.
Different musical constraints can be proposed in order to reduce the entropy value of a melodic line away from that of a random one, and we propose the following ones.
Assuming that the total numbers of ascending and descending intervals and unisons are known, the first two constraints measured from histograms are: wherep a is the probability of an ascending interval,p d is that of a descending one,p u is the probability of a unison, andp a +p d +p u = 1. Here, the unisons contribute to the ascending part as well as the descending part, as was explained in the methods section. The next constraint comes from the best estimation of the average magnitude of the melodic intervals using histograms (Equation (11) where the quantity −p u |ε N/2 | corrects the double counting of unisons. The asymmetry in the magnitudes of ascending and descending intervals is the final constraint. This asymmetry is present in the difference between the coefficients for the left and right branches in Equations (29) and (30). Using histograms, the best estimate that we can obtain for Equation (28) is: where the quantity |ε N/2 | p ũ p d −p ũ p a removes the contribution of unisons. Supplementary Table S7 contains the values of the quantities shown in Equations (11) and (28) and their corresponding approximations using histograms through Equations (34) and (35).
Minimizing the relative entropy subject to Equations (33)-(35) (in a similar procedure to that shown in [47]) produces the following PD (see Supplementary Note S6 for further details): where λ 1 and λ 2 are the Lagrange multipliers for Equations (34) and (35), respectively. The values of λ 1 and λ 2 were obtained using the expected values |ε| and ε from the histograms of the empirical distributions for the selected melodic lines, and allowing the relative error between the expected values from the statistical model and those from the real data to be smaller than 1.0%. Supplementary  Table S7 contains the expected values used in the statistical model, and Table 2 presents the values of the Lagrange multipliers generated from them. While the values of λ 1 are positive, those of λ 2 can be positive or negative, exhibiting possible asymmetries in the use of ascending and descending intervals. In addition, λ 1 is between one and two orders of magnitude larger than λ 2 . Figure 7b presents a comparison between the statistical model and the empirical results in the case of Suite No. 2 BWV 1008. Some differences between the empirical data and the results from the statistical model are expected, because there are patterns in real melodic lines that cannot be captured by this simple model.
The CCD (ascending branch) and CD (descending branch) can be utilized to compare different melodic lines that are either experimental or obtained from the statistical model. The CCD and CD were obtained from the histograms produced by the statistical model, randomly distributing the probability assigned to a bin between all the possible melodic intervals inside it, which were generated using the ambitus of the corresponding melodic line. Becausep u is known, the probability assigned to 0 inside the bins containing unisons was taken asp u , and the remaining probability of the bin was distributed randomly in the other possible melodic intervals. Figure 9 depicts the CCD and CD for the empirical data and the corresponding results from the statistical model for most melodic lines. In this figure, and taking into account the values in Table 2, the following features can be inferred:

1.
Different registers of musical instruments and human voices can be distinguished using the Lagrange multiplier λ 1 , allowing, for example, to discriminate between the same melodic line played in different parts of the register (a transposition). An example of a transposition is given in the Brandenburg Concerto No. 3 BWV 1048 by J. S. Bach, in which the harpsichord plays the same melodic line as the violone but transposed one octave higher (the fundamental frequency ratio of the transposition is equal to 2): While the entropy evolution in these melodic lines is the same, there is a change in the exponential decay parameters, characterized by the values of the Lagrange multipliers (see Table 2), and the numerical values of the expected values are related as: in agreement with the properties derived above for transposition processes (Equation (15)).

2.
With respect to the quantitative results of the model, the orders of magnitude of the fit parameters of the statistical model are in agreement with the corresponding results of the experimental fits. For each melodic line, Supplementary Table S8 contains the fit parameters to discontinuous asymmetric Laplace distributions, generated from the statistical model results. The average relative error in the histograms for the amplitude of the exponential distributions is 17.1%, and that for the decay coefficient is 20.6%. In the cases of the CD and CCD, the average errors of the amplitude and the decay coefficient are 7.2% and 11.8%, respectively. Supplementary Table S9 contains the values of these errors for each melodic line. 3.
In most cases (90% of the melodic lines), Equation (35) takes positive values (corresponding to negative values of λ 2 ), andp a −p d takes negative values (see Supplementary Table S3). This behavior is consistent with the asymmetry represented in Figure 4, in the sense that the magnitudes of ascending intervals are expected to be larger than those of descending ones, and the total number of descending intervals must be larger than that of ascending ones. Negative values ofp a −p d and λ 2 lead to different decay coefficients and different intercept points with the ordinate axis for the ascending and descending branches, which can be observed in the experimental fits of the CD and CCD through the comparison of the corresponding coefficients, Supplementary Table S5). Figure 6 was created with the purpose of magnifying these particular asymmetries: P 1 > P 2 and α 1 > α 2 (implying that λ 2 < 0). The two exceptions are the Piccolo Concerto RV444 of Antonio Vivaldi, where λ 2 > 0 andp a −p d > 0, and the melodic line of the tenor voice in Missa Super Dixit Maria, where λ 2 > 0 andp a −p d < 0. 4.
Because the difference between λ 1 and λ 2 is between one and two orders of magnitude (i.e., the decay coefficients have the same order of magnitude), and the bin width selection affects the measure of the decay parameters, the asymmetry in the values of the decay coefficients is better observed in the cumulative distributions than in the histograms. 5.
Because in Figure 6, the limit P 1 of the CD (constructed for descending intervals) when f 2 t+1 − f 2 t → 0 − represents the probability of a value slightly smaller than 0, and in the CCD (constructed for ascending intervals), P 2 when f 2 t+1 − f 2 t → 0 + represents the probability of a value slightly larger than 0, the asymmetryp a −p d ≈ P 2 − P 1 . This result can be observed in Figure 9 and represents the difference in the amplitudes of the exponential decay for the CD and CCD. In most cases, except for the Piccolo Concerto RV444,p a <p d , implying that P 1 > P 2 . In the case of the Piccolo Concerto RV444, it holds thatp a >p d , implying that P 1 < P 2 .

Transposition Processes and Mean Dissonance Level of Melodic Lines
As explained in the section on melody, tonal consonance properties can be formally associated to melodic intervals in the case of musicians. Because the musical instruments analyzed in this study use vibrating strings and air columns, the main consonance properties may be captured using the model of the harmonic spectrum presented in the tonal consonance section.
For each melodic line, the mean dissonance level D was measured using the curves shown in Figure 3 for intervals inside the octave, and the chroma properties of pitch for intervals wider than one octave. Table 2 lists the values of the mean dissonance D and their corresponding approximations D * using X L and σ 2 L in Equation (25). Comparing D * with D , the observed relative error is less than 1.0% for all melodic lines.
From the results in Table 2, melodic lines tend to be more dissonant for instruments with lower registers, which is a well-known phenomenon in music theory [10]. An interesting case is that of transposition, as the same melodic lines played in different parts of the register have different dissonance levels. For example, the melodic line of the violone in the Brandenburg Concerto BWV 1048 is perceived as more dissonant than that of the harpsichord.
Low registers are associated with small values of L, and therefore of | f 2 t+1 − f 2 t | and consequently also |ε| . For all melodic lines, a power law relation was observed between the quantity |ε| and the Lagrange multiplier λ 1 (see Figure 10a): where the magnitude of A is 9.423 × 10 −1 ± (9.76 × 10 −2 ), and B = −1.033 ± (1.26 × 10 −2 ), with R 2 = 0.998. If B is taken as −1, then A is dimensionless. Low values of |ε| correspond to high values of λ 1 , and vice versa, and λ 1 scales in a transposition process as: where λ O 1 and λ N 1 denote the first Lagrange multiplier in the original and new locations of the register, respectively. For the transposition between the violone and the harpsichord, λ Harpsichord 1 ≈ ω 2(−1.033) λ Violone 1 , with a 3% relative error (see Table 2). For 13 of the melodic lines studied, a linear relation was observed between the mean dissonance levels of melodic lines and the first Lagrange multiplier (see Figure 10b): where C = 1.122 × 10 −1 ± (1.7 × 10 −3 ) and D = (1236.29 ± 19.81) Hz 2 , with R 2 = 0.997. The Lagrange multiplier λ 1 locates the approximate region of exponential decay, and for these 13 melodic lines, this geometrical parameter can be employed as an indicator of the mean dissonance properties. Strong exponential decays correspond to low registers with high dissonance levels, and vice versa. The seven pieces that do not follow a linear relation (marked with " " in Table 2 and dot circles in Figure 10b) correspond to five cellos and a harpsichord, characterized by mean dissonance values between 0.25 and 0.30, and the piccolo of Concerto RV444 with a mean dissonance level of 0.0749.
The results show that the model proposed is suitable for the classification and generation of music. In the case of melody, the expected values can be used to classify melodic lines by their location in the register and the asymmetry in the use of ascending and descending intervals. For music generation, the expected values and the relevant PDs can be used as constraints for a melodic line, which is equivalent to providing full sets of intervals to be used in the musical piece.

Conclusions
The concept of the musical interval size was extended using two physical quantities: The difference between the fundamental frequencies of pitches and the difference in the squares of the fundamental frequencies. We explored the characteristics of these quantities in three different musical scales: The just, Pythagorean, and 12-TET. We found that both quantities contain information on the size of the interval and its location in the register, owing to the existence of a relationship between the construction rules of the scales and the sizes of intervals, which becomes linear in the most relevant regime for utilization in music. These quantities can be measured with different precision levels, allowing us in many cases to lift a degeneracy associated with the traditional musical interval size concept, in the sense that it cannot distinguish intervals of the same size located in different locations of the register.
The expected values of the two physical quantities were shown to be macroscopic quantities that contain relevant musical information. Specifically, they correspond to a generalization of the traditional mean musical interval size, as the expected values also take into account the mean location and the dispersion of the intervals in the register.
A link between the theory of tonal consonance and the expected values of the two considered physical quantities was developed. Specifically, knowing the mean location of musical intervals with a given size in the register, and the corresponding variance, it is possible to measure both the expected values and the mean dissonance properties of a musical piece, owing to the use of musical intervals produced by an instrument with a particular timbre.
In order to verify the usefulness of this formalism, it was applied to melodies. The frequency of occurrences of melodic intervals in 20 melodic lines from seven masterpieces of Western tonal music was measured, and the probability distributions of both quantities were obtained. In all cases, we obtained noncontinuous asymmetric Laplace distributions. In addition, the Shannon entropy associated with the appearances of melodic intervals during the progression of a melodic line increases up to a limiting value, which is smaller than the corresponding entropy for a random composition. In order to explain these empirical findings, a statistical model based on the minimization of the relative entropy under constraints was proposed for the difference in the squares of the fundamental frequencies. Two constraints are associated with the number of ascending, descending, and unison intervals, and the two other constraints correspond to expected values arising from the average magnitude of the physical quantity, and the asymmetry in the magnitudes of ascending and descending intervals. The model includes two Lagrange multipliers. The first locates the region in the register where the melody is played, giving information on musical processes such as transposition. The second captures asymmetry patterns between ascending and descending intervals. For 13 of the 20 studied melodic lines, the first Lagrange multiplier is related to the mean dissonance level of the melodic line, connecting macroscopic statistical properties with psychoacoustic features of the system.
The presented findings show that for the studied musical pieces, the selection of melodic intervals made by the composers, including their locations in the register, can be modeled as a tight compromise between order and disorder, with a principle of entropy extremalization constrained by macroscopic quantities with musical meanings, which embed microscopic musical rules, as well as the composer's preferences. While many complex systems exhibit emergent properties associated to nonphysical quantities, this work employed physical parameters to trace a connection between the properties of a musical piece as a whole and the psychoacoustic properties of its individual elements.
Supplementary Materials: The following are available online at http://www.mdpi.com/1099-4300/21/5/532/s1, Table S1: Frequency ratios used to construct the Pythagorean, the just, and the 12-TET scales, Table S2: Fitting parameters and determination coefficients for the dissonance curves, Table S3: Total number of melodic intervals, ambitus, and asymmetry between the number of ascending and descending intervals for each melodic line, Table S4: Determination coefficient R 2 for the fits of CCDs and histograms to exponential functions, for the quantities | f t+1 − f t | and | f 2 t+1 − f 2 t |, Table S5: Fit parameters for the discontinuous asymmetric Laplace distribution function: Real melodic lines, Table S6: Determination coefficient R 2 for the fit of the bin degeneracy distribution to a power law and to an exponential function, Table S7: Relevant expected values for real melodic lines and the statistical model results, Table S8: Fit parameters for the discontinuous asymmetric Laplace distribution function: Statistical model results, Table S9: Relative error of the fit parameters for the statistical model with respect to those of the real melodic lines, Note S1: Degeneracy equations for the quantities f j − f i and f 2 j − f 2 i in the Pythagorean, the just and the 12-TET scales, Note S2: Relation between the uncertainty in the frequency ratios ∆α and ∆α 2 with the uncertainty in the frequency ∆ f and the square of the frequency ∆ f 2 , Note S3: Tonal consonance of complex tones: Intervals of equal size played in different locations within the register, Note S4: Relating the average location in the register to an average of dissonance, Note S5: The PD representing a melodic line with the highest diversity of melodic intervals corresponds to the PD of a random melodic line, Note S6: Minimization of the relative entropy subject to constraints, Spreadsheet S1: Data corresponding to the melodic intervals of each melodic line.