-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1d.go
299 lines (253 loc) · 5.82 KB
/
1d.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"bufio"
"flag"
"fmt"
"io"
"math"
"os"
"path"
"strings"
"github.com/barnex/fftw"
"github.com/mumax/3/httpfs"
"github.com/mumax/3/util"
)
// main loop for table input
func mainTable() {
// process files
for _, f := range flag.Args() {
outFname := util.NoExt(f) + "_fft" + path.Ext(f)
processTable(f, outFname)
}
}
// FFT a single table file
func processTable(infname, outfname string) {
// read input file
in := httpfs.MustOpen(infname)
defer in.Close()
header, data := ReadTable(in)
// Process data
if *flag_Interp {
data = interp(data) // interpolate to equidistant times
}
// determine frequency step, beware zero padding
const TIME = 0 // time column
rows := len(data[TIME])
deltaT := data[TIME][rows-1] - data[TIME][0]
deltaT *= float32(*flag_Pad)
deltaF := 1 / (deltaT)
// mumax outputs one row too much
rows--
for i := range data {
data[i] = data[i][:rows]
}
window := windows[*flag_Win]
if window == nil {
panic(fmt.Sprint("invalid window:", *flag_Win, " options:", windows))
}
for c := range data {
applyWindow(data[c], window)
data[c] = zeropad(data[c], rows*(*flag_Pad))
}
transf := FFT(data[1:]) // FFT all but first (time) column
if *flag_NormCol == 1 {
errPrintln("-divcol may start at column 2")
os.Exit(1)
}
if *flag_NormCol > 1 {
divCol(transf, transf[*flag_NormCol-2])
}
output := reduce(transf, deltaF)
outHdr := makeFFTHeader(header)
if *flag_CleanPhase {
for c := range output[1:] {
if strings.HasPrefix(outHdr[c], "Ph_") {
cleanPhase(output[c])
}
}
}
// write output file
var out io.Writer
if *flag_Stdout {
out = os.Stdout
} else {
o := httpfs.MustCreate(outfname)
defer o.Close()
out = o
}
writeTable(out, outHdr, output)
}
// turn original table header into FFT header
func makeFFTHeader(origHeader []string) []string {
fftHdr := []string{"f (Hz)"}
for _, h := range origHeader[1:] {
for _, o := range outputs {
if !*o.Enabled {
continue
}
fftHdr = append(fftHdr, o.Name+"_"+h)
}
}
return fftHdr
}
func cleanPhase(data []float32) {
for i := 1; i < len(data); i++ {
for data[i]-data[i-1] > math.Pi {
data[i] -= 2 * math.Pi
}
for data[i]-data[i-1] < -math.Pi {
data[i] += 2 * math.Pi
}
}
}
func reduce(transf [][]complex64, deltaF float32) [][]float32 {
// count # outputs per input column
outPerCol := 0
for _, o := range outputs {
if *o.Enabled {
outPerCol++
}
}
nOut := 1 + outPerCol*len(transf)
out := make([][]float32, nOut)
for i := range out {
out[i] = make([]float32, len(transf[0]))
}
for r := range transf[0] {
// frequency first
freq := float32(r) * deltaF
out[0][r] = freq
i := 0
for c := range transf {
v := transf[c][r]
for _, o := range outputs {
if !*o.Enabled {
continue
}
out[i+1][r] = o.Filter(v)
i++
}
}
}
return out
}
// output FFT data table
func writeTable(out io.Writer, header []string, output [][]float32) {
WriteHeader(out, header)
// write data
for r := range output[0] {
for c := range output {
Fprint(out, output[c][r], "\t")
}
Fprint(out, "\n")
}
}
func WriteHeader(out io.Writer, header []string) {
Fprint(out, "# ", header[0])
for _, h := range header[1:] {
Fprint(out, "\t", h)
}
Fprint(out, "\n")
}
// interpolate input data to equidistant time points in column 0.
func interp(data [][]float32) [][]float32 {
interp := make([][]float32, len(data))
for i := range interp {
interp[i] = make([]float32, len(data[i]))
}
const TIME = 0 // time column
rows := len(data[TIME])
deltaT := data[TIME][rows-1] - data[TIME][0]
time := data[TIME]
time0 := time[0] // start time, not neccesarily 0
si := 0 // source index
for di := 0; di < len(time); di++ { // dst index
want := time0 + float32(di)*deltaT/float32(len(time)) // wanted time
for si < len(time)-1 && !(time[si] <= want && time[si+1] > want && time[si] != time[si+1]) {
si++
}
x := (want - time[si]) / (time[si+1] - time[si])
if x < 0 || x > 1 {
panic(fmt.Sprint("x=", x))
}
for c := range interp {
interp[c][di] = (1-x)*data[c][si] + x*data[c][si+1]
}
}
return interp
}
// zero-padd data to fit length
func zeropad(data []float32, length int) []float32 {
if len(data) == length {
return data
}
padded := make([]float32, length)
copy(padded, data)
return padded
}
func FFT(data [][]float32) [][]complex64 {
cols := len(data)
transf := make([][]complex64, cols)
for c := range transf {
rows := len(data[c])
transf[c] = make([]complex64, rows/2+1)
plan := fftw.PlanR2C([]int{rows}, data[c], transf[c], fftw.ESTIMATE)
plan.Execute()
// normalize FFT
norm := float32(math.Sqrt(float64(len(data[c]))))
for i := range transf[c] {
transf[c][i] /= complex64(complex(norm, 0))
}
}
// remove nyquist freq
for i := range transf {
transf[i] = transf[i][:len(transf[i])-1]
}
return transf
}
func divCol(data [][]complex64, norm []complex64) {
for i := range data[0] {
for c := range data {
n := 1 / norm[i]
data[c][i] *= n
}
}
}
// read data table
func ReadTable(in_ io.Reader) (header []string, data [][]float32) {
in := bufio.NewReader(in_)
header = readHeader(in)
cols := len(header)
data = readData(in, cols)
return
}
func readData(in *bufio.Reader, cols int) [][]float32 {
data := make([][]float32, cols)
var v float32
var err error
c := 0 // current column
_, err = fmt.Fscan(in, &v)
for err == nil {
data[c] = append(data[c], v)
c = (c + 1) % cols
_, err = fmt.Fscan(in, &v)
}
if err != io.EOF {
panic(err)
}
if c != 0 {
panic("truncated data")
}
return data
}
func readHeader(in *bufio.Reader) []string {
hdrBytes, _, err2 := in.ReadLine()
check(err2)
hdr := string(hdrBytes)
if hdr[0] != '#' {
panic(fmt.Sprint("invalid table header: ", hdr))
}
hdr = hdr[2:]
quants := strings.Split(hdr, "\t")
return quants
}