forked from ktye/fft
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fft.go
186 lines (172 loc) · 4.86 KB
/
fft.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Package gofft provides a fast discrete Fourier transformation algorithm.
//
// Implemented is the 1-dimensional DFT of complex input data
// for with input lengths which are powers of 2.
//
// The algorithm is non-recursive, works in-place overwriting
// the input array, and requires O(1) additional space.
//
// Before doing the transform on acutal data, prepare the fft with
// t := gofft.Prepare(N) where N is the length of the input array.
// Then multiple calls to gofft.FFT(x) can be done with
// different input vectors having the same length.
package gofft
import (
"fmt"
"math"
)
var (
factorsMap = map[int][]complex128{}
permMap = map[int][]int{}
)
// Prepare precomputes values used for FFT on a vector of length N.
// N must be a perfect power of 2, otherwise this will return an error.
func Prepare(N int) error {
if !IsPow2(N) {
return fmt.Errorf("Input dimension must be power of 2, is: %d", N)
}
if _, ok := factorsMap[N]; ok {
// Already prepared, no need to do anything
return nil
}
factorsMap[N] = roots(N)
permMap[N] = permutationIndex(N)
return nil
}
// checkN tests N as being a valid FFT vector length.
// Returns an error if it isn't.
func checkN(N int) error {
if _, ok := factorsMap[N]; !ok {
if !IsPow2(N) {
return fmt.Errorf("Input dimension must be power of 2, is: %d", N)
}
return fmt.Errorf("FFT is not initialized for input dimension: %d, must initialize with Prepare(N) first", N)
}
return nil
}
// FFT implements the fast Fourier transform.
// This is done in-place (modifying the input array).
// Requires O(1) additional memory.
// len(x) must be a perfect power of 2, otherwise this will return an error.
// You must call Prepare(len(x)) before this, otherwise this will return an error.
func FFT(x []complex128) error {
N, factors, perm, err := getVars(x)
if err != nil {
return err
}
fft(x, N, factors, perm)
return nil
}
// IFFT implements the inverse fast Fourier transform.
// This is done in-place (modifying the input array).
// Requires O(1) additional memory.
// len(x) must be a perfect power of 2, otherwise this will return an error.
// You must call Prepare(len(x)) before this, otherwise this will return an error.
func IFFT(x []complex128) error {
N, factors, perm, err := getVars(x)
if err != nil {
return err
}
ifft(x, N, factors, perm)
return nil
}
// Pre-load the fft variables for later use.
func getVars(x []complex128) (N int, factors []complex128, perm []int, err error) {
N = len(x)
factors = factorsMap[N]
perm = permMap[N]
err = checkN(N)
return
}
// fft does the actual work for FFT
func fft(x []complex128, N int, factors []complex128, perm []int) {
// Handle small N quickly
switch N {
case 1:
return
case 2:
f := factors[0] * x[1]
x[0], x[1] = x[0]+f, x[0]-f
return
case 4:
x[1], x[2] = x[2], x[1]
f := factors[0] * x[1]
x[0], x[1] = x[0]+f, x[0]-f
f = factors[0] * x[3]
x[2], x[3] = x[2]+f, x[2]-f
f = factors[0] * x[2]
x[0], x[2] = x[0]+f, x[0]-f
f = factors[1] * x[3]
x[1], x[3] = x[1]+f, x[1]-f
return
}
// Reorder the input array.
permute(x, perm, N)
// Butterfly
s := N
for n := 1; n < N; n <<= 1 {
s >>= 1
for o := 0; o < N; o += (n << 1) {
for k := 0; k < n; k++ {
i := k + o
f := factors[k*s] * x[i+n]
x[i], x[i+n] = x[i]+f, x[i]-f
}
}
}
}
// ifft does the actual work for IFFT
func ifft(x []complex128, N int, factors []complex128, perm []int) {
// Reverse the input vector
for i := 1; i < N/2; i++ {
j := N - i
x[i], x[j] = x[j], x[i]
}
// Do the transform.
fft(x, N, factors, perm)
// Scale the output by 1/N
invN := complex(1.0/float64(N), 0)
for i := 0; i < N; i++ {
x[i] *= invN
}
}
// permutationIndex builds the bit-inverted index vector,
// which is needed to permutate the input data.
func permutationIndex(N int) []int {
index := make([]int, N)
index[0] = 0 // Initial sequence for N=1
// For every next power of two, the sequence is multiplied by 2 in-place.
// Then the result is also appended to the end and increased by one.
for n := 1; n < N; n <<= 1 {
for i := 0; i < n; i++ {
index[i] <<= 1
index[i+n] = index[i] + 1
}
}
// Re-arrange the permutation to just the necessary swaps
for i := 1; i < N-1; i++ {
ind := index[i]
for ind < i {
ind = index[ind]
}
index[i] = ind
}
return index
}
// permutate permutes the input vector according to the permutation vector.
// Uses an in-place algorithm that on FFT permutation vectors runs in O(N) time.
func permute(x []complex128, perm []int, N int) {
// perm[0] is always 0, and perm[N-1] is always N-1, so skip those
for i := 1; i < N-1; i++ {
x[i], x[perm[i]] = x[perm[i]], x[i]
}
}
// roots computes the complex-roots-of 1 table of length N.
func roots(N int) []complex128 {
factors := make([]complex128, N/2)
for n := 0; n < N/2; n++ {
s, c := math.Sincos(-2.0 * math.Pi * float64(n) / float64(N))
factors[n] = complex(c, s)
}
return factors
}