-
Notifications
You must be signed in to change notification settings - Fork 8
/
costasLoop.go
218 lines (175 loc) · 5.08 KB
/
costasLoop.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
package dsp
import (
"github.com/racerxdl/segdsp/tools"
"math"
)
type CostasLoop interface {
ComplexWorker
GetError() float32
GetAverageError() float32
GetFrequency() float32
GetFrequencyHz(float32) float32
GetFrequencyShift() []float32
}
// region Base Costas Loop
type baseCostasLoop struct {
avgError float32
error float32
frequencyShift []float32
lastBuffLen int
}
func (cl *baseCostasLoop) GetError() float32 {
return cl.error
}
func (cl *baseCostasLoop) GetAverageError() float32 {
return cl.avgError
}
func (cl *baseCostasLoop) GetFrequencyShift() []float32 {
if cl.lastBuffLen == 0 {
return make([]float32, 0)
}
return cl.frequencyShift[:cl.lastBuffLen]
}
func (cl *baseCostasLoop) PredictOutputSize(input int) int {
return input
}
// endregion
// region Second Order Costas Loop
type CostasLoop2 struct {
controlLoop
baseCostasLoop
}
func MakeCostasLoop2WithFrequencyRange(loopBandwidth, minRelativeFrequency, maxRelativeFrequency float32) CostasLoop {
cl := makeControlLoop(loopBandwidth, minRelativeFrequency, maxRelativeFrequency)
cl2 := &CostasLoop2{controlLoop: *cl, baseCostasLoop: baseCostasLoop{error: 0, lastBuffLen: 0}}
return cl2
}
func MakeCostasLoop2(loopBandwidth float32) CostasLoop {
return MakeCostasLoop2WithFrequencyRange(loopBandwidth, -1, 1)
}
func (cl *CostasLoop2) Work(input []complex64) []complex64 {
output := make([]complex64, cl.PredictOutputSize(len(input)))
cl.WorkBuffer(input, output)
return output
}
func (cl *CostasLoop2) WorkBuffer(input, output []complex64) int {
if len(cl.frequencyShift) < len(input) {
cl.frequencyShift = make([]float32, len(input))
}
cl.lastBuffLen = len(input)
for i := 0; i < len(input); i++ {
n := tools.PhaseToComplex(-cl.phase)
output[i] = input[i] * n
cl.error = real(output[i]) * imag(output[i])
cl.error = tools.Clip(cl.error, 1)
cl.avgError += cl.error
cl.AdvanceLoop(cl.error)
cl.phaseWrap()
cl.frequencyLimit()
cl.frequencyShift[i] = cl.freq
}
cl.avgError /= float32(len(input) + 1)
return len(input)
}
// endregion
// region 4th Order Costas Loop
type CostasLoop4 struct {
controlLoop
baseCostasLoop
}
func MakeCostasLoop4WithFrequencyRange(loopBandwidth, minRelativeFrequency, maxRelativeFrequency float32) CostasLoop {
cl := makeControlLoop(loopBandwidth, minRelativeFrequency, maxRelativeFrequency)
cl2 := &CostasLoop4{controlLoop: *cl, baseCostasLoop: baseCostasLoop{error: 0, lastBuffLen: 0}}
return cl2
}
func MakeCostasLoop4(loopBandwidth float32) CostasLoop {
return MakeCostasLoop4WithFrequencyRange(loopBandwidth, -1, 1)
}
func (cl *CostasLoop4) Work(input []complex64) []complex64 {
output := make([]complex64, cl.PredictOutputSize(len(input)))
cl.WorkBuffer(input, output)
return output
}
func (cl *CostasLoop4) WorkBuffer(input, output []complex64) int {
if len(cl.frequencyShift) < len(input) {
cl.frequencyShift = make([]float32, len(input))
}
cl.lastBuffLen = len(input)
for i := 0; i < len(input); i++ {
n := tools.PhaseToComplex(-cl.phase)
output[i] = input[i] * n
vr := float32(1)
vi := float32(1)
if real(output[i]) <= 0 {
vr = -1
}
if imag(output[i]) <= 0 {
vi = -1
}
cl.error = imag(output[i])*vr - real(output[i])*vi
cl.error = tools.Clip(cl.error, 1)
cl.avgError += cl.error
cl.AdvanceLoop(cl.error)
cl.phaseWrap()
cl.frequencyLimit()
cl.frequencyShift[i] = cl.freq
}
cl.avgError /= float32(len(input) + 1)
return len(input)
}
// endregion
// region 8th Order Costas Loop
type CostasLoop8 struct {
controlLoop
baseCostasLoop
}
func MakeCostasLoop8WithFrequencyRange(loopBandwidth, minRelativeFrequency, maxRelativeFrequency float32) CostasLoop {
cl := makeControlLoop(loopBandwidth, minRelativeFrequency, maxRelativeFrequency)
cl2 := &CostasLoop8{controlLoop: *cl, baseCostasLoop: baseCostasLoop{error: 0, lastBuffLen: 0}}
return cl2
}
func MakeCostasLoop8(loopBandwidth float32) CostasLoop {
return MakeCostasLoop2WithFrequencyRange(loopBandwidth, -1, 1)
}
func (cl *CostasLoop8) GetError() float32 {
return cl.error
}
func (cl *CostasLoop8) Work(input []complex64) []complex64 {
output := make([]complex64, cl.PredictOutputSize(len(input)))
cl.WorkBuffer(input, output)
return output
}
func (cl *CostasLoop8) WorkBuffer(input, output []complex64) int {
if len(cl.frequencyShift) < len(input) {
cl.frequencyShift = make([]float32, len(input))
}
cl.lastBuffLen = len(input)
K := float32(math.Sqrt(2) - 1)
for i := 0; i < len(input); i++ {
n := tools.PhaseToComplex(-cl.phase)
output[i] = input[i] * n
vr := float32(1)
vi := float32(1)
if real(output[i]) < 0 {
vr = -1
}
if imag(output[i]) < 0 {
vi = -1
}
if tools.Abs(real(output[i])) > tools.Abs(imag(output[i])) {
cl.error = imag(output[i])*vr - real(output[i])*vi*K
} else {
cl.error = imag(output[i])*vr*K - real(output[i])*vi
}
cl.error = imag(output[i])*vr - real(output[i])*vi
cl.error = tools.Clip(cl.error, 1)
cl.avgError += cl.error
cl.AdvanceLoop(cl.error)
cl.phaseWrap()
cl.frequencyLimit()
cl.frequencyShift[i] = cl.freq
}
cl.avgError /= float32(len(input) + 1)
return len(input)
}
// endregion