User:Arseniiv/Fitting: Difference between revisions
fitting to an arbitrary pattern of steps |
m →To a periodic scale with specific step pattern: I meant more_itertools |
||
Line 25: | Line 25: | ||
If we have a pattern of steps in mind, say sLMLsLMsML, we can try fitting a scale to a scale of this form by writing an error term for each note in the period of the new scale. These terms will be integer linear combinations of our chosen step sizes <math>s, M, L</math>, an overal scale shift <math>d</math> (see [[#To edX]]) and numbers (cent values of intervals). | If we have a pattern of steps in mind, say sLMLsLMsML, we can try fitting a scale to a scale of this form by writing an error term for each note in the period of the new scale. These terms will be integer linear combinations of our chosen step sizes <math>s, M, L</math>, an overal scale shift <math>d</math> (see [[#To edX]]) and numbers (cent values of intervals). | ||
Having all those errors in symbolic form, it’s particularly easy to minimize RMS error constrained by all steps of the scale constituting exactly the period of the old scale, using Lagrange multiplier method. We end up with a linear system comprising of equations in step sizes, <math>d</math> and a single Lagrange multiplier related to the periodicity constraint, which is then solved to get the optimal step sizes. All these computations can be done using <code>sympy</code> Python package. In the code below I also use <code> | Having all those errors in symbolic form, it’s particularly easy to minimize RMS error constrained by all steps of the scale constituting exactly the period of the old scale, using Lagrange multiplier method. We end up with a linear system comprising of equations in step sizes, <math>d</math> and a single Lagrange multiplier related to the periodicity constraint, which is then solved to get the optimal step sizes. All these computations can be done using <code>sympy</code> Python package. In the code below I also use <code>more_itertools</code> to make my life a tad easier. | ||
==== Code ==== | ==== Code ==== | ||
Should work in Python 3.12 and several previous versions. Install fresh <code> | Should work in Python 3.12 and several previous versions. Install fresh <code>more_itertools</code> and <code>sympy</code>. | ||
<pre> | <pre> | ||
Line 170: | Line 170: | ||
print('\n'.join(map(str, fit.pitches))) | print('\n'.join(map(str, fit.pitches))) | ||
</pre> | </pre> | ||
== Notes == | == Notes == | ||
<references /> | <references /> |