-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.go
274 lines (236 loc) · 6.49 KB
/
signal.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
// Package signal provides types and functions to aid in signal processing.
package signal
import (
"math"
)
const twopi = 2 * math.Pi
const DefaultSize = 4096
type Sampler interface {
// Sample reads values from src at phase by interval and returns next phase to sample.
//
// The side effect resulting from sampling is implementation specific.
Sample(src Continuous, interval, phase float64) float64
}
// Continuous represents a continuous-time signal.
//
// Effectively, many things may be represented as continuous. A function returning
// math.Sin(2*math.Pi*t) samples a sine wave while Discrete.Interpolate returns an
// interpolated value from its table, with both satisfying this type.
type Continuous func(t float64) float64
// Sample satisfies Sampler interface.
//
// The side effect of fn sampling src can not be known, so this returns phase+interval.
func (fn Continuous) Sample(src Continuous, interval, phase float64) float64 {
_ = fn(src(phase))
return phase + interval
}
// Discrete represents a discrete-time signal.
//
// If building a lookup table, let sampling interval equal the recipricol of len(Discrete).
//
// const n = 1024
// sig := make(Discrete, n)
// sig.Sample(SineFunc, 1/n, 0)
//
// If samples are intended to be played back in sequence, provide normalized frequency
// against output sample rate; e.g. to sample four seconds of 440Hz sine wave at 44.1kHz
//
// r := 44100.0
// t := 4.0
// out := make(Discrete, int(r*t)) // allocate four seconds of space
// out.Sample(SineFunc, 440/r, 0) // sample 440Hz sine wave
//
// To play these samples over four second period, use an oscillator as clock.
//
// osc := snd.NewOscil(out, 1/t, nil) // package dasa.cc/snd
//
// TODO document sampling at different rates.
type Discrete []float64
// Sample satisfies Sampler interface.
func (sig Discrete) Sample(src Continuous, interval, phase float64) float64 {
for i := 0; i < len(sig); i++ {
sig[i] = src(phase)
phase += interval
}
return phase
}
// Interp uses the fractional component of t to return an interpolated sample.
//
// TODO currently does linear interpolation...
func (sig Discrete) Interp(t float64) float64 {
if t <= -0 {
t = -t
}
t -= float64(int(t)) // fractional
t *= float64(len(sig)) // integer is index, fractional is amount to interpolate to next index
frac := t - float64(int(t))
i := int(t - frac)
if frac == 0 {
return sig[i]
}
j := i + 1
if j == len(sig) {
j = 0
}
return (1-frac)*sig[i] + frac*sig[j]
}
// At uses the fractional component of t to return the sample at the truncated index.
func (sig Discrete) At(t float64) float64 {
if t <= -0 {
t = -t
}
t -= float64(int(t))
t *= float64(len(sig))
return sig[int(t)]
}
func (sig Discrete) Index(i int) float64 {
return sig[i&(len(sig)-1)]
}
// Normalize alters sig so values belong to [-1..1].
func (sig Discrete) Normalize() {
var max float64
for _, x := range sig {
a := math.Abs(x)
if max < a {
max = a
}
}
for i, x := range sig {
sig[i] = x / max
}
}
// NormalizeRange alters sig so values belong to [s..e].
//
// Calling this method for values that already occupy [s..e] will degrade values
// further due to round-off error.
func (sig Discrete) NormalizeRange(s, e float64) {
if s > e {
s, e = e, s
}
n := e - s
var min, max float64
for _, x := range sig {
if min > x {
min = x
}
if max < x {
max = x
}
}
r := max - min
for i, x := range sig {
sig[i] = s + n*(x-min)/r
}
}
// Reverse sig in place so the first element becomes the last and the last element becomes the first.
func (sig Discrete) Reverse() {
for l, r := 0, len(sig)-1; l < r; l, r = l+1, r-1 {
sig[l], sig[r] = sig[r], sig[l]
}
}
// UnitInverse sets each sample to 1 - x.
func (sig Discrete) UnitInverse() {
for i, x := range sig {
sig[i] = 1 - x
}
}
// AdditiveInverse sets each sample to -x.
func (sig Discrete) AdditiveInverse() {
for i, x := range sig {
sig[i] = -x
}
}
func (sig Discrete) MultiplyScalar(x float64) {
for i := range sig {
sig[i] *= x
}
}
func (sig Discrete) Multiply(xs Discrete) {
for i := range sig {
sig[i] *= xs.At(float64(i) / float64(len(sig)))
}
}
// AdditiveSynthesis adds the fundamental, fd, for the partial harmonic, pth, to sig.
func (sig Discrete) AdditiveSynthesis(fd Discrete, pth int) {
for i := range sig {
j := i * pth % (len(fd) - 1)
sig[i] += fd[j] * (1 / float64(pth))
}
}
// SineFunc is the continuous signal of a sine wave.
func SineFunc(t float64) float64 {
return math.Sin(twopi * t)
}
// Sine returns a discrete sample of SineFunc.
func Sine() Discrete {
sig := make(Discrete, DefaultSize)
sig.Sample(SineFunc, 1./DefaultSize, 0)
return sig
}
// TriangleFunc is the continuous signal of a triangle wave.
func TriangleFunc(t float64) float64 {
// return 2*math.Abs(SawtoothFunc(t)) - 1
return 4*math.Abs(t+0.25-math.Floor(0.75+t)) - 1
}
// Triangle returns a discrete sample of TriangleFunc.
func Triangle() Discrete {
sig := make(Discrete, DefaultSize)
sig.Sample(TriangleFunc, 1./DefaultSize, 0)
return sig
}
// SquareFunc is the continuous signal of a square wave.
func SquareFunc(t float64) float64 {
if math.Signbit(SineFunc(t)) {
return -1
}
return 1
}
// Square returns a discrete sample of SquareFunc.
func Square() Discrete {
sig := make(Discrete, DefaultSize)
sig.Sample(SquareFunc, 1./DefaultSize, 0)
return sig
}
// SawtoothFunc is the continuous signal of a sawtooth wave.
func SawtoothFunc(t float64) float64 {
return 2 * (t - math.Floor(0.5+t))
}
// Sawtooth returns a discrete sample of SawtoothFunc.
func Sawtooth() Discrete {
sig := make(Discrete, DefaultSize)
sig.Sample(SawtoothFunc, 1./DefaultSize, 0)
return sig
}
func ExpDecayFunc(t float64) float64 {
return math.Exp(twopi * -t)
}
// ExpDecay returns a discrete sample of ExpDecayFunc.
func ExpDecay() Discrete {
sig := make(Discrete, DefaultSize)
sig.Sample(ExpDecayFunc, 1./DefaultSize, 0)
return sig
}
// fundamental default used for sinusoidal synthesis.
var fundamental = Sine()
// SquareSynthesis adds odd partial harmonics belonging to [3..n], creating a sinusoidal wave.
func SquareSynthesis(n int) Discrete {
sig := Sine()
for i := 3; i <= n; i += 2 {
sig.AdditiveSynthesis(fundamental, i)
}
sig.Normalize()
return sig
}
// SawtoothSynthesis adds all partial harmonics belonging to [2..n], creating a sinusoidal wave
// that is the inverse of a sawtooth.
func SawtoothSynthesis(n int) Discrete {
sig := Sine()
for i := 2; i <= n; i++ {
sig.AdditiveSynthesis(fundamental, i)
}
sig.Normalize()
return sig
}
type Float interface {
~float32 | ~float64
}