-
Notifications
You must be signed in to change notification settings - Fork 6
/
real.go
250 lines (232 loc) · 5.07 KB
/
real.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2018 The ZikiChombo Authors. All rights reserved. Use of this source
// code is governed by a license that can be found in the License file.
package fft
import (
"math"
"math/cmplx"
)
// Real computes an FFT for a Real only data.
//
// For even length transforms, the implementation
// uses a complex FFT of size N/2 and some pre/post processing.
// For odd length transforms, the implementtion uses a complex
// FFT of size N.
type Real struct {
ft *T // half sized for even
n int //
cBuf []complex128 //
twidz []complex128 // only for even
scaler float64 // only for even
}
// NewReal creates a new FFT transformer for
// float data of length n.
func NewReal(n int) *Real {
if n&1 == 0 {
m := n / 2
res := &Real{ft: New(m)}
res.n = n
res.cBuf = res.ft.Win(nil)
res.ft.Scale(false)
twidz := make([]complex128, m)
N := float64(n)
for i := range twidz {
s, c := math.Sincos(float64(i) * 2.0 * math.Pi / N)
twidz[i] = complex(c, -s)
}
res.twidz = twidz
res.scaler = 1.0 / math.Sqrt(N)
return res
}
res := &Real{ft: New(n)}
res.n = n
res.cBuf = res.ft.Win(nil)
return res
}
// Do performs a DFT on real data in d.
//
// d must be the size specified in the call to NewReal()
// which created r, or Do will panic.
//
// Do operates in place, overwriting d. Do returns
// d overwritten as (i.e. cast to) a HalfComplex object.
//
func (r *Real) Do(d []float64) HalfComplex {
if r.n&1 == 0 {
return r.evenDo(d)
}
return r.oddDo(d)
}
func (r *Real) evenDo(d []float64) HalfComplex {
r.pack(d)
r.ft.Do(r.cBuf)
hc := r.toHC(d)
if r.scaler == 1.0 {
return hc
}
for i := range hc {
hc[i] *= r.scaler
}
return hc
}
func (r *Real) oddDo(d []float64) HalfComplex {
for i, v := range d {
r.cBuf[i] = complex(v, 0.0)
}
r.ft.Do(r.cBuf)
res := HalfComplex(d)
res.FromCmplx(r.cBuf)
return res
}
// Inv computes the inverse transform of a real data
// from a HalfComplex object.
//
// Inv operates in place but returns the same data as hc, cast to
// a []float64.
func (r *Real) Inv(hc HalfComplex) []float64 {
if r.n&1 == 0 {
return r.evenInv(hc)
}
return r.oddInv(hc)
}
func (r *Real) evenInv(hc HalfComplex) []float64 {
if r.scaler != 1.0 {
for i := range hc {
hc[i] /= r.scaler
}
}
r.fromHC(hc)
r.ft.Inv(r.cBuf)
res := []float64(hc)
r.unpack(res)
if r.scaler != 1.0 {
sc := 1.0 / float64(len(r.cBuf))
for i := range res {
res[i] *= sc
}
}
return res
}
func (r *Real) oddInv(hc HalfComplex) []float64 {
hc.ToCmplx(r.cBuf)
r.ft.Inv(r.cBuf)
for i, c := range r.cBuf {
hc[i] = real(c)
}
return []float64(hc)
}
// Scale sets whether or not r scales the transform results.
// Scale returns whether or not r was configured to scale
// the transform results prior to calling Scale.
func (r *Real) Scale(v bool) {
if r.n&1 == 0 {
if !v {
r.scaler = 1.0
} else {
r.scaler = 1.0 / math.Sqrt(float64(r.n))
}
return
}
r.ft.Scale(v)
}
// N returns the length of the arguments to the transform
// implemented by r.
func (r *Real) N() int {
return r.n
}
// Translate r.cBuf into halfcomplex in d.
//
// Method from The DSP Book.
// some bugs fixed (wrong sign of
// twiddles, lack of appropriate DC scaling, index off by one error).
//
// http://dsp-book.narod.ru/FFTBB/0270_PDF_C14.pdf
//
func (r *Real) toHC(d []float64) HalfComplex {
const (
halfR = complex(0.5, 0)
halfI = complex(0, 0.5)
)
N := len(d)
if N != r.n {
panic("invalid length")
}
res := HalfComplex(d)
if N == 0 {
return res
}
cb := r.cBuf
h := len(cb)
a := cb[0]
f0 := halfR * a
g0 := -halfI * a
shift := r.twidz[0]
res.SetCmplx(0, complex(2.0, 0.0)*(f0+shift*g0))
if h+h == N {
res[h] = 2.0 * real(f0-g0)
}
for i := 1; i < h; i++ {
a, b := cb[i], cmplx.Conj(cb[h-i])
fi := halfR * (a + b)
gi := halfI * (b - a)
shift := r.twidz[i]
xi := fi + shift*gi
res.SetCmplx(i, xi)
}
return res
}
func (r *Real) fromHC(hc HalfComplex) {
// Method derived in part from literature and in part trial and error.
// We derive backwards fi, gi from Do, and from that derive
// the original complex value output from the forward fft.
const (
halfR = complex(0.5, 0)
halfI = complex(0, 0.5)
)
N := len(hc)
if N != r.n {
panic("invalid HalfComplex length")
}
if N == 0 {
return
}
h := len(r.cBuf)
a, b := hc.Cmplx(0), 0i
f := halfR * (a + b)
g := cmplx.Conj(r.twidz[0]) * (a - f)
r.cBuf[0] = halfR * (f/halfR - g/halfI)
var ny float64
if h+h == N {
ny = 0.5 * hc[h]
} else {
ny = 0.5 * hc[h-1]
}
r.cBuf[0] = complex(real(r.cBuf[0])+ny, imag(r.cBuf[0])-ny)
var j int
for i := 1; i < h; i++ {
j = h - i
a, b := hc.Cmplx(i), cmplx.Conj(hc.Cmplx(j))
fi := halfR * (a + b)
gi := cmplx.Conj(r.twidz[i]) * (a - fi)
r.cBuf[i] = halfR * (fi/halfR - gi/halfI)
}
}
func (r *Real) pack(d []float64) {
cb := r.cBuf
end := len(cb)
for i := 0; i < end; i++ {
cb[i] = complex(d[2*i], d[2*i+1])
}
}
func (r *Real) unpack(d []float64) {
cb := r.cBuf
end := len(cb)
var re, im float64
var v complex128
for i := 0; i < end; i++ {
v = cb[i]
re = real(v)
im = imag(v)
d[2*i] = re
d[2*i+1] = im
}
}