#!/bin/lua require 'utils' require 'mathlib' function comp(vars, ...) local s = 'return function (x) return %s end' local fn = assert(loadstring(s:format(...))) setfenv(fn, vars) return fn() end function abs2(P, w) local re, im = 0, 0 for i = 1, #P do local k = i - 1 re = re + P[i] * cos(k*w) im = im - P[i] * sin(k*w) end return re^2 + im^2 end local vars = {} local N = {} local D = {} local i = 1 local num = true for k, v in pairs(math) do vars[k] = v end while arg[i] do local a = arg[i] if a:match('^%-[%a_]') then vars[a:sub(2)] = comp(vars, arg[i+1])() i = i + 1 elseif a == '/' then num = false else local n = comp(vars, arg[i])() push(num and N or D, n) end i = i + 1 end local n = 1024 local w1, w2 = 0, pi local dw = (w2 - w1) / n for w = w1, w2, dw do local g = abs2(N, w) / abs2(D, w) if g > 0 then local y = db(g) print(w, y) end end printf("p\n")