-
Notifications
You must be signed in to change notification settings - Fork 36
/
fir.go
133 lines (116 loc) · 2.71 KB
/
fir.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
package main
import (
"sort"
"strings"
stationxml "github.com/GeoNet/delta/internal/stationxml/v1.1"
)
type FIR struct {
Causal bool `yaml:"causal"`
Symmetry string `yaml:"symmetry"`
Decimation float64 `yaml:"decimation"`
Gain float64 `yaml:"gain"`
Notes string `yaml:"notes"`
Factors []float64 `yaml:"factors"`
}
func (f FIR) SymmetryLookup() string {
switch strings.ToUpper(f.Symmetry) {
case "EVEN":
return "SymmetryEven"
case "ODD":
return "SymmetryOdd"
default:
return "SymmetryNone"
}
}
func (f FIR) SymmetryType() stationxml.Symmetry {
switch strings.ToUpper(f.Symmetry) {
case "EVEN":
return stationxml.EvenSymmetry
case "ODD":
return stationxml.OddSymmetry
default:
return stationxml.NoneSymmetry
}
}
func (f FIR) Correction(sps float64) float64 {
switch strings.ToUpper(f.Symmetry) {
case "EVEN":
return float64(len(f.Factors)*2-1) / (2.0 * sps)
case "ODD":
return float64((len(f.Factors)-1)*2+1-1) / (2.0 * sps)
default:
return float64(len(f.Factors)-1) / (2.0 * sps)
}
}
func (fir FIR) Stage(filter string, stage ResponseStage, count int, freq, rate float64) stationxml.ResponseStageType {
var corr float64
if v := float64(fir.Decimation); v > 1.0 {
if corr = stage.Correction; corr == 0.0 {
corr = fir.Correction(rate / v)
}
}
var coeffs []stationxml.NumeratorCoefficient
for n, c := range fir.Factors {
coeffs = append(coeffs, stationxml.NumeratorCoefficient{
I: n + 1,
Value: c,
})
}
return stationxml.ResponseStageType{
Number: stationxml.CounterType(count),
FIR: &stationxml.FIRType{
BaseFilterType: stationxml.BaseFilterType{
ResourceId: "FIR#" + filter,
Name: stage.Lookup,
Description: "",
InputUnits: stationxml.UnitsType{
Name: stage.InputUnits,
},
OutputUnits: stationxml.UnitsType{
Name: stage.OutputUnits,
},
},
Symmetry: fir.SymmetryType(),
NumeratorCoefficient: coeffs,
},
Decimation: &stationxml.DecimationType{
InputSampleRate: stationxml.FrequencyType{
FloatType: stationxml.FloatType{
Value: rate,
},
},
Factor: int(fir.Decimation),
Delay: stationxml.FloatType{
Value: corr,
},
Correction: stationxml.FloatType{
Value: corr,
},
},
StageGain: &stationxml.GainType{
Value: func() float64 {
if stage.Gain != 0.0 {
return stage.Gain
}
return 1.0
}(),
Frequency: freq,
},
}
}
type firMap map[string]FIR
func (f firMap) Keys() []string {
var keys []string
for k := range f {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func (f firMap) Values() []FIR {
var values []FIR
for _, k := range f.Keys() {
values = append(values, f[k])
}
return values
}