#!/bin/lua function note(...) io.stderr:write(string.format(...)) end require 'utils' require 'mathlib' function sinc(t, H2) local ph = fmod(t, 1) local eps = 0.000001 if ph < eps or ph > (1 - eps) then return H2*cos(H2*pi*t)/cos(pi*t) else return sin(H2*pi*t)/sin(pi*t) end end function evf(t1, t2, n, fn, c) local dt = (t2 - t1) / n local u = 0 local c = c or 0 for t = t1, t2, dt do u = fn(t) print(t, u+c) end end function int(t1, t2, n, fn, c) local dt = (t2 - t1) / n local u = 0 local c = c or 0 for t = t1, t2, dt do print(t, u+c) u = u + (fn(t) + fn(t+dt)) * dt / 2 end return u end -- dvojny integral pres ten samy interval function intint(t1, t2, n, fn, c) local dt = (t2 - t1) / n local dx = dt local u = 0 local y = 0 local u1 = 0 local c = c or 0 for t = t1, t2, dt do print(t, y+c) u1 = u u = u + (fn(t) + fn(t+dt)) * dt / 2 y = y + (u1 + u) * dt / 2 --[[ y = y u = u + (fn(t) + 2*fn(t+dt) + fn(t+2*dt)) * dt / 2 ]] end return y end function impulse(N) return function(t) return sinc(t, 2*N+1) end end function square(N, pwm) pwm = pwm or 0.5 return function(t) return sinc(t, 2*N+1) - sinc(t+pwm, 2*N+1) end end function morph(N, swsq) swsq = swsq or 0.5 return function(t) return sinc(t, 2*N+1) - swsq * sinc(t+0.5, 2*N+1) - 1 + swsq end end function saw(N) return function(t) return sinc(t, 2*N+1) - 1 end end function supersaw(N, pwm) return function(t) return sinc(t+pwm, 2*N+1) - 1 --return sinc(t, 2*N+1) - 1 + sinc(t+pwm, 2*N+1) - 1 end end --intint(0, 4, 1024, square(32)); --int(0, 4, 4096, saw(64), 0) int(0, 2, 4096, supersaw(64, 0.5), 0)