3 (*by Torben
Mogensen (torbenm@diku
.dk
)*)
6 val pi
= 3.14159265358979
8 fun pr (s
: string) : unit
= print(s
)
10 fun impossible s
= impossible0 (s ^
"\n")
11 and impossible0 s
= (pr ("\nimpossible: " ^ s
); raise Impossible
)
13 fun zipWith
f ([],[]) = []
14 | zipWith
f ((a
::b
),(c
::d
)) = f (a
,c
) :: zipWith
f (b
,d
)
15 | zipWith f _
= impossible
"zipWith"
18 |
zip ((a
::b
),(c
::d
)) = (a
,c
) :: zip (b
,d
)
19 | zip _
= impossible
"zip"
21 fun ~
+ ((x
:real,y
:real),(v
,w
)) = (x
+v
,y
+w
)
23 fun ~
- ((x
:real,y
:real),(v
,w
)) = (x
-v
,y
-w
)
25 fun ~
* ((x
:real,y
:real),(v
,w
)) = (x
*v
-y
*w
,x
*w
+y
*v
)
28 |
evens (x
::y
::l
) = x
:: evens l
29 | evens _
= impossible
"evens"
32 |
odds (x
::y
::l
) = y
:: odds l
33 | odds _
= impossible
"odds"
35 fun fmul (c
,pin
,[]) = []
37 = ~
*((Math
.cos(c
),Math
.sin(c
)), a
) :: fmul (c
+pin
,pin
,b
)
40 |
cp (a
::b
) = a
:: cp b
42 fun fft ([(a
,b
)], 1) = [(a
+0.0,b
+0.0)]
44 = let val n
= n2
div 2
45 val a
= fft (evens x
, n
)
46 val cb
= fmul (0.0,pi
/(real n
),fft (odds x
, n
))
48 let val l1
= zipWith ~
+ (a
,cb
)
49 val l2
= zipWith ~
- (a
,cb
)
50 in (*resetRegions a
; resetRegions cb
;*) l1 @ l2
54 local val a
= 16807.0 and m
= 2147483678.0
58 in t
- m
* real(floor (t
/m
)) end
62 fun mkList(tr
as (seed
,0,acc
)) = tr
63 |
mkList(seed
,n
,acc
) = mkList(nextrand seed
, n
-1, seed
::acc
)
67 fun run () = (pr
"\nfft by Torben Mogensen (torbenm@diku.dk)\n\nfft'ing... ";
68 let val r
= fft (zip (#
3(mkList(7.0,n
,[])),
69 #
3(mkList(8.0,n
,[]))), n
) in