Module:Harmonics in ED: Difference between revisions

From Xenharmonic Reference
Created module code for Harmonics in ED template
 
removed Module:Utils line
Line 1: Line 1:
local p = {}
local p = {}
local utils = require("Module:Utils")
local primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89}
local primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89}
local p_cols = {"888888", "BB4E45", "5B963D", "4C55AB", "A3983F", "924FA3", "3D88AC", "3B977D", "B98147"} -- up to 23
local p_cols = {"888888", "BB4E45", "5B963D", "4C55AB", "A3983F", "924FA3", "3D88AC", "3B977D", "B98147"} -- up to 23

Revision as of 04:15, 16 December 2025

Documentation for this module may be created at Module:Harmonics in ED/doc

local p = {}
local primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89}
local p_cols = {"888888", "BB4E45", "5B963D", "4C55AB", "A3983F", "924FA3", "3D88AC", "3B977D", "B98147"} -- up to 23

local function steps(et, harm) -- steps of harmonic
    return math.log(harm) / math.log(2) * et
end

local function steps_re(et, harm) -- steps of reduced harmonic
    return steps(et, harm) - (math.floor(math.log(harm) / math.log(2)) * et)
end

local function rel_err(et, harm) -- relative error of harmonic
    st = steps(et, harm)
    return math.floor(st + 0.5) - st
end

local function abs_err(et, harm) -- absolute error of harmonic
    return 1200 / et * rel_err(et, harm)
end

local function rel_col(err) -- color used for relative error
    abs_err = math.abs(err)
    red = err * 270
    green = 135 - (err * 270)
    blue = 135 - (err * 1350)
    if blue < 0 then blue = 0 end
    return string.format("%02x%02x%02x", red, green, blue)
end

function p.table(frame) -- making the table itself
    local et = frame.args["et"]
    local p_lim = frame.args["p_lim"]
    local tab = "{| class=\"wikitable\"\n"
    tab = tab .. "|+Approximation of prime harmonics in "
    tab = tab .. et .. " edo\n"
    tab = tab .. "! colspan=\"2\" |Harmonic\n"
    for i,v in primes do
        tab = tab .. "! style=\"background-color:#"
        tab = tab .. (p_cols[i] or "555555") .. "\" |" .. v .. "\n"
        if v > p_lim then break end
    end
    tab = tab .. "|-\n! rowspan=\"2\" |Error\n!Absolute (¢)\n"
    for v in primes do
        tab = tab .. "| " .. (rel_err(et, v) > 0 and "+")
        tab = tab .. string.format("%.1f", abs_err(et, v)) .. "\n"
        if v > p_lim then break end
    end
    tab = tab .. "|-\n!Relative (%)\n"
    for v in primes do
        local er = rel_err(et, v)
        tab = tab .. "| style=\"background-color:#" .. rel_col(er)
        tab = tab .. "\" | " .. (er > 0 and "+")
        tab = tab .. string.format("%.1f", er * 100) .. "\n"
        if v > p_lim then break end
    end
    tab = tab .. "|-\n! colspan=\"2\" |Steps\n(reduced)\n"
    for v in primes do
        tab = tab .. "|" .. steps(et, v) .. "\n(" .. steps_re(et, v) .. ")\n"
        if v > p_lim then break end
    end
    tab = tab .. "|}"
    return tab
end

return p