Math-WalshTransform
view release on metacpan or search on metacpan
lua/WalshTransform.lua view on Meta::CPAN
-- www.pjb.com.au --
-- --
-- This module is free software; you can redistribute it and/or --
-- modify it under the same terms as Lua5 itself. --
-- ----------------------------------------------------------------- --
local M = {} -- public interface
M.Version = '1.17'
M.VersionDate = '12aug2010'
-- Example usage:
-- local WT = require 'WalshTransform'
-- H = WT.fht(a)
--------------------- infrastructure ----------------------
local function warn(str)
io.stderr:write(str,'\n')
end
local function die(str)
io.stderr:write(str,'\n')
os.exit(1)
end
lua/WalshTransform.lua view on Meta::CPAN
--[[
=pod
=head1 NAME
WalshTransform.lua - Fast Hadamard and Walsh Transforms
=head1 SYNOPSIS
local WT = require 'WalshTransform' -- name doesn't matter of course
local f = {1.618, 2.718, 3.142, 4.669} -- must be power-of-two long
local FH1 = WT.fht(f) -- Hadamard transform
local fh1 = WT.fhtinv(FH1)
-- or
local FW2 = WT.fwt(f) -- Walsh transform
local fw2 = WT.fwtinv(FW2)
local FH2 = WT.walsh2hadamard(FW2)
local PS = WT.power_spectrum(f)
lua/test.lua view on Meta::CPAN
function ok(b,s)
i_test = i_test + 1
if b then
io.write('ok '..i_test..' - '..s.."\n")
else
io.write('not ok '..i_test..' - '..s.."\n")
Failed = Failed + 1
end
end
--------------------------- infrastructure ----------------
local M = require 'WalshTransform'
local x = {0,2,2,0};
local X = M.fht(x);
ok (equal(X, {1,0,0,-1}), "Hadamard transform");
X = M.fwt(x);
--io.write('X = '..table.concat(X,', ').."\n")
ok (equal(X, {1,0,-1,0}), "Walsh transform");
ok (equal(M.fhtinv{1,0,0,-1}, {0,2,2,0}), "Inverse Hadamard transform");
local IWT = M.fwtinv{1,0,-1,0}
( run in 0.414 second using v1.01-cache-2.11-cpan-05444aca049 )