-- codecs/audio/Mdct tests: the FFT against a direct DFT for mixed-radix sizes, the forward / inverse MDCT and the DCT-IV -- against their O(N^2) definitions (AAC, Vorbis and CELT sizes), perfect reconstruction (TDAC) with the sine, KBD and -- Vorbis windows, window power complementarity, bad sizes. No fixtures (the definitions are the reference). local T = require("@tests/T") local Mdct = require("@rosetta/codecs/audio/Mdct") local t = T.suite("mdct") local rand = T.rng(7) local cos, sin, pi = math.cos, math.sin, math.pi local function noise(n: number): { number } local a = table.create(n, 0) for i = 1, n do a[i] = rand(20001) / 10000 - 1 end return a end -- FFT vs DFT for _, n in { 1, 2, 3, 4, 5, 6, 7, 8, 12, 15, 16, 30, 49, 60, 64, 120, 240, 480, 77 } do local plan = Mdct.fft(n) t:check(plan, "fft plan " .. n) local re, im = noise(n), noise(n) local wr, wi = table.create(n, 0), table.create(n, 0) for k = 0, n - 1 do local sr, si = 0, 0 for j = 0, n - 1 do local a = -2 * pi * j * k / n sr += re[j + 1] * cos(a) - im[j + 1] * sin(a) si += re[j + 1] * sin(a) + im[j + 1] * cos(a) end wr[k + 1], wi[k + 1] = sr, si end Mdct.Fft.forward(plan :: any, re, im) local err = 0 for k = 1, n do err = math.max(err, math.abs(re[k] - wr[k]), math.abs(im[k] - wi[k])) end t:check(err < 1e-9 * n, string.format("fft %d: max error %g", n, err)) end -- MDCT / IMDCT vs the definitions local maxErr = 0 for _, n in { 4, 8, 16, 64, 240, 256, 480, 1920, 2048 } do local plan = Mdct.new(n) :: Mdct.Plan t:check(plan, "mdct plan " .. n) local m = n // 2 local x = noise(n) local X = table.create(m, 0) Mdct.forward(plan, x, 1, X, 1) local Y = noise(m) local y = table.create(n, 0) Mdct.inverse(plan, Y, 1, 1, y, 1) local step = if n > 500 then 37 else 1 -- spot-check the big sizes local ef, ei = 0, 0 for k = 0, m - 1, step do local s = 0 for j = 0, n - 1 do s += x[j + 1] * cos(2 * pi / n * (j + 0.5 + n / 4) * (k + 0.5)) end ef = math.max(ef, math.abs(s - X[k + 1])) end for j = 0, n - 1, step do local s = 0 for k = 0, m - 1 do s += Y[k + 1] * cos(2 * pi / n * (j + 0.5 + n / 4) * (k + 0.5)) end ei = math.max(ei, math.abs(s - y[j + 1])) end t:check(ef < 1e-9 * n and ei < 1e-9 * n, string.format("mdct %d: forward error %g, inverse error %g", n, ef, ei)) maxErr = math.max(maxErr, ef, ei) -- strided coefficients + offsets + scale local X2 = table.create(m * 3 + 5, 0) Mdct.forward(plan, x, 1, X2, 3, 3, 0.5) local ok = true for k = 0, m - 1 do ok = ok and math.abs(X2[3 + 3 * k] - 0.5 * X[k + 1]) < 1e-9 end t:check(ok, "mdct " .. n .. ": strided / scaled forward") end t:note("mdct vs definition: max error %.3g", maxErr) -- TDAC: windowed forward + inverse + overlap-add reproduces the signal for _, kind in { "sine", "kbd", "vorbis" } do for _, n in { 256, 2048, 240 } do local w = Mdct.window(kind, n, if n == 256 then 6 else 4) :: { number } local m = n // 2 local plan = Mdct.new(n) :: Mdct.Plan local pc = 0 for i = 1, m do pc = math.max(pc, math.abs(w[i] ^ 2 + w[i + m] ^ 2 - 1)) end t:check(pc < 1e-12, kind .. " " .. n .. ": power complementary") local frames = 6 local sig = noise(m * (frames + 1)) local out = table.create(m * (frames + 1), 0) local buf, X, y = table.create(n, 0), table.create(m, 0), table.create(n, 0) for f = 0, frames - 1 do for i = 1, n do buf[i] = sig[f * m + i] * w[i] end Mdct.forward(plan, buf, 1, X, 1) Mdct.inverse(plan, X, 1, 1, y, 1, 4 / n) for i = 1, n do out[f * m + i] += y[i] * w[i] end end local e = 0 for i = m + 1, m * frames do e = math.max(e, math.abs(out[i] - sig[i])) end t:check(e < 1e-9, string.format("%s %d: reconstruction error %g", kind, n, e)) end end -- DCT-IV is its own inverse (times 2 / M) do local plan = Mdct.new(512) :: Mdct.Plan local u = noise(256) local c, back = table.create(256, 0), table.create(256, 0) Mdct.dct4(plan, u, 1, 1, c, 1, 1) Mdct.dct4(plan, c, 1, 1, back, 1, 1, 2 / 256) local e = 0 for i = 1, 256 do e = math.max(e, math.abs(back[i] - u[i])) end t:check(e < 1e-10, "dct4 involution: " .. e) end -- bad sizes t:check(Mdct.new(6) == nil and Mdct.new(0) == nil and Mdct.new(-8) == nil and Mdct.new(8.5) == nil and Mdct.new("x" :: any) == nil, "bad mdct sizes") t:check(Mdct.fft(0) == nil and Mdct.window("nope", 8) == nil and Mdct.window("sine", 7) == nil, "bad fft sizes / windows") t:done()