-
Notifications
You must be signed in to change notification settings - Fork 8
/
ffagc.go
77 lines (59 loc) · 1.75 KB
/
ffagc.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
package dsp
import "math"
type FeedForwardAGC struct {
sampleHistory []complex64
reference float64
numSamples int
}
const maxGain = float64(1e-4)
func MakeFeedForwardAGC(numSamples int, reference float32) *FeedForwardAGC {
return &FeedForwardAGC{
sampleHistory: make([]complex64, numSamples),
reference: float64(reference),
numSamples: numSamples,
}
}
func (f *FeedForwardAGC) Work(input []complex64) []complex64 {
var gain float64
output := make([]complex64, len(input))
samples := append(f.sampleHistory, input...)
for i := 0; i < len(output); i++ {
maxEnv := maxGain
for j := 0; j < len(f.sampleHistory); j++ {
maxEnv = math.Max(maxEnv, envelope(samples[i+j]))
}
gain = f.reference / maxEnv
output[i] = complex(float32(gain)*real(samples[i]), float32(gain)*imag(samples[i]))
}
f.sampleHistory = samples[len(samples)-f.numSamples:]
return output
}
func (f *FeedForwardAGC) WorkBuffer(input, output []complex64) int {
var gain float64
samples := append(f.sampleHistory, input...)
if len(output) < len(input) {
panic("There is not enough space in output buffer")
}
for i := 0; i < len(input); i++ {
maxEnv := maxGain
for j := 0; j < len(f.sampleHistory); j++ {
maxEnv = math.Max(maxEnv, envelope(samples[i+j]))
}
gain = f.reference / maxEnv
output[i] = complex(float32(gain)*real(samples[i]), float32(gain)*imag(samples[i]))
}
f.sampleHistory = samples[len(samples)-f.numSamples:]
return len(input)
}
func (dc *FeedForwardAGC) PredictOutputSize(inputLength int) int {
return inputLength
}
func envelope(c complex64) float64 {
realAbs := math.Abs(float64(real(c)))
imagAbs := math.Abs(float64(real(c)))
if realAbs > imagAbs {
return realAbs + 0.4*imagAbs
} else {
return imagAbs + 0.4*realAbs
}
}