-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
360 lines (300 loc) · 8.83 KB
/
main.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
import (
"encoding/csv"
"flag"
"fmt"
"math"
"math/cmplx"
"os"
"strconv"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/olekukonko/tablewriter"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)
func makeXYPoints(x []float64, y []float64) plotter.XYs {
pts := make(plotter.XYs, len(x))
for i := range pts {
pts[i].X = x[i]
pts[i].Y = y[i]
}
return pts
}
func plotXY(x []float64, y []float64) (*plot.Plot, error) {
p, err := plot.New()
if err != nil {
return nil, fmt.Errorf("could not create new plot: %v", err)
}
if err := plotutil.AddLinePoints(p, makeXYPoints(x, y)); err != nil {
return nil, fmt.Errorf("could not plot points: %v", err)
}
return p, nil
}
func plotDFT(data []complex128, fb float64, db bool, file string) error {
amplitudes := make([]float64, len(data))
frequencies := make([]float64, len(data))
for i, value := range data {
v := cmplx.Abs(value)
if db {
v = 20 * math.Log10(math.Max(v, 1e-3))
}
amplitudes[i] = v
frequencies[i] = fb * float64(i)
}
p, err := plotXY(frequencies, amplitudes)
if err != nil {
return fmt.Errorf("could not plot frequency amplitudes: %v", err)
}
if db {
p.Y.Label.Text = "Magnitude^2 [db]"
} else {
p.Y.Label.Text = "Magnitude^2"
}
p.X.Label.Text = "Frequency [Hz]"
if err := p.Save(10*vg.Inch, 5*vg.Inch, file); err != nil {
return fmt.Errorf("could not save plot to file %s: %v", file, err)
}
log.Infof("DFT plot saved in %s\n", file)
return nil
}
type DFTResult struct {
k int
xk complex128
}
func dftValue(c chan DFTResult, data []float64, k int, N int) {
defer wg.Done()
xk := 0i
for n, x := range data {
xValue := complex(0, -2.0*math.Pi*float64(k*n)/float64(N))
xk += complex(float64(x), 0) * cmplx.Exp(xValue)
}
c <- DFTResult{k, xk}
}
func DFT(data []float64, N int) []complex128 {
queue := make(chan DFTResult, N/2)
dft := make([]complex128, N/2)
for k := 0; k < (N/2)-1; k++ {
wg.Add(1)
go dftValue(queue, data, k, N)
}
wg.Wait()
close(queue)
for result := range queue {
dft[result.k] = result.xk
}
return dft
}
func loadData(path string) ([]float64, error) {
log.Debugf("Reading data from %s \n", path)
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("could not open file: %v", err)
}
defer file.Close()
csvReader := csv.NewReader(file)
lines, err := csvReader.ReadAll()
if err != nil {
return nil, fmt.Errorf("could not read lines from file: %v", err)
}
values := make([]float64, len(lines))
for i, value := range lines {
values[i], err = strconv.ParseFloat(value[0], 64)
if err != nil {
return nil, fmt.Errorf("could not parse line %d in file %s: %v", i, path, err)
}
}
log.Debugf("Read %d points\n", len(values))
return values, nil
}
// Calculates the indices of the first n harmonic frequencies, taking into account the
// aliasing effects.
func aliasedHarmonics(
signalFreq float64,
baseFreq float64,
samplingFreq float64,
nHarmonics int,
) []int {
mSig := int(signalFreq / baseFreq)
log.Debugf("Signal index: %d\n", mSig)
N := int(samplingFreq / baseFreq)
log.Debugf("DFT len : %d\n", N)
mh := make([]int, nHarmonics)
for i := 0; i < nHarmonics; i++ {
k := i + 1
m := (k * mSig) % N
if m < N/2 {
mh[i] = m
} else {
mh[i] = N - m
}
}
log.Debugf("Aliased harmonics indices %v\n", mh)
aliasedHarmonicFrequencies := make([]float64, len(mh))
for i, value := range mh {
aliasedHarmonicFrequencies[i] = baseFreq * float64(value)
}
log.Debugf("Aliased harmonics freqs %.3g\n", aliasedHarmonicFrequencies)
return mh
}
func intInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// THD (Total Harmonic Distorsion). Defined as the ratio of the sum of the powers
// of harmonic components to the power of the signal frequency.
func THD(dft []complex128, signalIndices []int, harmonicIndices []int) float64 {
dftMagnitudes := convertToMagnitudeSquared(dft)
harmonicsMagnitude := 0.0
for idx, i := range harmonicIndices {
if idx > 0 { // skip first value (base freq term)
harmonicsMagnitude += dftMagnitudes[i]
}
}
signalMagnitude := 0.0
for _, idx := range signalIndices {
signalMagnitude += dftMagnitudes[idx]
}
return 20.0 * math.Log10(math.Sqrt(harmonicsMagnitude/signalMagnitude))
}
// SNHR (Signal to Non Harmonic Ratio). Defined as the ratio of power of the signal
// frequency to the sum of the powers of non-harmonic components
func SNHR(dft []complex128, signalIndices []int, harmonicIndices []int) float64 {
dftMagnitudes := convertToMagnitudeSquared(dft)
nonHarmonicsMagnitude := 0.0
for idx, value := range dftMagnitudes {
if !intInSlice(idx, harmonicIndices) && idx > 0 { // skip harmonics and 0 freq term
nonHarmonicsMagnitude += value
}
}
signalMagnitude := 0.0
for _, idx := range signalIndices {
signalMagnitude += dftMagnitudes[idx]
}
return 20.0 * math.Log10(math.Sqrt(signalMagnitude/nonHarmonicsMagnitude))
}
// SFDR (Spurious Free Dynamic Range). Defined as the ratio of power of the signal frequency
// to the power of the of the next largest noise or harmonic distortion.
func SFDR(dft []complex128, signalIndices []int) float64 {
dftMagnitudes := convertToMagnitudeSquared(dft)
nonSignalMaxMagnitude := 0.0
for idx, value := range dftMagnitudes {
if !intInSlice(idx, signalIndices) && idx > 0 { // skip harmonics
if value > nonSignalMaxMagnitude {
nonSignalMaxMagnitude = value
}
}
}
signalMagnitude := 0.0
for _, idx := range signalIndices {
signalMagnitude += dftMagnitudes[idx]
}
return 20.0 * math.Log10(math.Sqrt(signalMagnitude/nonSignalMaxMagnitude))
}
// SINAD (Signal to Noise And Distortion). Defined as the ratio of the power of the signal
// to the total power of the noise and harmonic distortions.
func SINAD(dft []complex128, signalIndices []int) float64 {
dftMagnitudes := convertToMagnitudeSquared(dft)
nonSignalMagnitude := 0.0
for idx, value := range dftMagnitudes {
if !intInSlice(idx, signalIndices) && idx > 0 { // skip harmonics
nonSignalMagnitude += value
}
}
signalMagnitude := 0.0
for _, idx := range signalIndices {
signalMagnitude += dftMagnitudes[idx]
}
return 20.0 * math.Log10(math.Sqrt(signalMagnitude/nonSignalMagnitude))
}
// ENOB (Effective Number Of Bits). ENOB specifies the number of bits of an ideal ADC
// that would have the same resolution as the tested, real ADC.
func ENOB(sinad float64) float64 {
return (sinad - 1.76) / 6.02
}
func convertToMagnitudeSquared(data []complex128) []float64 {
x := make([]float64, len(data))
for i, value := range data {
a := cmplx.Abs(value)
x[i] = a * a
}
return x
}
var wg sync.WaitGroup
var (
input = flag.String("input", "", "Input file path.")
fsig = flag.Float64("fsig", 0.0, "Original signal frequency.")
fsam = flag.Float64("fsam", 0.0, "Sampling frequency.")
dftlen = flag.Int("dftlen", 1024, "Length of the DFT.")
loglevel = flag.String("loglevel", "info", "Logging level.")
)
func main() {
fmt.Println("Starting Fourier-ADC")
flag.Parse()
logLevel, err := log.ParseLevel(*loglevel)
if err != nil {
log.Fatalf("could not understand provided loglevel %v, %v", *loglevel, err)
}
log.Infof("Setting logging level %v", logLevel)
log.SetLevel(logLevel)
data, err := loadData(*input)
if err != nil {
log.Fatalf("could not load data from the file %s: %v", *input, err)
}
fs := *fsam
freq := *fsig
fb := fs / float64(*dftlen)
table := tablewriter.NewWriter(os.Stdout)
tableData := [][]string{
{
strconv.FormatFloat(freq, 'f', 3, 64),
strconv.FormatFloat(fs, 'f', 3, 64),
strconv.FormatInt(int64(*dftlen), 10),
strconv.FormatFloat(fb, 'f', 3, 64),
},
}
table.SetHeader([]string{"Fsig [Hz]", "Fs [Hz]", "DFT len", "Fb [Hz]"})
for _, v := range tableData {
table.Append(v)
}
fmt.Println("Input signal parameters:")
table.Render()
started := time.Now()
dft := DFT(data, *dftlen)
elapsed := time.Since(started)
log.Debugf("Calculated DFT in %s.\n", elapsed)
plotFileName := strings.Split(*input, ".")[0] + ".png"
plotDFT(dft, fb, true, plotFileName)
mSig := int(freq / fb)
aliasedHarmonicIndices := aliasedHarmonics(freq, fb, fs, 10)
signalIndices := []int{mSig}
thd := THD(dft, signalIndices, aliasedHarmonicIndices)
snhr := SNHR(dft, signalIndices, aliasedHarmonicIndices)
sfdr := SFDR(dft, signalIndices)
sinad := SINAD(dft, signalIndices)
enob := ENOB(sinad)
table = tablewriter.NewWriter(os.Stdout)
tableData = [][]string{
{
strconv.FormatFloat(thd, 'f', 3, 64),
strconv.FormatFloat(snhr, 'f', 3, 64),
strconv.FormatFloat(sfdr, 'f', 3, 64),
strconv.FormatFloat(sinad, 'f', 3, 64),
strconv.FormatFloat(enob, 'f', 3, 64),
},
}
table.SetHeader([]string{"THD [db]", "SNHR [dB]", "SFDR [dB]", "SINAD [dB]", "Enob [bits]"})
for _, v := range tableData {
table.Append(v)
}
fmt.Println("ADC metrics:")
table.Render()
}