-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgocomm.go
executable file
·420 lines (346 loc) · 9.28 KB
/
gocomm.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Package gocomm provides different channel objects and functionalites related to them
package gocomm
import (
"fmt"
"reflect"
"sync"
"github.com/wiless/vlib"
)
// Possibly to be deprecated
var WGroup sync.WaitGroup
type ChannelDataStruct interface {
GetMaxExpected() int
}
type SBitObj struct {
Ch uint8 // The uint8 data is here
MaxExpected int
Message string
Ts float64
TimeStamp float64
}
type SBitAObj struct {
Ch []uint8
MaxExpected int
Message string
Ts float64
TimeStamp float64
}
type SFloatObj struct {
Ch float64
MaxExpected int
Message string
Ts float64
TimeStamp float64
}
type SFloatAObj struct {
Ch vlib.VectorF
MaxExpected int
Message string
Ts float64
TimeStamp float64
}
type SComplex128Obj struct {
Ch complex128
MaxExpected int
Message string
Ts float64
TimeStamp float64
}
type SComplex128AObj struct {
Ch vlib.VectorC
MaxExpected int
Message string
Ts float64
TimeStamp float64
}
func (s *SBitObj) Next(sample uint8) {
s.Ch = sample
s.UpdateTimeStamp()
}
// Next function sets the sample given in argument and updates the Timestamp
func (s *SBitAObj) Next(sample []uint8) {
s.Ch = sample
s.UpdateTimeStamp()
}
func (s *SComplex128Obj) Next(sample complex128) {
s.Ch = sample
s.UpdateTimeStamp()
}
func (s *SComplex128AObj) Next(sample []complex128) {
s.Ch = sample
s.UpdateTimeStamp()
}
func (s *SBitObj) UpdateTimeStamp() {
s.TimeStamp += s.Ts
}
func (s *SBitAObj) UpdateTimeStamp() {
s.TimeStamp += s.Ts
}
func (s *SComplex128Obj) UpdateTimeStamp() {
s.TimeStamp += s.Ts
}
func (s *SComplex128AObj) UpdateTimeStamp() {
s.TimeStamp += s.Ts
}
func (s SBitObj) GetMaxExpected() int {
return s.MaxExpected
}
func (s SBitAObj) GetMaxExpected() int {
return s.MaxExpected
}
func (s SComplex128Obj) GetMaxExpected() int {
return s.MaxExpected
}
func (s SComplex128AObj) GetMaxExpected() int {
return s.MaxExpected
}
func (s SFloatAObj) GetMaxExpected() int {
return s.MaxExpected
}
func (s SFloatObj) GetMaxExpected() int {
return s.MaxExpected
}
type BitChannel chan SBitObj
type BitAChannel chan SBitAObj
type FloatChannel chan SFloatObj
type FloatAChannel chan SFloatAObj
type Complex128Channel chan SComplex128Obj
type Complex128AChannel chan SComplex128AObj
func NewBitChannel() BitChannel {
return make(BitChannel, 1)
}
func NewBitAChannel() BitAChannel {
return make(BitAChannel, 1)
}
func NewFloatChannel() FloatChannel {
return make(FloatChannel, 1)
}
func NewFloatAChannel() FloatAChannel {
return make(FloatAChannel, 1)
}
func NewComplex128Channel() Complex128Channel {
return make(Complex128Channel, 1)
}
func NewComplex128AChannel() Complex128AChannel {
return make(Complex128AChannel, 1)
}
func ChannelDuplexer(InCH Complex128Channel, OutCHA []Complex128Channel) {
Nchanels := len(OutCHA)
var chdataIn SComplex128Obj
var chdataOut SComplex128Obj
NextSize := 1
for cnt := 0; cnt < NextSize; cnt++ {
chdataIn = <-InCH
data := chdataIn.Ch
NextSize = chdataIn.MaxExpected
// fmt.Printf("%d InputDuplexer : %v ", cnt, data)
for i := 0; i < Nchanels; i++ {
chdataOut.Ch = data
chdataOut.MaxExpected = NextSize
chdataOut.Message = chdataIn.Message
OutCHA[i] <- chdataOut
}
}
close(InCH)
}
func Bit2BitA(InCH BitChannel, OutCH BitAChannel, veclen int) {
NextSize := 1
var Outobjs SBitAObj
var InObj SBitObj
for j := 0; j < NextSize; j++ {
vecdata := make([]uint8, veclen)
for i := 0; i < veclen; i++ {
InObj = <-InCH
vecdata[i] = InObj.Ch
NextSize = InObj.GetMaxExpected()
if i == 0 {
Outobjs.TimeStamp = InObj.TimeStamp
Outobjs.Ts = InObj.Ts * float64(veclen)
maxexp := int(float64(InObj.MaxExpected) / float64(veclen))
Outobjs.MaxExpected = maxexp
Outobjs.Message = InObj.Message + " S2P"
}
}
Outobjs.Ch = vecdata
// fmt.Printf("\n Wrote Vector %d of %d", j, NextSize)
OutCH <- Outobjs
}
}
/// Converts each Vector Sample to a Sample which can be processed at sample rate
/// This can be considered as Upsampler Each vector at rate Ts , is communicated to the next block at Ts/N samples
func Vector2Sample(uid int, NextSize int, InCH Complex128AChannel, OutCH Complex128Channel) {
var chdataOut SComplex128Obj
var chdataIn SComplex128AObj
cnt := 0
for i := 0; i < NextSize; i++ {
chdataIn = <-InCH
indata := chdataIn.Ch
veclen := len(indata)
cnt += veclen
for indx := 0; indx < veclen; indx++ {
chdataOut.Ch = indata[indx]
OutCH <- chdataOut
}
}
fmt.Printf("\n User%d : Closing", uid)
// close(InCH)
}
func Complex2ComplexA(InCH Complex128Channel, OutCH Complex128AChannel, veclen int) {
NextSize := 1
var Outobjs SComplex128AObj
var InObj SComplex128Obj
for j := 0; j < NextSize; j++ {
vecdata := make([]complex128, veclen)
for i := 0; i < veclen; i++ {
InObj = <-InCH
vecdata[i] = InObj.Ch
NextSize = InObj.GetMaxExpected()
if i == 0 {
Outobjs.TimeStamp = InObj.TimeStamp
Outobjs.Ts = InObj.Ts * float64(veclen)
maxexp := int(float64(InObj.MaxExpected) / float64(veclen))
Outobjs.MaxExpected = maxexp
Outobjs.Message = InObj.Message + " S2P"
}
}
Outobjs.Ch = vecdata
// fmt.Printf("\n Wrote Vector %d of %d", j, NextSize)
OutCH <- Outobjs
}
}
func ComplexA2Complex(InCH Complex128AChannel, OutCH Complex128Channel) {
var chdataOut SComplex128Obj
var chdataIn SComplex128AObj
NextSize := 1 // NoOfSuch vectors expected
for i := 0; i < NextSize; i++ {
chdataIn = <-InCH
NextSize = chdataIn.MaxExpected
indata := chdataIn.Ch
veclen := len(indata)
chdataOut.Message = chdataIn.Message + " P2S"
chdataOut.MaxExpected = veclen * NextSize
chdataOut.TimeStamp = chdataIn.TimeStamp
chdataOut.Ts = chdataIn.Ts / float64(veclen)
// fmt.Printf("\n Received %d of %d block,blocksize = %d, Output Expected %d", i+1, NextSize, veclen, chdataOut.MaxExpected)
// fmt.Printf("\n Data TimeStamp : %v", chdataIn.TimeStamp)
for indx := 0; indx < veclen; indx++ {
chdataOut.Ch = indata[indx]
OutCH <- chdataOut
chdataOut.TimeStamp += chdataOut.Ts
}
}
// close(InCH)
}
func (s SComplex128Obj) String() string {
result := fmt.Sprintf("(%f,%f)\t%% [#%d]@%f : \"%s\"", s.TimeStamp, s.Ch, s.MaxExpected, s.Ts, s.Message)
return result
}
func (s SComplex128AObj) String() (result string) {
result = fmt.Sprintf("(%f,%f)\t%% [#%d]@%f : \"%s\"", s.TimeStamp, s.Ch, s.MaxExpected, s.Ts, s.Message)
return result
}
func (s SBitObj) String() string {
result := fmt.Sprintf("(%f,%d)\t%% [#%d]@%f : \"%s\"", s.TimeStamp, s.Ch, s.MaxExpected, s.Ts, s.Message)
return result
}
func (s SBitAObj) String() (result string) {
result = fmt.Sprintf("(%f,%d)\t%% [#%d]@%f : \"%s\"", s.TimeStamp, s.Ch, s.MaxExpected, s.Ts, s.Message)
return result
}
func ComplexA2ComplexFn(InObj SComplex128AObj) (Outobjs []SComplex128Obj) {
veclen := len(InObj.Ch)
Outobjs = make([]SComplex128Obj, veclen)
maxexp := InObj.MaxExpected * veclen
Ts := InObj.Ts / float64(veclen)
for i := 0; i < veclen; i++ {
Outobjs[i].Ch = InObj.Ch[i]
Outobjs[i].Message = InObj.Message
Outobjs[i].MaxExpected = maxexp
Outobjs[i].Ts = Ts
Outobjs[i].TimeStamp = InObj.TimeStamp + float64(i)*Ts
}
return Outobjs
}
func Complex2ComplexAFn(InObj []SComplex128Obj) (Outobjs SComplex128AObj) {
veclen := len(InObj)
samples := vlib.NewVectorC(veclen)
for i := 0; i < veclen; i++ {
samples[i] = InObj[i].Ch
}
maxexp := int(float64(InObj[0].MaxExpected) / float64(veclen))
Outobjs.Ch = samples
Outobjs.Message = InObj[0].Message
Outobjs.MaxExpected = maxexp
Outobjs.Ts = InObj[0].Ts * float64(veclen)
Outobjs.TimeStamp = InObj[0].TimeStamp
return Outobjs
}
func SinkComplex(ch Complex128Channel, mode string) {
cnt := 1
if mode != "" {
fmt.Printf("\n%s=[", mode)
for i := 0; i < cnt; i++ {
result := <-ch
cnt = result.MaxExpected
fmt.Printf("(%v,%v);", result.TimeStamp, result.Ch)
}
fmt.Printf("]")
} else {
for i := 0; i < cnt; i++ {
result := <-ch
cnt = result.MaxExpected
fmt.Printf("\n Sink %d (of %d): %v", i+1, cnt, result)
}
}
WGroup.Done()
}
func SinkComplexA(ch Complex128AChannel) {
cnt := 1
for i := 0; i < cnt; i++ {
result := <-ch
cnt = result.MaxExpected
fmt.Printf("\n Sink %d (of %d): %v", i+1, cnt, result)
}
WGroup.Done()
}
func ComplexA2Bits(obj []SComplex128Obj) vlib.VectorB {
count := len(obj)
result := vlib.NewVectorB(count * 2)
for i := 0; i < count; i++ {
result[2*i] = uint8(real(obj[i].Ch))
result[2*i+1] = uint8(imag(obj[i].Ch))
}
return result
}
func Complex2Bits(obj SComplex128Obj) (outobj []SBitObj) {
result := make([]SBitObj, 2)
for i := 0; i < 2; i++ {
result[i].MaxExpected = obj.MaxExpected * 2
result[i].Ts = obj.Ts / 2.0
result[i].TimeStamp = obj.TimeStamp + float64(i)*result[i].Ts
result[i].Message = obj.Message
}
result[0].Ch = uint8(real(obj.Ch))
result[1].Ch = uint8(imag(obj.Ch))
// fmt.Printf("\n bit encoded received %#v", result)
// }
return result
}
type generic interface{}
func ToInt(value generic) (result int) {
switch reflect.TypeOf(value).String() {
case "float64":
return int(value.(float64))
case "int":
return value.(int)
default:
return 0
}
fmt.Print("Type is ", reflect.TypeOf(value))
sol := int(value.(float64))
defer func() {
result = 0
}()
fmt.Print("Solution is ", sol)
return result
}