User:Arseniiv/Fitting: Difference between revisions
m →To a periodic scale with specific step pattern: I meant more_itertools |
m →Code: fix code |
||
Line 40: | Line 40: | ||
import sympy as sp | import sympy as sp | ||
def | def _cents(ratio: float | str) -> float: | ||
if isinstance(ratio, str): | if isinstance(ratio, str): | ||
ratio = float(Fraction(ratio)) | ratio = float(Fraction(ratio)) | ||
Line 48: | Line 48: | ||
"""Make a scale of pitches in cents from intervals delimited by spaces.""" | """Make a scale of pitches in cents from intervals delimited by spaces.""" | ||
return tuple(map( | return tuple(map(_cents, intervals.split())) | ||
def match_pattern(scale: Sequence[float], pattern: str) -> dict[str, float] | None: | def match_pattern(scale: Sequence[float], pattern: str) -> dict[str, float] | None: | ||
Line 88: | Line 88: | ||
note_errors = [vars_[SHIFT]] | note_errors = [vars_[SHIFT]] | ||
sym_pitches = [sp.sympify(0)] | sym_pitches = [sp.sympify(0)] | ||
for letter, pitch in zip(pattern, | for letter, pitch in zip(pattern, scale): | ||
last_pitch = sym_pitches[-1] + vars_[letter] | last_pitch = sym_pitches[-1] + vars_[letter] | ||
sym_pitches.append(last_pitch) | sym_pitches.append(last_pitch) | ||
Line 96: | Line 96: | ||
note_errors.pop() | note_errors.pop() | ||
sym_pitches.pop(0) | sym_pitches.pop(0) | ||
assert len(note_errors) == len( | assert len(note_errors) == len(scale) | ||
error_sqr_sum = sum(error ** 2 for error in note_errors) | error_sqr_sum = sum(error ** 2 for error in note_errors) |