Module:Harmonics in edo: Difference between revisions

Inthar (talk | contribs)
No edit summary
Inthar (talk | contribs)
can't get the fifthspan thing to work
Line 3: Line 3:
local function edoprox(edo, odds, title, prec)
local function edoprox(edo, odds, title, prec)
   local f = 1/edo
   local f = 1/edo
  local eps = .0001
   local todd = {'! colspan="2" | Odd harmonic '}
   local todd = {'! colspan="2" | Odd harmonic '}
   local tapprox = {'! colspan="2" | Approximation '}
   local tapprox = {'! colspan="2" | Approximation '}
Line 9: Line 8:
   local trel = {'! [[Relative error|relative]] (%) '}
   local trel = {'! [[Relative error|relative]] (%) '}
   local tdeg = {'! colspan="2" | Steps ([[octave reduction|reduced]])'}
   local tdeg = {'! colspan="2" | Steps ([[octave reduction|reduced]])'}
  local tfs = {'! colspan="2" | [[Fifthspan]]'}
   local fmt_approx = string.format(' %%.%df', prec)
   local fmt_approx = string.format(' %%.%df', prec)
   local fmt_abs = string.format(' %%+.%df', prec)
   local fmt_abs = string.format(' %%+.%df', prec)
Line 17: Line 15:
     v = s*edo
     v = s*edo
     ev = math.floor(v + .5)
     ev = math.floor(v + .5)
    m = math.floor(math.log(3)/math.log(2)*edo + .5)
     table.insert(todd, ' ' .. p )
     table.insert(todd, ' ' .. p )
     table.insert(tapprox, string.format(fmt_approx, 1200*(ev % edo)/edo) )
     table.insert(tapprox, string.format(fmt_approx, 1200*(ev % edo)/edo) )
Line 23: Line 20:
     table.insert(trel, string.format(fmt_rel, 100 * (ev - v)))
     table.insert(trel, string.format(fmt_rel, 100 * (ev - v)))
     table.insert(tdeg, ' ' .. ev .. ' ('.. ev % edo .. ')')
     table.insert(tdeg, ' ' .. ev .. ' ('.. ev % edo .. ')')
    diatonic_edo = m/edo > 4/7 and m/edo < 3/5 and gcd(m, edo) == 1
    if(diatonic_edo) then -- if the best fifth = m\edo is diatonic AND gcd(m, edo) == 1
    fs = 0
    position = 0
    while (position ~= ev and fs < edo) do
    fs = fs + 1
    position = (position + m) % edo
    end
    if (fs > edo/2) then
    fs = fs - edo
    end
    table.insert(tfs, ' ' .. fs )
    end
   end
   end
   local titleMarkup = ''
   local titleMarkup = ''
Line 42: Line 26:


   end
   end
   noFifthSpan = '{| class="wikitable center-all"\n' ..
   return '{| class="wikitable center-all"\n' ..
titleMarkup ..
titleMarkup ..
'|-\n' ..
'|-\n' ..
Line 53: Line 37:
   table.concat(tdeg, '\n|') .. '\n' ..
   table.concat(tdeg, '\n|') .. '\n' ..
     '|}'
     '|}'
    if (diatonic_edo) then
    return noFifthSpan ..
  table.concat(tfs, '\n|') .. '\n' ..
    '|}'
    else
    return noFifthspan
    end
end
end


local odds = { 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43 }
local odds = { 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43 }
function gcd(a, b)
if (b ~= 0) then
return a
else
return gcd(y, x%y)
end
end


function p.harmonics_in_edo (frame)
function p.harmonics_in_edo (frame)