Last active
May 4, 2023 02:05
-
-
Save VictorTaelin/39892b4a301c9ff89a522b8cb573972e to your computer and use it in GitHub Desktop.
fast fourier transform using only integers on HVM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(Rot (V z)) = (V (- 0.0 z)) | |
(Rot (G x y)) = (G (Rot y) x) | |
(Add (V z) (V w)) = (V (+ z w)) | |
(Add (G x y) (G w z)) = (G (Add x w) (Add y z)) | |
(Get (V x) f) = (f x) | |
(Get (G x y) f) = (f x y) | |
Nil = λm λx (m x) | |
(Skp f) = λm (f λa (Get a λx λy (G (m x) (m y)))) | |
(Twx f) = λm (f λa (Get a λx λy (Rot (G (m x) (m y))))) | |
(Mix (L x) (L y) twid zero) = | |
let mins = (Add x (twid λa(a) y)) | |
let maxs = (Add x (twid λa(Rot a) y)) | |
(B (L (G mins zero)) (L (G maxs zero))) | |
(Mix (B x y) (B w z) twid zero) = | |
let lfts = (Mix x w (Skp twid) (G zero zero)) | |
let rgts = (Mix y z (Twx twid) (G zero zero)) | |
(B lfts rgts) | |
(FFT (L x)) = (L (V x)) | |
(FFT (B x y)) = | |
let eves = (FFT x) | |
let odds = (FFT y) | |
(Mix eves odds Nil (V 0.0)) | |
// Conversions | |
// ----------- | |
// List | |
(List.concat List.nil ys) = ys | |
(List.concat (List.cons x xs) ys) = (List.cons x (List.concat xs ys)) | |
// Complex | |
PI = 3.141592653589793 | |
(CSca (C ar ai) s) = (C (* ar s) (* ai s)) | |
(CAdd (C ar ai) (C br bi)) = (C (+ ar br) (+ ai bi)) | |
(CMul (C ar ai) (C br bi)) = (C (- (* ar br) (* ai bi)) (+ (* ar bi) (* ai br))) | |
(CPol ang) = (C (& ang 0.0) (& (/ PI 2.0) ang)) | |
(Com num) = (Com.val num E) | |
(Com.val (V x) path) = (CSca (Com.twi path (/ PI 2.0) (C 1.0 0.0)) x) | |
(Com.val (G a0 a1) path) = (CAdd (Com.val a0 (O path)) (Com.val a1 (I path))) | |
(Com.twi E ang num) = num | |
(Com.twi (O path) ang num) = (Com.twi path (/ ang 2.0) num) | |
(Com.twi (I path) ang num) = (Com.twi path (/ ang 2.0) (CMul num (CPol ang))) | |
// Tree | |
(Map f (L x)) = (L (f x)) | |
(Map f (B x y)) = (B (Map f x) (Map f y)) | |
(Flt (L x)) = [x] | |
(Flt (B x y)) = (List.concat (Flt x) (Flt y)) | |
(Idx E (L x)) = x | |
(Idx (O p) (B x y)) = (Idx p x) | |
(Idx (I p) (B x y)) = (Idx p y) | |
(Dep (L x)) = 0 | |
(Dep (B x y)) = (+ 1 (Dep x)) | |
(Inv tree) = (Inv.go (Dep tree) E tree) | |
(Inv.go 0 path tree) = (L (Idx path tree)) | |
(Inv.go n path tree) = (B (Inv.go (- n 1) (O path) tree) (Inv.go (- n 1) (I path) tree)) | |
(See x) = (Flt (Inv (Map λx(Com x) x))) | |
// Main | |
Main = | |
let c0 = 1.0 | |
let c1 = 2.0 | |
let c2 = 3.0 | |
let c3 = 4.0 | |
let c4 = 5.0 | |
let c5 = 6.0 | |
let c6 = 7.0 | |
let c7 = 8.0 | |
(See (FFT (B (B (B (L c0) (L c4)) (B (L c2) (L c6))) (B (B (L c1) (L c5)) (B (L c3) (L c7)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improved: