-
Notifications
You must be signed in to change notification settings - Fork 5
/
unroll.go
66 lines (61 loc) · 1.13 KB
/
unroll.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"math"
"os"
"strconv"
)
var M int
func main() {
n, e := strconv.Atoi(os.Args[1])
if e != nil {
panic("unroll n")
}
O("#include<complex.h>\n")
O("#define z double complex\n")
M = 1<<n
O("int fft%d(z *x){\n", M)
O("z p,q;\n")
for i := 0; i<M; i++ {
O("z y%d;\n", i)
}
f(M, 1, 0)
O("}\n")
}
// func O(f string, a ...interface{}) { fmt.Printf(f, a...) }
var O = fmt.Printf
func f(N, s, o int) {
if N == 1 {
O("z x1_%d=x[%d];\n", o, o)
return
}
f(N/2, 2*s, o)
for i := 0; i < N/2; i++ {
O("y%d=x%d_%d;\n", i, N/2, i+o)
}
f(N/2, 2*s, o+s)
for i := 0; i < N/2; i++ {
O("y%d=x%d_%d;\n", i+N/2, N/2, i+o+s)
}
for i := 0; i < N/2; i++ {
O("p=y%d;\n", i)
re := math.Cos(2.0 * math.Pi * float64(i) / float64(N))
im := math.Sin(2.0 * math.Pi * float64(i) / float64(N))
re = small(re)
im = small(im)
O("q=((%v)*I*(%v))*y%d;\n", re, -im, i+N/2)
if N == M {
O("x[%d]=p+q;\n", i+o)
O("x[%d]=p-q;\n", i+o+N/2)
} else {
O("z x%d_%d=p+q;\n", N, i+o)
O("z x%d_%d=p-q;\n", N, i+o+N/2)
}
}
}
func small(f float64) float64 {
if math.Abs(f) < 1e-16 {
return 0
}
return f
}