-
Notifications
You must be signed in to change notification settings - Fork 36
/
response_type.go
284 lines (224 loc) · 7.6 KB
/
response_type.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
package stationxml
import (
"bytes"
"encoding/gob"
"encoding/xml"
"math"
"math/cmplx"
)
const LaplaceRadiansSecondPzTransferFunction = "LAPLACE (RADIANS/SECOND)"
type Float struct {
Value float64 `xml:",chardata"`
}
type Units struct {
Name string `xml:"Name"`
Description string `xml:"Description,omitempty"`
}
type ApproximationBound struct {
Value float64 `xml:",chardata"`
}
type NumeratorCoefficient struct {
I int `xml:"i,attr"`
Value float64 `xml:",chardata"`
}
type PolynomialCoefficient struct {
Number int `xml:"number,attr"`
Value float64 `xml:",chardata"`
}
type CoefficientNumerator struct {
Number int `xml:"number,attr"`
Value float64 `xml:",chardata"`
}
type CoefficientDenominator struct {
Number int `xml:"number,attr"`
Value float64 `xml:",chardata"`
}
type PoleZero struct {
Number int `xml:"number,attr"`
Real Float `xml:"Real"`
Imaginary Float `xml:"Imaginary"`
}
type CoefficientsType struct {
ResourceId string `xml:"resourceId,attr,omitempty"`
Name string `xml:"name,attr,omitempty"`
Description string `xml:"description,attr,omitempty"`
InputUnits Units `xml:"InputUnits"`
OutputUnits Units `xml:"OutputUnits"`
CfTransferFunctionType string `xml:"CfTransferFunctionType"`
Numerators []CoefficientNumerator `xml:"Numerator,omitempty"`
Denominators []CoefficientDenominator `xml:"Denominator,omitempty"`
}
type DecimationType struct {
InputSampleRate float64 `xml:"InputSampleRate"`
Factor int `xml:"Factor"`
Offset int `xml:"Offset"`
Delay float64 `xml:"Delay"`
Correction float64 `xml:"Correction"`
}
type FirType struct {
ResourceId string `xml:"resourceId,attr"`
Name string `xml:"name,attr,omitempty"`
Description string `xml:"description,attr,omitempty"`
InputUnits Units `xml:"InputUnits"`
OutputUnits Units `xml:"OutputUnits"`
Symmetry string `xml:"Symmetry"`
NumeratorCoefficients []NumeratorCoefficient `xml:"NumeratorCoefficient"`
}
type PolesZerosType struct {
ResourceId string `xml:"resourceId,attr"`
Name string `xml:"name,attr,omitempty"`
Description string `xml:"description,attr,omitempty"`
InputUnits Units `xml:"InputUnits"`
OutputUnits Units `xml:"OutputUnits"`
PzTransferFunctionType string `xml:"PzTransferFunctionType"`
NormalizationFactor float64 `xml:"NormalizationFactor"`
NormalizationFrequency float64 `xml:"NormalizationFrequency"`
Zeros []PoleZero `xml:"Zero"`
Poles []PoleZero `xml:"Pole"`
}
// Gain ccalculates the poles and zeros response gain at a given frequency
func (pz PolesZerosType) Gain(freq float64) float64 {
var w complex128
switch pz.PzTransferFunctionType {
case LaplaceRadiansSecondPzTransferFunction:
w = complex(0.0, 2.0*math.Pi*freq)
default:
w = complex(0.0, freq)
}
h := complex(float64(1.0), float64(0.0))
for _, zero := range pz.Zeros {
h *= (w - complex(zero.Real.Value, zero.Imaginary.Value))
}
for _, pole := range pz.Poles {
h /= (w - complex(pole.Real.Value, pole.Imaginary.Value))
}
return cmplx.Abs(h)
}
type PolynomialType struct {
ResourceId string `xml:"resourceId,attr,omitempty"`
Name string `xml:"name,attr,omitempty"`
Description string `xml:"description,attr,omitempty"`
InputUnits Units `xml:"InputUnits"`
OutputUnits Units `xml:"OutputUnits"`
ApproximationType string `xml:"ApproximationType"`
FrequencyLowerBound float64 `xml:"FrequencyLowerBound"`
FrequencyUpperBound float64 `xml:"FrequencyUpperBound"`
ApproximationLowerBound ApproximationBound `xml:"ApproximationLowerBound"`
ApproximationUpperBound ApproximationBound `xml:"ApproximationUpperBound"`
MaximumError float64 `xml:"MaximumError"`
Coefficients []PolynomialCoefficient `xml:"Coefficient,omitempty"`
}
func (p PolynomialType) Value(input float64) float64 {
var value float64
for n, c := range p.Coefficients {
value += c.Value * math.Pow(input, float64(n))
}
return value
}
type StageGain struct {
Value float64 `xml:"Value"`
Frequency float64 `xml:"Frequency"`
}
type ResponseStageType struct {
Number int `xml:"number,attr"`
Coefficients *CoefficientsType `xml:"Coefficients,omitempty"`
Decimation *DecimationType `xml:"Decimation,omitempty"`
FIR *FirType `xml:"FIR,omitempty"`
PolesZeros *PolesZerosType `xml:"PolesZeros,omitempty"`
Polynomial *PolynomialType `xml:"Polynomial,omitempty"`
StageGain *StageGain `xml:"StageGain,omitempty"`
}
// clone two responses to avoid shared backing arrays
func (r *ResponseStageType) Clone() (ResponseStageType, error) {
var buff bytes.Buffer
if err := gob.NewEncoder(&buff).Encode(r); err != nil {
return ResponseStageType{}, err
}
var c ResponseStageType
if err := gob.NewDecoder(&buff).Decode(&c); err != nil {
return ResponseStageType{}, err
}
return c, nil
}
type InstrumentSensitivity struct {
Value float64 `xml:"Value"`
Frequency float64 `xml:"Frequency"`
InputUnits Units `xml:"InputUnits"`
OutputUnits Units `xml:"OutputUnits"`
}
type InstrumentPolynomial struct {
ResourceId string `xml:"resourceId,attr,omitempty"`
Name string `xml:"name,attr"`
Description string `xml:"description,attr,omitempty"`
InputUnits Units `xml:"InputUnits"`
OutputUnits Units `xml:"OutputUnits"`
ApproximationType string `xml:"ApproximationType"`
FrequencyLowerBound float64 `xml:"FrequencyLowerBound"`
FrequencyUpperBound float64 `xml:"FrequencyUpperBound"`
ApproximationLowerBound ApproximationBound `xml:"ApproximationLowerBound"`
ApproximationUpperBound ApproximationBound `xml:"ApproximationUpperBound"`
MaximumError float64 `xml:"MaximumError"`
Coefficients []PolynomialCoefficient `xml:"Coefficient,omitempty"`
}
// ResponseType is a struct that mimics the StationXML ResponseType element, but is not constrained to a particular version.
type ResponseType struct {
XMLName xml.Name `xml:"Response"`
ResourceId string `xml:"resourceId,attr,omitempty"`
InstrumentSensitivity *InstrumentSensitivity `xml:"InstrumentSensitivity,omitempty"`
InstrumentPolynomial *InstrumentPolynomial `xml:"InstrumentPolynomial,omitempty"`
Stages []ResponseStageType `xml:"Stage,omitempty"`
// used for instrument polynomial calculations
frequency float64
}
func NewResponseType(data []byte) (*ResponseType, error) {
var s ResponseType
if err := s.Unmarshal(data); err != nil {
return nil, err
}
return &s, nil
}
func (r *ResponseType) Scale() float64 {
scale := 1.0
for _, s := range r.Stages {
if s.StageGain == nil || s.StageGain.Value == 0.0 {
continue
}
scale *= s.StageGain.Value
}
return scale
}
func (r *ResponseType) PolynomialType() *PolynomialType {
for _, s := range r.Stages {
if s.Polynomial == nil {
continue
}
return s.Polynomial
}
return nil
}
func (r *ResponseType) PolynomialCoefficients() []PolynomialCoefficient {
var coeffs []PolynomialCoefficient
if p := r.PolynomialType(); p != nil && len(p.Coefficients) > 0 {
coeffs = append(coeffs, p.Coefficients[0])
}
if p := r.PolynomialType(); p != nil && len(p.Coefficients) > 1 {
if scale := r.Scale(); scale != 0.0 {
coeffs = append(coeffs, PolynomialCoefficient{
Number: 1,
Value: p.Coefficients[1].Value / scale,
})
}
}
return coeffs
}
func (r *ResponseType) Unmarshal(data []byte) error {
return xml.Unmarshal(data, r)
}
func (r ResponseType) Marshal() ([]byte, error) {
body, err := xml.MarshalIndent(r, "", " ")
if err != nil {
return nil, err
}
head := []byte(xml.Header)
return append(head, append(body, '\n')...), nil
}