-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcalibration.go
65 lines (55 loc) · 1.4 KB
/
calibration.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
package hal
import (
"fmt"
)
type Measurement struct {
Expected float64 `json:"expected"`
Observed float64 `json:"observed"`
}
type Calibrator interface {
Calibrate(float64) float64
}
func CalibratorFactory(points []Measurement) (Calibrator, error) {
if points == nil {
return &noopCalibrator{}, nil
}
l := len(points)
switch l {
case 0:
return &noopCalibrator{}, nil
case 1:
return &onePointCalibration{offset: points[0].Expected - points[0].Observed}, nil
case 2:
min := points[0]
max := points[1]
if points[0].Expected >= points[1].Expected {
min = points[1]
max = points[0]
}
return &twoPointCalibration{
refLow: min.Expected,
refHigh: max.Expected,
rawLow: min.Observed,
rawHigh: max.Observed,
}, nil
default:
return nil, fmt.Errorf("Expected calibration type can only be 0, 1 or 2. Found: %d", l)
}
}
type twoPointCalibration struct {
rawLow, rawHigh, refLow, refHigh float64
}
func (c *twoPointCalibration) Calibrate(value float64) float64 {
// CorrectedValue = (((RawValue – RawLow) * ReferenceRange) / RawRange) + ReferenceLow
return (((value - c.rawLow) * (c.refHigh - c.refLow)) / (c.rawHigh - c.rawLow)) + c.refLow
}
type onePointCalibration struct {
offset float64
}
func (c *onePointCalibration) Calibrate(value float64) float64 {
return value + c.offset
}
type noopCalibrator struct{}
func (n *noopCalibrator) Calibrate(v float64) float64 {
return v
}