-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsources.go
161 lines (130 loc) · 2.85 KB
/
sources.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
package signals
import "encoding/gob"
import (
"math"
)
func init() {
gob.Register(Constant{})
gob.Register(Sine{})
gob.Register(Sinc{})
gob.Register(Pulse{})
gob.Register(Square{})
gob.Register(RampUp{})
gob.Register(RampDown{})
gob.Register(Heavyside{})
gob.Register(Sigmoid{})
gob.Register(Gauss{})
}
func DB(vol float64) float64 {
return 6 * math.Log2(vol)
}
func Vol(DB float64) float64 {
return math.Pow(2, DB/6)
}
// a Signal with constant value
type Constant struct {
Constant y
}
func NewConstant(DB float64) Constant {
return Constant{y(unitYfloat64 * Vol(DB))}
}
func (s Constant) property(p x) y {
return s.Constant
}
// a PeriodicSignal that varies sinusoidally, repeating with Cycle width.
type Sine struct {
Cycle x
}
func (s Sine) property(p x) y {
return y(math.Sin(float64(p)/float64(s.Cycle)*2*math.Pi) * unitYfloat64)
}
func (s Sine) Period() x {
return s.Cycle
}
// a Signal that is a wave 'packet', with fundamental (central) wavelength of Cycle.
type Sinc struct {
Cycle x
}
func (s Sinc) property(p x) y {
if p==0 {return unitY}
xp:=float64(p)/float64(s.Cycle)*2*math.Pi
return y(math.Sin(xp)/xp * unitYfloat64)
}
// a Signal that peaks, centred on zero, as a Gaussian distribution, width q.
type Gauss struct {
Q22 float64 // 2 * q squared
}
func (s Gauss) property(p x) y {
return y(math.Exp(-float64(p)*float64(p)/s.Q22) * unitYfloat64)
}
// a LimitedSignal that produces unitY for a Width, zero otherwise.
type Pulse struct {
Width x
}
func (s Pulse) property(p x) y {
if p > s.Width || p < 0 {
return 0
} else {
return unitY
}
}
func (s Pulse) MaxX() x {
return s.Width
}
// a PeriodicSignal that produces equal regions of +unitY then -unitY, repeating with Cycle width.
type Square struct {
Cycle x
}
func (s Square) property(p x) y {
if p%s.Cycle >= s.Cycle/2 {
return -unitY
} else {
return unitY
}
}
func (s Square) Period() x {
return s.Cycle
}
// a Signal which ramps from zero to unitY over a Period width.
type RampUp struct {
Period x
}
func (s RampUp) property(p x) y {
if p < 0 {
return 0
} else if p > s.Period {
return unitY
} else {
return y(x(unitY) / s.Period * p)
}
}
// a Signal which ramps from unitY to zero, over a Period width.
type RampDown struct {
Period x
}
func (s RampDown) property(p x) y {
if p < 0 {
return unitY
} else if p > s.Period {
return 0
} else {
return y(x(unitY) / s.Period * (s.Period - p))
}
}
// a Signal that returns +unitY for positive x and zero for negative x.
type Heavyside struct {
}
func (s Heavyside) property(p x) y {
if p < 0 {
return 0
}
return unitY
}
// a Signal that smoothly transitions from 0 to +unitY.
// with a maximum gradient (first derivative) at x=0, of Steepness.
type Sigmoid struct {
Steepness x
}
func (s Sigmoid) property(p x) y {
return y(unitYfloat64 / (1 + math.Exp(-float64(p)/float64(s.Steepness))))
}