Module:Harmonics in equal: Difference between revisions

ArrowHead294 (talk | contribs)
mNo edit summary
m Broken
Tag: Undo
Line 5: Line 5:
local function map(p, steps, num, denom)
local function map(p, steps, num, denom)
local s = math.log(p) / math.log(num/denom)
local s = math.log(p) / math.log(num/denom)
return math.floor(s * steps + 0.5)
return math.floor(s*steps + .5)
end
end
-- check consistency for 9, 15 and 21
-- check consistency for 9, 15 and 21
Line 16: Line 16:
local p21 = map(21, steps, num, denom)
local p21 = map(21, steps, num, denom)
return (p9 == 2 * p3) and (p15 == p3 + p5) and (p21 == p3 + p7)
return (p9 == 2*p3) and (p15 == p3+p5) and (p21 == p3+p7)
end
end


Line 22: Line 22:
local thead
local thead
if collapsed then
if collapsed then
thead = "{| class=\"wikitable center-all mw-collapsible mw-collapsed\"\n"
thead = '{| class="wikitable center-all mw-collapsible mw-collapsed"\n'
else
else
thead = "{| class=\"wikitable center-all\"\n"
thead = '{| class="wikitable center-all"\n'
end
end
local tpri = {"! colspan=\"2\" | Harmonic "}
local tpri = {'! colspan="2" | Harmonic '}
local tabs = {"! rowspan=\"2\" | Error \n! Absolute ([[cent|¢]]) "}
local tabs = {'! rowspan="2" | Error \n! Absolute ([[cent|¢]]) '}
local trel = {"! [[Relative interval error|Relative]] ([[relative cent|%]]) "}
local trel = {'! [[Relative interval error|Relative]] ([[relative cent|%]]) '}
local tdeg
local tdeg
if reduction then
if reduction then
tdeg = {"! colspan=\"2\" | Steps<br />([[octave reduction|reduced]])"}
tdeg = {'! colspan="2" | Steps<br>([[octave reduction|reduced]])'}
else
else
tdeg = {"! colspan=\"2\" | Step"}
tdeg = {'! colspan="2" | Step'}
end
end
local fmt_abs = string.format(' %%+.%df', prec)
local fmt_abs = string.format(' %%+.%df', prec)
local fmt_rel = " %+.1f"
local fmt_rel = ' %+.1f'
local equave = math.log(num/denom) / math.log(2)
local equave = math.log(num/denom) / math.log(2)
for _, p in pairs(intervals) do
for _, p in pairs(intervals) do
s = math.log(p) / math.log(num/denom)
s = math.log(p) / math.log(num/denom)
v = s * steps
v = s*steps
ev = math.floor(v + 0.5)
ev = math.floor(v + .5)
table.insert(tpri, "" .. p)
table.insert(tpri, "" .. p)
table.insert(tabs, "" .. string.gsub(string.format(fmt_abs, 1200 * equave * (ev - v ) / steps), "%-", "-"))
table.insert(tabs, "" .. string.gsub(string.format(fmt_abs, 1200 * equave * (ev - v ) / steps), "%-", "-"))
table.insert(trel, "" .. string.gsub(string.format(fmt_rel, 100 * (ev - v)), "%-", "-"))
table.insert(trel, "" .. string.gsub(string.format(fmt_rel, 100 * (ev - v)), "%-", "-"))
if reduction then
if reduction then
table.insert(tdeg, "" .. ev .. "<br />(".. ev % steps .. ")")
table.insert(tdeg, '' .. ev .. '<br>('.. ev % steps .. ')')
else
else
table.insert(tdeg, "" .. ev)
table.insert(tdeg, '' .. ev)
end
end
end
end
local titleMarkup = ""
local titleMarkup = ''
if title then
if title then
titleMarkup = "|-\n|+ style=\"white-space: nowrap;\" | " .. title .. "\n"
titleMarkup = '|-\n|+ style=\"white-space: nowrap;" | ' .. title .. '\n'
end
end
return thead ..
return thead ..
titleMarkup ..
titleMarkup ..
"|-\n! " ..
'|-\n' ..
table.concat(tpri, " !! ") .. "\n" ..
table.concat(tpri, '\n!') .. '\n' ..
"|-\n| " ..
'|-\n' ..
table.concat(tabs, " || ") .. "\n" ..
table.concat(tabs, '\n|') .. '\n' ..
"|-\n| "..
'|-\n' ..
table.concat(trel, " || ") .. "\n" ..
table.concat(trel, '\n|') .. '\n' ..
"\n| " ..
'|-\n' ..
table.concat(tdeg, " || ") .. "\n" ..
table.concat(tdeg, '\n|') .. '\n' ..
"|}"
'|}'
end
end


Line 78: Line 78:
local function eval_num_arg(input, def_value)
local function eval_num_arg(input, def_value)
local result = input
local result = input
if type(input) ~= "number" then
if type(input) ~= 'number' then
result = def_value
result = def_value
if type(input) == "string" then
if type(input) == 'string' then
input = input:match("^%s*(.-)%s*$")
input = input:match("^%s*(.-)%s*$")
if string.len(input) > 0 then
if string.len(input) > 0 then
Line 92: Line 92:
-- calculate default precision
-- calculate default precision
local function prec_by_equal(steps, num, denom)
local function prec_by_equal(steps, num, denom)
return math.floor(10 * math.log(steps * 1.9 * math.log(2) / math.log(num / denom)) / math.log(10)) / 10
return math.floor(10*math.log(steps*1.9*math.log(2)/math.log(num/denom))/math.log(10))/10
end
end


function p.harmonics_in_equal (frame)
function p.harmonics_in_equal (frame)
-- optional number of steps, default: 12
-- optional number of steps, default: 12
local steps = eval_num_arg(frame.args["steps"], 12)
local steps = eval_num_arg(frame.args['steps'], 12)
-- numerator, default: 2
-- numerator, default: 2
local num = eval_num_arg(frame.args["num"], 2)
local num = eval_num_arg(frame.args['num'], 2)
-- denominator, default: 1
-- denominator, default: 1
local denom = eval_num_arg(frame.args["denom"], 1)
local denom = eval_num_arg(frame.args['denom'], 1)
-- optional number of columns, default: 11
-- optional number of columns, default: 11
local columns = eval_num_arg(frame.args["columns"], 11)
local columns = eval_num_arg(frame.args['columns'], 11)
-- optional start column, default: start with prime 2
-- optional start column, default: start with prime 2
local start = eval_num_arg(frame.args["start"], 1)
local start = eval_num_arg(frame.args['start'], 1)
-- option intervals
-- option intervals
local select_intervals = "integer"
local select_intervals = "integer"




local name = steps .. "ed" .. num .. "/" .. denom
local name = steps .. 'ed' .. num .. '/' .. denom
if denom == 1 then
if denom == 1 then
if num == 2 then
if num == 2 then
name = steps .. "edo"
name = steps .. 'edo'
select_intervals = "odd"
select_intervals = "odd"
if check_consistency(steps, num, denom) then
if check_consistency(steps, num, denom) then
Line 120: Line 120:
end
end
elseif num == 3 then
elseif num == 3 then
name = steps .. "edt"
name = steps .. 'edt'
else
else
name = steps .. "ed" .. num
name = steps .. 'ed' .. num
end
end
end
end
if num == 3 and denom == 2 then
if num == 3 and denom == 2 then
name = steps .. "edf"
name = steps .. 'edf'
end
end
-- override default intervals
-- override default intervals
if frame.args["intervals"] and string.len(frame.args["intervals"]) > 0 then
if frame.args['intervals'] and string.len(frame.args['intervals']) > 0 then
select_intervals = frame.args["intervals"]
select_intervals = frame.args['intervals']
end
end
Line 141: Line 141:
end
end
local title = frame.args["title"]
local title = frame.args['title']
if title == nil or string.len(title) == 0 then
if title == nil or string.len(title) == 0 then
title = "Approximation of ".. title_intervals .. " in " .. name
title = "Approximation of ".. title_intervals .. " in " .. name
end
end
-- optional precision for abs error, default about 3 digits
-- optional precision for abs error, default about 3 digits
local prec = eval_num_arg(frame.args["prec"], prec_by_equal(steps, num, denom))
local prec = eval_num_arg(frame.args['prec'], prec_by_equal(steps, num, denom))
local reduction = true
local reduction = true
if steps == 1 then
if steps == 1 then
reduction = false
reduction = false
end
end
local collapsed = utils.value_provided(frame.args["collapsed"])
local collapsed = utils.value_provided(frame.args['collapsed'])
return approx(steps, num, denom, {unpack(intervals[select_intervals], start, start + columns - 1)}, title, prec, reduction, collapsed)
return approx( steps, num, denom, {unpack(intervals[select_intervals], start, start+columns-1)}, title, prec, reduction, collapsed)
end
end


return p
return p;