DR error measures: Difference between revisions

From Xenharmonic Reference
No edit summary
Line 11: Line 11:
with root real-valued harmonic ''x''. Let <math>D_0 = 0, D_i = \sum_{k=1}^i \delta_k</math> be the delta signature +δ<sub>1</sub> +δ<sub>2</sub> ... +δ<sub>''n''</sub> written cumulatively.
with root real-valued harmonic ''x''. Let <math>D_0 = 0, D_i = \sum_{k=1}^i \delta_k</math> be the delta signature +δ<sub>1</sub> +δ<sub>2</sub> ... +δ<sub>''n''</sub> written cumulatively.


We want to measure the error ''without having to fix any dyad'' (as one might naively fix a dyad and measure errors in the other deltas).
We want to measure the error ''without having to fix any dyad'' (as one might naively fix a dyad and measure errors in the other deltas). To do this we solve a least-squares error problem: use a root-sum-square error function and optimize ''x'' to minimize that function.


== Rooted linear error ==
== Domain and error model ==
We have two choices:
* to measure either the linear error or the logarithmic one (called the ''domain'');
* the collection of intervals to sum over (which we call the ''error model''):
** ''Rooted'': Only intervals from the root real-valued harmonic ''x'' are chosen.
** ''Pairwise'': All intervals in the chord are compared.
** ''All-steps'': Only intervals between adjacent notes are compared.
The method to solve the problem will also differ depending on the numbers of variables involved (only one variable ''x'' for fully delta-rational).
 
We arrive at the following general formula: Let <math>[n] = \{0, 1, 2, ..., n\},</math> let <math>I \subseteq {[n] \choose 2},</math> and let <math>f_D</math> represent the domain function (identity or <math>\log</math>). Then the error function to be minimized by optimizing <math>x</math> and any free deltas is:
 
<math>
\sum_{i<j, \{i,j\} \in I} \Bigg( f_D \Bigg( \frac{x + D_j}{x + D_i} \Bigg) - f_D\Bigg( \frac{r_j}{r_i} \Bigg) \Bigg)^2.
</math>
 
=== Rooted linear ===
'''Rooted linear error''' (here ''linear'' means "in frequency space, not pitch space") measures error by optimizing how well ''cumulative'' intervals from the root real-valued harmonic match the target chord's DR signature.
'''Rooted linear error''' (here ''linear'' means "in frequency space, not pitch space") measures error by optimizing how well ''cumulative'' intervals from the root real-valued harmonic match the target chord's DR signature.


=== Fully DR ===
<math>
We wish to minimize the following frequency-domain error function by optimizing ''x'':
\displaystyle{ \sqrt{\sum_{i=1}^n \Bigg( \frac{x + D_i}{x} - r_i \Bigg)^2 }  = \sqrt{\sum_{i=1}^n \Bigg( 1 + \frac{D_i}{x} - r_i \Bigg)^2 } }.
</math>
 
=== Rooted logarithmic ===
This error measure also measures errors of rooted intervals, but measures the error in logarithmic interval distance and thus arguably has a more musically intuitive meaning.
 
The error function to be minimized, with units in nepers (logarithmic unit for frequency ratio of ''e''), is
 
<math>\sqrt{\sum_{i=1}^n \Bigg(\log \frac{x + D_i}{r_i x} \Bigg)^2}.</math>
 
(To scale to cents, multiply by 1200/log 2.)
 
=== Pairwise linear ===
Measure all pairwise intervals, linearly:
 
<math>
\displaystyle{ \sqrt{\sum_{0\leq i<j\leq n} \Bigg( \frac{x + D_j}{x + D_i} - \frac{r_j}{r_i} \Bigg)^2 } }.
</math>
 
=== Pairwise logarithmic ===
Measure all pairwise intervals, logarithmically (in nepers):
 
<math>\sqrt{\sum_{0\leq i<j\leq n} \Bigg(\log \frac{x + D_j}{x + D_i} - \log \frac{r_j}{r_i} \Bigg)^2}.</math>
 
=== All-steps linear ===
Measure stepwise intervals, linearly:


<math>
<math>
\displaystyle{ \sqrt{\sum_{i=1}^n \Bigg( \frac{x + D_i}{x} - r_i \Bigg)^2 }  = \sqrt{\sum_{i=1}^n \Bigg( 1 + \frac{D_i}{x} - r_i \Bigg)^2 } }.
\displaystyle{ \sqrt{\sum_{0\leq i<n} \Bigg( \frac{x + D_{i+1}}{x + D_i} - \frac{r_{i+1}}{r_i} \Bigg)^2 } }.
</math>
</math>


=== All-steps logarithmic ===
Measure stepwise intervals, logarithmically (in nepers):
<math>\sqrt{\sum_{0\leq i<n} \Bigg(\log \frac{x + D_{i+1}}{x + D_i} - \log \frac{r_{i+1}}{r_i} \Bigg)^2}.</math>
== Solution methods ==
=== Analytic solution (FDR case) ===
==== Rooted linear ====
Setting the derivative to 0 gives us the closed-form solution
Setting the derivative to 0 gives us the closed-form solution


Line 32: Line 80:


to obtain the least-squares linear error.
to obtain the least-squares linear error.
=== Grid method (FDR case) ===


=== Partially DR (one related delta set, one free variable) ===
=== Partially DR (one related delta set, one free variable) ===
Line 75: Line 125:


Todo: The L-BFGS-B algorithm is suited for five-variable (base real-valued harmonic + four free deltas; a realistic upper bound on real-world use cases of partial DR) optimization problems with bounds, so let's talk about that
Todo: The L-BFGS-B algorithm is suited for five-variable (base real-valued harmonic + four free deltas; a realistic upper bound on real-world use cases of partial DR) optimization problems with bounds, so let's talk about that
=== Arbitrary related delta sets ===
== Rooted logarithmic error ==
This error measure also measures errors of rooted intervals, but measures the error in logarithmic interval distance and thus arguably has a more musically intuitive meaning.
=== Fully DR ===
The error function to be minimized, with units in nepers (logarithmic unit for frequency ratio of ''e''), is
<math>\sqrt{\sum_{i=1}^n \Bigg(\log \frac{x + D_i}{r_i x} \Bigg)^2}.</math>
(To scale to cents, multiply by 1200/log 2.)
== All-interval linear error ==
Measure all pairwise intervals, linearly
=== Fully DR ===
<math>
\displaystyle{ \sqrt{\sum_{0\leq i<j\leq n} \Bigg( \frac{x + D_j}{x + D_i} - \frac{r_j}{r_i} \Bigg)^2 } }.
</math>
== All-interval logarithmic error ==
This error measure measures errors of ''all'' intervals, not just rooted ones.
=== Fully DR ===
The error function to be minimized, with units in nepers, is
<math>\sqrt{\sum_{0\leq i<j\leq n} \Bigg(\log \frac{x + D_j}{x + D_i} - \log \frac{r_j}{r_i} \Bigg)^2}.</math>


== External links ==
== External links ==
* [https://inthar-raven.github.io/delta/ Inthar's DR chord explorer (includes least-squares error calculation in both domains and error models)]
* [https://inthar-raven.github.io/delta/ Inthar's DR chord explorer (includes least-squares error calculation in both domains and multiple error models)]
[[Category:Atypical ratios]]
[[Category:Atypical ratios]]

Revision as of 19:36, 12 December 2025

This is a technical or mathematical page. While the subject may be of some relevance to music, the page treats the subject in technical language.

This article will describe several least-squares error measures for delta-rational chords. They have the advantage of not fixing a particular interval in the chord when constructing the chord of best fit. However, like any other numerical measure of concordance or error, you should take them with a grain of salt.

Conventions and introduction

The idea motivating least-squares error measures on a chord as an approximation to a given delta signature is the following (for simplicity, let’s talk about the fully DR case first):

We want the error of a chord 1:r1:r2:...:rn (in increasing order), with n > 1, in the linear domain as an approximation to a fully delta-rational chord with signature +δ12 ... +δn, i.e. a chord

x:x+δ1::x+l=1nδl

with root real-valued harmonic x. Let D0=0,Di=k=1iδk be the delta signature +δ12 ... +δn written cumulatively.

We want to measure the error without having to fix any dyad (as one might naively fix a dyad and measure errors in the other deltas). To do this we solve a least-squares error problem: use a root-sum-square error function and optimize x to minimize that function.

Domain and error model

We have two choices:

  • to measure either the linear error or the logarithmic one (called the domain);
  • the collection of intervals to sum over (which we call the error model):
    • Rooted: Only intervals from the root real-valued harmonic x are chosen.
    • Pairwise: All intervals in the chord are compared.
    • All-steps: Only intervals between adjacent notes are compared.

The method to solve the problem will also differ depending on the numbers of variables involved (only one variable x for fully delta-rational).

We arrive at the following general formula: Let [n]={0,1,2,...,n}, let I([n]2), and let fD represent the domain function (identity or log). Then the error function to be minimized by optimizing x and any free deltas is:

i<j,{i,j}I(fD(x+Djx+Di)fD(rjri))2.

Rooted linear

Rooted linear error (here linear means "in frequency space, not pitch space") measures error by optimizing how well cumulative intervals from the root real-valued harmonic match the target chord's DR signature.

i=1n(x+Dixri)2=i=1n(1+Dixri)2.

Rooted logarithmic

This error measure also measures errors of rooted intervals, but measures the error in logarithmic interval distance and thus arguably has a more musically intuitive meaning.

The error function to be minimized, with units in nepers (logarithmic unit for frequency ratio of e), is

i=1n(logx+Dirix)2.

(To scale to cents, multiply by 1200/log 2.)

Pairwise linear

Measure all pairwise intervals, linearly:

0i<jn(x+Djx+Dirjri)2.

Pairwise logarithmic

Measure all pairwise intervals, logarithmically (in nepers):

0i<jn(logx+Djx+Dilogrjri)2.

All-steps linear

Measure stepwise intervals, linearly:

0i<n(x+Di+1x+Diri+1ri)2.

All-steps logarithmic

Measure stepwise intervals, logarithmically (in nepers):

0i<n(logx+Di+1x+Dilogri+1ri)2.

Solution methods

Analytic solution (FDR case)

Rooted linear

Setting the derivative to 0 gives us the closed-form solution

x=i=1nDin+i=1nri,

which can be plugged back into

1=1n(1+Dixri)2

to obtain the least-squares linear error.

Grid method (FDR case)

Suppose we wish to approximate a target delta signature of the form +δ1+?+δ3 with the chord 1:r1:r2:r3 (where the +? is free to vary). By a derivation similar to the above, the least-squares problem is

minimizex,y(x+δ1xr1)2+(x+δ1+yxr2)2+(x+δ1+y+δ3xr3)2,

where y represents the free delta +?.

We can set the partial derivatives with respect to x and y of the inner expression equal to zero (since the derivative of sqrt() is never 0) and use SymPy to solve the system:

import sympy
x = sympy.Symbol("x", real=True)
y = sympy.Symbol("y", real=True)
d1 = sympy.Symbol("\\delta_{1}", real=True)
d2 = sympy.Symbol("\\delta_{2}", real=True)
d3 = sympy.Symbol("\\delta_{3}", real=True)
r1 = sympy.Symbol("r_1", real=True)
r2 = sympy.Symbol("r_2", real=True)
r3 = sympy.Symbol("r_3", real=True)
err_squared = ((x + d1) / x - r1) ** 2 + ((x + d1 + y) / x - r2) ** 2 + ((x + d1 + y + d3) / x - r3) ** 2
err_squared.expand()
err_squared_x = sympy.diff(err_squared, x)
err_squared_y = sympy.diff(err_squared, y)
sympy.nonlinsolve([err_squared_x, err_squared_y], [x, y])

The unique solution with x > 0 is

x=2δ1+δ3+2yr2+r32,

y=2δ12r1+δ12r2+δ12r3δ1δ3r1+δ1δ3r2δ1δ3r3+δ1δ3+δ32r2δ322δ1r12δ1δ3r2+δ3r3.

We similarly include a free variable to be optimized for every additional +?, after coalescing strings of consecutive +?'s and omitting the middle notes, and after trimming leading and trailing +?'s.

Todo: The L-BFGS-B algorithm is suited for five-variable (base real-valued harmonic + four free deltas; a realistic upper bound on real-world use cases of partial DR) optimization problems with bounds, so let's talk about that