Module:Interval edo approximation: Difference between revisions
No edit summary |
No edit summary |
||
| (25 intermediate revisions by 5 users not shown) | |||
| Line 2: | Line 2: | ||
-- Calculates EDO approximations for just intervals | -- Calculates EDO approximations for just intervals | ||
-- Usage: {{#invoke:EDO_Approximations|main|interval=3/2|tolerance=9|min_edo=5|max_edo=60}} | -- Usage: {{#invoke:EDO_Approximations|main|interval=3/2|tolerance=9|min_edo=5|max_edo=60}} | ||
local u = require("Module:Utils") | |||
local yesno = require("Module:Yesno") | |||
local p = {} | local p = {} | ||
-- ===== CONFIGURATION VARIABLES ===== | -- ===== CONFIGURATION VARIABLES ===== | ||
local DEFAULT_TOLERANCE = | local DEFAULT_TOLERANCE = 13.0 -- Relative error tolerance in percent | ||
local DEFAULT_MIN_EDO = 5 -- Minimum EDO to check | local DEFAULT_MIN_EDO = 5 -- Minimum EDO to check | ||
local DEFAULT_MAX_EDO = 60 -- Maximum EDO to check | local DEFAULT_MAX_EDO = 60 -- Maximum EDO to check | ||
| Line 14: | Line 15: | ||
-- Python: return 1200 * math.log2(ratio) | -- Python: return 1200 * math.log2(ratio) | ||
local function cents(ratio) | local function cents(ratio) | ||
return 1200 * u.log2(ratio) | |||
return 1200 * | |||
end | end | ||
| Line 117: | Line 91: | ||
local args = frame.args | local args = frame.args | ||
local interval_str = args.interval or args[1] | local interval_str = args.interval or args[1] | ||
local interval_name = args.interval_name -- Optional display name | |||
-- Convert string parameters to numbers using config defaults | -- Convert string parameters to numbers using config defaults | ||
local tolerance = tonumber(args.tolerance) or DEFAULT_TOLERANCE | local tolerance = tonumber(args.tolerance) or DEFAULT_TOLERANCE | ||
| Line 129: | Line 103: | ||
-- Parse interval string to numeric ratio | -- Parse interval string to numeric ratio | ||
local ratio = | local ratio = u.eval_num_arg(interval_str) | ||
if not ratio then | if not ratio then | ||
return "Error: Invalid interval format (use format like '3/2')" | return "Error: Invalid interval format (use format like '3/2')" | ||
| Line 138: | Line 112: | ||
if #results == 0 then | if #results == 0 then | ||
return "No | return "No edos found within tolerance of " .. tolerance .. "%" | ||
end | end | ||
-- Build the wikitable | -- Build the wikitable | ||
-- mw-collapsible: adds [hide] toggle button | |||
-- mw-collapsed: table defaults to collapsed ##removed | |||
-- sortable: makes columns sortable by clicking headers | |||
local output = {} | local output = {} | ||
table.insert(output, '{| class="wikitable"') | table.insert(output, '{| class="wikitable center-all mw-collapsible sortable"') | ||
table.insert(output, '|-' | -- Calculate the precise ratio in cents for the caption | ||
local ratio_cents = cents(ratio) | |||
-- Include subtitle info in caption to avoid breaking sortable functionality | |||
local display_name = (interval_name and interval_name ~= "") and interval_name or interval_str | |||
table.insert(output, '|+ style="font-size: 105%;" | ' | |||
.. string.format('Edo approximations for %s (%.2f{{c}})<br /><span style="font-size: 0.75em;">\'\'≤ %dedo, relative error ≤ %g%%\'\'</span>', | |||
display_name, ratio_cents, max_edo, tolerance)) | |||
table.insert(output, '|-') | table.insert(output, '|-') | ||
table.insert(output, '! | table.insert(output, '! Edo' | ||
.. ' !! class="unsortable" | Step size' | |||
.. ' !! Cents ([[cent|¢]])' | |||
.. ' !! Absolute error ([[cent|¢]])' | |||
.. ' !! [[Relative interval error|Relative error]] ([[relative cent|%]])') | |||
for _, result in ipairs(results) do | for _, result in ipairs(results) do | ||
| Line 156: | Line 143: | ||
-- Python: step_str = f"{steps}\\{edo}" | -- Python: step_str = f"{steps}\\{edo}" | ||
local step_size = string.format("%d\\%d", result.steps, result.edo) | local step_size = string.format("%d\\%d", result.steps, result.edo) | ||
-- Calculate approximation in cents: steps * (1200/edo) | |||
local approximation_cents = result.steps * (1200 / result.edo) | |||
local approx_str = string.format("%.2f", approximation_cents) | |||
-- Python: abs_err = f"{result['abs_error']:+.2f}" | -- Python: abs_err = f"{result['abs_error']:+.2f}" | ||
| Line 164: | Line 155: | ||
table.insert(output, '|-') | table.insert(output, '|-') | ||
table.insert(output, string.format('| %s || %s || %s || %s', edo_link, step_size, abs_err, rel_err)) | table.insert(output, string.format('| %s || %s || %s || %s || %s', edo_link, step_size, approx_str, abs_err, rel_err)) | ||
end | end | ||
table.insert(output, '|}') | table.insert(output, '|}') | ||
local result = table.concat(output, '\n') | |||
if yesno(frame.args["debug"]) == true then | |||
result = '<syntaxhighlight lang="wikitext">' .. result .. '</syntaxhighlight>' | |||
end | |||
return frame:preprocess(result) | |||
end | end | ||
return p | return p | ||