func (s *setup) SimulateLink(SNR float64, pdp vlib.VectorF, spcode vlib.VectorC, uid int) float64 { var cdma core.CDMA bitTs := 1.0 cdma.InitializeChip() cdma.SetSpreadCode(spcode, true) var mpchannel core.MPChannel mpchannel.InitializeChip() /// param := core.NewIIDChannel() param.SetPDP(pdp) param.Mode = "iid" symbolTs := 2 * bitTs param.Ts = 5 * symbolTs // bitTs // dataArray.Ts * float64(BlockSize) mpchannel.InitParam(param) var modem core.Modem modem.InitializeChip() modem.InitModem(2) feedback := make(gocomm.Complex128AChannel, 10) mpchannel.SetFeedbackChannel(feedback) modem.SetFeedbackChannel(feedback) bitsample := vlib.VectorB(sources.RandB(s.BlockSize)) bitChannel := gocomm.NewBitChannel() var dataArray gocomm.SBitObj dataArray.MaxExpected = bitsample.Size() dataArray.Message = fmt.Sprintf("Src %f ", SNR) dataArray.Ts = bitTs dataArray.MaxExpected = s.BlockSize // fmt.Printf("bits=%v", bitsample) go (func(bitChannel gocomm.BitChannel) { for i := 0; i < s.BlockSize; i++ { dataArray.Ch = bitsample[i] // fmt.Printf("\n Transmitting .. %v", dataArray) bitChannel <- dataArray // modem.ModulateFn(dataArray) // symCH <- dataArray dataArray.TimeStamp += dataArray.Ts } })(bitChannel) go modem.Modulate(bitChannel) // fmt.Printf("Generated Bits", samples) symCH := chipset.ToComplexCH(modem.PinByName("outputPin0")) go cdma.Spread(symCH) OutCH := chipset.ToComplexACH(cdma.PinByID(2)) ch1 := gocomm.NewComplex128Channel() go gocomm.ComplexA2Complex(OutCH, ch1) /// Add Multipath channel go mpchannel.Channel(ch1) ch2 := chipset.ToComplexCH(mpchannel.PinByName("outputPin0")) /// Add Noise noiseDb := 1.0 / dsp.InvDb(SNR) // fmt.Printf("\n %f AWGN Noise : %f", SNR, noiseDb) var awgn channel.ChannelEmulator awgn.SetNoise(0, noiseDb) awgn.InitializeChip() go awgn.AWGNChannel(ch2) ch3 := chipset.ToComplexCH(awgn.PinByID(1)) // go awgn. (1/noiseDb, samples) go cdma.DeSpread(ch3) ch4 := chipset.ToComplexCH(cdma.PinByID(3)) //go sink.CROremote(cdma.PinByID(3)) go modem.DeModulate(ch4) ch5 := chipset.ToComplexCH(modem.PinByID(3)) // .Channel.(gocomm.Complex128Channel) // gocomm.WGroup.Add(1) // gocomm.WGroup.Add(1) // go gocomm.SinkComplex(ch4, "") // /// // bitsample var BER core.BER BER.TrueBits = bitsample BER.InitializeChip() ch6 := gocomm.NewBitChannel() BER.Reset() go BER.BERCount(ch6) go (func(ch6 gocomm.BitChannel) { count := 1 for i := 0; i < count; i++ { data := <-ch5 count = data.MaxExpected //fmt.Printf("\n sym received %#v", data) rxbits := gocomm.Complex2Bits(data) // fmt.Printf("\n bits converted received %#v", rxbits) ch6 <- rxbits[0] ch6 <- rxbits[1] // dec := gocomm.Complex2Bits(data) // result[2*i] = dec[0] // result[2*i+1] = dec[1] // fmt.Printf("\n %v", gocomm.Complex2Bits(data)) } })(ch6) ch7 := chipset.ToFloatCH(BER.PinByName("outputPin0")) count := 1 // result := vlib.NewVectorB(BlockSize) var result float64 = 0 for i := 0; i < count; i++ { err := <-ch7 count = err.MaxExpected // fmt.Printf("\nerr=(%f,%v) %#v", err.TimeStamp, err.Ch, err) result = err.Ch } // err := float64(bitsample.CountErrors(result)) / float64(BlockSize) // fmt.Printf("\ntxbits=%v", bitsample) // fmt.Printf("\nrxbits=%v", result) // fmt.Printf("\nErr=%v %v", bitsample.CountErrors(result), err) // gocomm.WGroup.Wait() if uid == 0 { s.snr_ber[SNR] += result } else { s.snr_ber1[SNR] += result } // snr_ber[SNR] /= s.snr_block[SNR] s.updateTable() // fmt.Printf("\r BLOCK %v \n BER : %v", s.snr_block, snr_ber) return result }
func main() { rand.Seed(time.Now().Unix()) // rand.Seed(1) start := time.Now() var N = 10 var N0 float64 = 1.0 / 10 txbits := vlib.VectorB(sources.RandB(N)) qpskModem := new(core.Modem) var BitsPerSymbol = 2 qpskModem.Init(BitsPerSymbol, "") fmt.Printf("\n%v", qpskModem) /// Take every N-bit and push to modem-channel-demod length := len(txbits) // slength := length / BitsPerSymbol //var result = make([]complex128, slength) /// Actual Modulation happens here cnt := 0 // rxcnt := 0 // symCH := make([]chan complex128, length) COUNT := length / 2 rxCH := make([]chan []uint8, COUNT) for i := 0; i < COUNT; i++ { rxCH[i] = make(chan []uint8) } for i := 0; i < length; i += BitsPerSymbol { symCH := make(chan complex128) go qpskModem.GenerateSymbolCH(cnt, txbits[i:i+BitsPerSymbol], symCH) go func(twowayChannel chan complex128, cnt int) { symbol := <-twowayChannel noise := sources.RandNC(N0) fmt.Printf("\n Added Noise %d %v", cnt, noise) rxsymbol := symbol + noise twowayChannel <- rxsymbol }(symCH, cnt) go qpskModem.DemodSymbolCH(cnt, symCH, rxCH[cnt]) cnt++ } ORDER := true //// UNORDERED receiver bits... for i := 0; i < COUNT; i++ { if ORDER { func(cnt int, tmpRxCh chan []uint8) { for rxbits := range tmpRxCh { fmt.Printf("\n Unordered Loop Detected bits %d %v", cnt, rxbits) } }(i, rxCH[i]) } else { go func(cnt int, tmpRxCh chan []uint8) { for rxbits := range tmpRxCh { fmt.Printf("\n Unordered Loop Detected bits %d %v", cnt, rxbits) } }(i, rxCH[i]) fmt.Printf("Waiting for routines to finish") time.Sleep(2 * time.Second) } } /// Ordered read from the Receiver channels // cnt = 0 // for _, channel := range rxCH { // for rxbits := range channel { // fmt.Printf("\n Outer Loop Detected bits %d %v", cnt, rxbits) // } // cnt++ // } // txsymbols := qpskModem.ModulateBits(txbits) // rxsymbols := vlib.ElemAddCmplx(txsymbols, sources.Noise(len(txsymbols), N0)) // rxbits := qpskModem.DeModulateBits(rxsymbols) // // fmt.Printf("\nTx=%v", txbits) // // fmt.Printf("\nRx=%v", rxbits) // fmt.Printf("\nERR=%v/%d = %f", txbits.CountErrors(rxbits), len(txbits), float64(txbits.CountErrors(rxbits))/float64(len(txbits))) fmt.Printf("\n\n Elapsed %v \n", time.Since(start).String()) }