Module:Powharmonic series

From Xenharmonic Wiki
Jump to navigation Jump to search

Documentation transcluded from /doc
Note: Do not invoke this module directly; use the corresponding template instead: Template:Powharmonic series.

Generates a powharmonic series interval table for a given exponent, which is used to model timbres involving stretched or compressed harmonics.


local p = {}

function p.powharmonic(frame)
    local n = tonumber(frame.args["n"])
    local expo = tonumber(frame.args["expo"])
    local out_str = "{| class=\"wikitable center-all\"\n"
	    .. "|+ style=\"font-size: 105%;\" | Intervals of the " .. expo .. "-Powharmonic series\n"
	    .. "! rowspan=\"2\" | Pitch !! colspan=\"2\" | Frequency multiplier !! colspan=\"3\" | Pitch\n"
	    .. "|-\n"
	    .. "! Definition !! Decimal !! [[Cent]]s !! Change (cents) !! Octave-reduced (cents)"
	    .. "\n"
    local fmt_cent = string.format("%%.%df", tonumber(frame.args["prec"]))
    local p_prev = 0
    local p_current = 0

    for i = 1, n do
        p_current = 1200 * math.log(i) / math.log(2) * expo
        out_str = out_str
            .. "|-\n"
            .. "| ".. i
            .. " || " .. i .. "<sup>" .. expo .. "</sup>"
            .. " || " .. string.format(fmt_cent, i^expo)
            .. " || " .. string.format(fmt_cent, p_current)
            .. " || " .. string.format(fmt_cent, p_current - p_prev)
            .. " || " .. string.format(fmt_cent, math.fmod(p_current, 1200))
            .. "\n"
        p_prev = p_current
    end

    out_str = out_str .. "|}"
    return out_str
end

return p