Module:Harmonics in equal: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Sintel (talk | contribs)
No edit summary
Sintel (talk | contribs)
No edit summary
Line 40: Line 40:
intervals.prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 }
intervals.prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 }
intervals.odd = {3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53 }
intervals.odd = {3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53 }
intervals.integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,  12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27}
intervals.integer = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11,  12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27}


-- evaluate input on error use default
-- evaluate input on error use default
Line 74: Line 74:
local start = eval_num_arg(frame.args['start'], 1)
local start = eval_num_arg(frame.args['start'], 1)
-- option intervals
-- option intervals
local interv = intervals.prime
local select_intervals = "integer"
if denom == 1 and num == 2 then
select_intervals = "odd"
end
if frame.args['intervals'] and string.len(frame.args['intervals']) > 0 then
if frame.args['intervals'] and string.len(frame.args['intervals']) > 0 then
interv = intervals[frame.args['intervals']]
select_intervals = frame.args['intervals']
end
end


Line 92: Line 95:
name = steps .. 'edf'
name = steps .. 'edf'
end
end
title_intervals = "primes"
if select_intervals == "odd" then
title_intervals = "odd harmonics"
elseif select_intervals == "integer" then
title_intervals = "harmonics"
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 harmonics 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))
return approx( steps, num, denom, {unpack(interv, start, start+columns-1)}, title, prec)
return approx( steps, num, denom, {unpack(intervals[select_intervals], start, start+columns-1)}, title, prec)
end
end


return p;
return p;

Revision as of 22:51, 14 January 2022

Module documentation[view] [edit] [history] [purge]
This module should not be invoked directly; use its corresponding template instead: Template:Harmonics in equal.

Calculates approximations for harmonics in equal-step tunings and present them in form of a table.

Introspection summary for Module:Harmonics in equal 
Functions provided (1)
Line Function Params
64 primes_in_equal (invokable) (frame)
Lua modules required (0)
Variable Module Functions used

No function descriptions were provided. The Lua code may have further information.


local p = {}

local function approx(steps, num, denom, intervals, title, prec)
	local tpri = {'! colspan="2" | Prime number '}
	local tabs = {'! rowspan="2" | Error \n! absolute ([[cent|¢]]) '}
	local trel = {'! [[Relative interval error|relative]] (%) '}
	local tdeg = {'! colspan="2" | Steps<br>([[Octave_reduction|reduced]])'}
	local fmt_abs = string.format(' %%+.%df', prec)
	local fmt_rel = ' %+.0f'
	local equave = math.log(num/denom) / math.log(2)
	for _, p in pairs(intervals) do
		s = math.log(p) / math.log(num/denom)
		v = s*steps
		ev = math.floor(v + .5)
		table.insert(tpri, ' ' .. p)
		table.insert(tabs, string.format(fmt_abs, 1200 * equave * (ev - v ) / steps))
		table.insert(trel, string.format(fmt_rel, 100 * (ev - v)))
		table.insert(tdeg, ' ' .. ev .. '<br>('.. ev % steps .. ')')
	end
	local titleMarkup = ''
	if title then
		titleMarkup = '|-\n|+ ' .. title .. '\n'
	end
	
	return '{| class="wikitable center-all"\n' ..
		titleMarkup ..
		'|-\n' ..
		table.concat(tpri, '\n!') .. '\n' ..
		'|-\n' ..
		table.concat(tabs, '\n|') .. '\n' ..
		'|-\n' ..
		table.concat(trel, '\n|') .. '\n' ..
		'|-\n' ..
	table.concat(tdeg, '\n|') .. '\n' ..
		'|}'
end

local intervals = {}

intervals.prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 }
intervals.odd = {3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53 }
intervals.integer = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11,  12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27}

-- evaluate input on error use default
local function eval_num_arg(input, def_value)
	local result = input
	if type(input) ~= 'number' then
		result = def_value
		if type(input) == 'string' then
			input = input:match("^%s*(.-)%s*$")
			if string.len(input) > 0 then
				result = tonumber(input)
			end
		end
	end
	return result
end

-- calculate default precision
local function prec_by_equal(steps, num, denom)
	return math.floor(math.log(steps*1.9*math.log(2)/math.log(num/denom))/math.log(10))
end

function p.primes_in_equal (frame)
	-- optional number of steps, default: 12
	local steps = eval_num_arg(frame.args['steps'], 12)
	-- numerator, default: 2
	local num = eval_num_arg(frame.args['num'], 2)
	-- denominator, default: 1
	local denom = eval_num_arg(frame.args['denom'], 1)
	-- optional number of columns, default: 12
	local columns = eval_num_arg(frame.args['columns'], 12)
	-- optional start column, default: start with prime 2
	local start = eval_num_arg(frame.args['start'], 1)
	-- option intervals
	local select_intervals = "integer"
	if denom == 1 and num == 2 then
		select_intervals = "odd"
	end
	if frame.args['intervals'] and string.len(frame.args['intervals']) > 0 then
		select_intervals = frame.args['intervals']
	end

	local name = steps .. 'ed' .. num .. '/' .. denom
	if denom == 1 then
		if num == 2 then
			name = steps .. 'edo'
		elseif num == 3 then
			name = steps .. 'edt'
		else
			name = steps .. 'ed' .. num
		end
	end
	if num == 3 and denom == 2 then
		name = steps .. 'edf'
	end
	
	title_intervals = "primes"
	if select_intervals == "odd" then
		title_intervals = "odd harmonics"
	elseif select_intervals == "integer" then
		title_intervals = "harmonics"
	end
	
	local title = frame.args['title']
	if title == nil or string.len(title) == 0 then
		title = "Approximation of ".. title_intervals .. " in " .. name
	end
	-- optional precision for abs error, default about 3 digits
	local prec = eval_num_arg(frame.args['prec'], prec_by_equal(steps, num, denom))
	return approx( steps, num, denom, {unpack(intervals[select_intervals], start, start+columns-1)}, title, prec)
end

return p;