User:Fastaro/Generalized Pythagorean tuning: Difference between revisions
→Generating the ratios with Python code: - changed to decimal format |
|||
Line 132: | Line 132: | ||
2/1 | 2/1 | ||
=== Finding Potential MOS === | |||
<syntaxhighlight lang="python"> | |||
from math import log, floor | |||
from fractions import Fraction | |||
p = 3 | |||
q = 2 | |||
# Define the function to calculate 'n' using the floor function | |||
def calculate_n(x, p, q): | |||
return floor(x * log(p) / log(q)) | |||
mos_limit = 100 # This can be adjusted as needed | |||
# Initialize lists to store the results | |||
ratios = [] | |||
# Calculate and store the values of the ratios for each 'x' | |||
for x in range(mos_limit + 1): | |||
n = calculate_n(x, p, q) | |||
ratio1 = Fraction(p**x, q**n) | |||
ratio2 = Fraction(q**(n+1), p**x) | |||
ratios.append((ratio1, ratio2)) | |||
# Find Moments of Symmetry (MOS) | |||
threshold = 0.1 # Define how close the absolute difference must be to 0 or 1 to qualify as MOS | |||
mos_values = [] | |||
for i, (ratio1, ratio2) in enumerate(ratios): | |||
abs_diff = abs(float(ratio1) - float(ratio2)) | |||
if abs_diff < threshold: | |||
mos_values.append((i, abs_diff)) | |||
elif abs(1 - abs_diff) < threshold: | |||
mos_values.append((i, abs(1 - abs_diff))) | |||
# Sort MOS values by the error term | |||
mos_values_sorted = sorted(mos_values, key=lambda x: x[1])[1:] | |||
# Format the sorted MOS values for printing | |||
formatted_mos_values = ['MOS val: {} Error: {:.10f}'.format(mos_val, error) for mos_val, error in mos_values_sorted] | |||
print('\n'.join(formatted_mos_values)) | |||
</syntaxhighlight> | |||
== Implications and applications == | == Implications and applications == |