Skip to content

Instantly share code, notes, and snippets.

@wiless
Created November 27, 2014 19:16
Show Gist options
  • Save wiless/c266c1a6d97a0e718552 to your computer and use it in GitHub Desktop.
Save wiless/c266c1a6d97a0e718552 to your computer and use it in GitHub Desktop.
Sample code to calculate rssi in a sample deployment
package main
import (
"fmt"
"github.com/grd/statistics"
"log"
"math/cmplx"
"math/rand"
"time"
"wiless/cellular/antenna"
"wiless/cellular/deployment"
"wiless/cellular/pathloss"
"wiless/vlib"
)
var matlab *vlib.Matlab
// func startServer(obj *deployment.DropSystem) {
// rpc.Register(obj)
// rpcserver := rpc.NewServer()
// rpcserver.RegisterName("Box", obj)
// rpc.HandleHTTP()
// l, e := net.Listen("tcp", ":1234")
// if e != nil {
// log.Fatal("listen error:", e)
// }
// go http.Serve(l, nil)
// }
type LinkInfo struct {
RxID int
NodeTypes []string
MinPathLos vlib.VectorF
MinPathLosNode vlib.VectorI
}
func SingleCellDeploy(system *deployment.DropSystem) {
// var system deployment.DropSystem
setting := deployment.NewDropSetting()
temp := deployment.NewDropSetting()
temp.SetDefaults()
// jbdata, _ := json.Marshal(*temp)
CellRadius := 100.0
AreaRadius := CellRadius * 3.0
setting.SetCoverage(deployment.CircularCoverage(AreaRadius))
// bs :=
// bs := deployment.NodeType{Name: "BS", Hmin: 0.0, Hmax: 0.0}
WAPNodes := 10
NCluster := 5
ClusterSize := 10
setting.AddNodeType(deployment.NodeType{Name: "BS", Hmin: 20.0, Hmax: 20.0, Count: 1})
setting.AddNodeType(deployment.NodeType{Name: "UE", Hmin: 0.0, Hmax: 10.0, Count: 150})
setting.AddNodeType(deployment.NodeType{Name: "WAP", Hmin: 0.0, Hmax: 0.0, Count: WAPNodes})
setting.AddNodeType(deployment.NodeType{Name: "PICO", Hmin: 0.0, Hmax: 0.0, Count: NCluster * ClusterSize})
// fmt.Println("Setting : %v", setting)
vlib.SaveStructure(setting, "nodetype.txt", true)
// setting.Init()
// setting.SetNodeCount("BS", 2)
// setting.SetNodeCount("UE", 22)
// setting.Init()
system.SetSetting(setting)
system.Init()
system.SetTxNodeNames("BS", "WAP", "PICO")
setting.SetRxNodeNames("UE")
// system.DropNodeType("BS")
system.DropNodeType("UE")
var wlocation vlib.VectorC
///
wappos := deployment.RandPointR(AreaRadius)
wlocation = deployment.RectangularEqPoints(wappos, 50, rand.Float64()*360, WAPNodes)
wlocation = deployment.AnnularRingPoints(deployment.ORIGIN, 100, 200, WAPNodes)
wlocation = deployment.AnnularRingEqPoints(deployment.ORIGIN, 200, WAPNodes)
matlab.Export("wappos", wappos)
matlab.ExportStruct("wappos", vlib.Complex(wappos))
// matlab.Q(matlab.AddText(wappos, "WAP"))
// nids := system.GetNodeIDs("PICO")
// fmt.Printf("PICO nodes ids ", nids)
for i := 0; i < NCluster; i++ {
picopos := deployment.RandPointR(AreaRadius)
plocation := deployment.CircularPoints(picopos, 20, ClusterSize)
segment := vlib.NewSegmentI(i*ClusterSize, ClusterSize)
system.SetNodeLocationOf("PICO", segment, plocation)
// matlab.Q(matlab.AddText(picopos, "Cluster"))
}
// matlab.Q(matlab.AddText(deployment.ORIGIN, "BS"))
///
system.SetNodeLocation("BS", 0, complex(0, 0))
system.SetAllNodeLocation("WAP", wlocation)
matlab.Export("bs", system.Locations("BS"))
matlab.Export("ue", system.Locations("UE"))
matlab.Export("wap", system.Locations("WAP"))
matlab.Export("pico", system.Locations("PICO"))
// plotcmd := `hold off;
// plot(real(bs),imag(bs),'ro');
// hold all;
// plot(real(ue),imag(ue),'.');
// plot(real(wap),imag(wap),'m*');
// plot(real(pico),imag(pico),'ro');
// grid on;`
// matlab.Command(plotcmd)
// matlab.AddText(wappos, "WAPcentre")
// matlab.Flush()
/// MOVING BS on HEX co-ords
// {
// origincords := deployment.HexagonalPoints(complex(0, 0), CellRadius)
// // system.SetAllNodeLocation("BS", origincords)
// var otherCords vlib.VectorC
// for i := 0; i < len(origincords); i++ {
// var centre complex128
// if i == 5 {
// centre = (origincords[i] + origincords[0])
// } else {
// centre = (origincords[i] + origincords[i+1])
// }
// otherCords = append(otherCords, deployment.HexagonalPoints(centre, CellRadius)...)
// }
// // matlab.Export("pos", otherCords)
// }
}
func main() {
matlab = vlib.NewMatlab("output")
matlab.Silent = true
matlab.Json = true
rand.Seed(time.Now().Unix())
data := statistics.Float64{17.2, 18.1, 16.5, 18.3, 12.6}
mean := statistics.Mean(&data)
fmt.Printf("The sample mean is %g", mean)
var singlecell deployment.DropSystem
// modelsett:=pathloss.NewModelSettingi()
var model pathloss.PathLossModel
model.ModelSetting.SetDefault()
aas := antenna.NewAAS()
aas.SetDefault()
// antenna.RunAAS(*aas)
aas.N = 1
aas.Omni = false
aas.VTiltAngle = 10
aas.HTiltAngle = 30
aas.BeamTilt = 0
aas.DisableBeamTit = false
aas.SLAV = 30
aas.CreateElements(vlib.Location3D{0, 0, 10})
Hpattern := vlib.NewVectorF(360)
Vpattern := vlib.NewVectorF(360)
Epattern := vlib.NewVectorF(360)
angle := vlib.NewVectorF(360)
fmt.Println("================ starting pattern")
for i := 0; i < 360; i++ {
theta := float64(i)
// fmt.Println("Input to HGain = ", theta)
Hpattern[i] = aas.ElementDirectionHGain(theta)
Vpattern[i] = aas.ElementDirectionVGain(theta)
Epattern[i] = aas.ElementEffectiveGain(theta, theta)
angle[i] = vlib.ToRadian(theta)
// fmt.Printf("\n %f", aas.ElementDirectionGain(theta))
}
// wlocation = deployment.AnnularRingPoints(deployment.ORIGIN, 100, 200, WAPNodes)
wlocation := deployment.AnnularRingEqPoints(deployment.ORIGIN, 10, 360)
// fmt.Print("\nwlocation=", wlocation)
node3Dlocations := vlib.FromVectorC(wlocation, 9)
// gain := vlib.NewVectorF(len(node3Dlocations))
hgain := vlib.NewVectorF(len(node3Dlocations))
vgain := vlib.NewVectorF(len(node3Dlocations))
thH := vlib.NewVectorF(360)
thV := vlib.NewVectorF(360)
gain := vlib.NewVectorF(360)
// rad := 10.0
location := vlib.LoadLocationsFromFile("locations.csv", ",")
for i := 0.0; i < 360.0; i += 1 {
// xyloc := vlib.GetEJtheta(i) * complex(rad, 0)
str := fmt.Sprintf("Gain(%d,:)", int(i)+1)
for j := 0.0; j < 360.0; j += 1 {
ggain, tthH, tthV := aas.AASGain(location)
thH[int(i)] = tthH
thV[int(j)] = tthV
gain[int(j)] = ggain
}
matlab.Export(str, gain)
}
// for i := 0; i < len(node3Dlocations); i++ {
// gain[i], thH[i], thV[i] = aas.AASGain(node3Dlocations[i])
// // fmt.Printf("\nGain @ %f,%f : %f H=%f V=%f ", node3Dlocations[i].Cmplx(), node3Dlocations[i].Z, gain[i], thH[i], thV[i])
// // fmt.Printf("\n Location : %v", node3Dlocations[i].XZ())
// hgain[i] = aas.ElementDirectionHGain(thH[i])
// vgain[i] = aas.ElementDirectionVGain(thH[i])
// }
matlab.Export("HGain", hgain)
matlab.Export("VGain", vgain)
// matlab.Export("EGain", gain)
// matlab.Export("thH", thH)
// matlab.Export("thV", thV)
// gain, thH, thV := aas.AASGain(vlib.Location3D{10, 0, 10})
// fmt.Printf("\n Gain : %f %f %f ", gain, thH, thV)
// fmt.Printf("\n %#v", aas)
matlab.Export("hpattern", Hpattern)
matlab.Export("phaseRadian", (angle))
matlab.Export("epattern", Epattern)
matlab.Export("vpattern", Vpattern)
// matlab.Command("polar(angle,hpattern);hold all;grid on;")
// matlab.Command("polar(angle,vpattern)")
// matlab.Command("legend 'horizontal' 'vertical'")
matlab.Close()
return
SingleCellDeploy(&singlecell)
txNodeNames := singlecell.GetTxNodeNames()
rxNodeNames := singlecell.GetRxNodeNames()
log.Print(txNodeNames, rxNodeNames)
rxlocs := singlecell.Locations("UE")
RxLinkInfo := make([]LinkInfo, len(rxlocs))
var pathLossPerRxNode map[int]vlib.VectorF
pathLossPerRxNode = make(map[int]vlib.VectorF)
log.Print(pathLossPerRxNode)
for i := 0; i < rxlocs.Size(); i++ {
rxlocation := rxlocs[i]
var info LinkInfo
info.RxID = i
func(rxlocation complex128, txNodeNames []string) {
info.NodeTypes = make([]string, len(txNodeNames))
info.MinPathLos = vlib.NewVectorF(len(txNodeNames))
info.MinPathLosNode = vlib.NewVectorI(len(txNodeNames))
for indx, name := range txNodeNames {
txlocs := singlecell.Locations(name)
allpathlossPerTxType := vlib.NewVectorF((txlocs.Size()))
info.NodeTypes[indx] = name
for k := 0; k < txlocs.Size(); k++ {
srcLocation := txlocs[k]
distance := cmplx.Abs(rxlocation - srcLocation)
lossDb := model.LossInDb(distance)
// fmt.Printf("%d %v %v distance = %v", i, src, rxlocs[k], rxlocs[k]-src)
// matstr := fmt.Sprintf("Distance(%d,%d)", rxnodeId+1, k+1)
allpathlossPerTxType[k] = lossDb
// fmt.Printf("\n Distance %f : loss %f dB", distance, lossDb)
// matlab.Export(matstr, data)
}
data := statistics.Float64(allpathlossPerTxType)
info.MinPathLos[indx], info.MinPathLosNode[indx] = statistics.Min(&data)
// RxLinkInfo[i] = info
//log.Println(name, ":", allpathlossPerTxType)
}
}(rxlocation, txNodeNames)
RxLinkInfo[i] = info
// fmt.Printf("\n Info[%d] : %#v", i, info)
}
// enc := json.NewEncoder(os.Stdout)
// map[string]interface{}
// type struct data
// type IPFilePairs []*IPFilePair
// databyte, err := json.Marshal(RxLinkInfo[0])
// fmt.Printf("\nerr %s,%v", databyte, err)
// simulation.SaveStructure(RxLinkInfo, "GeometricInfo.dat", true)
// x, _ := json.Marshal(singlecell.Nodes[0])
// x, _ := sample.MarshalJSON()
// fmt.Printf("\n NODE = %#v", singlecell.Nodes[0])
// var node *deployment.Node = singlecell.Nodes[0]
matlab.ExportStruct("nodeTypes", txNodeNames)
matlab.ExportStruct("nodeinfo0", *singlecell.Nodes[0])
matlab.ExportStruct("nodeinfo1", *singlecell.Nodes[1])
// enc := json.NewEncoder(os.Stdout)
// enc.Encode(txNodeNames)
// fmt.Printf("\n NODE = %#v", singlecell.Nodes[1])
matlab.ExportStruct("Nodes", *singlecell.Nodes[0])
// matlab.ExportStruct("Keys", matlab.Keys)
// matlab.Export("Keys", matlab.Keys)
// fd, _ := os.Create("Allresults.dat")
// enc := json.NewEncoder(fd)
// var encerr error
// encerr = enc.Encode(vlib.GenType("Keys", matlab.Keys))
// if encerr != nil {
// log.Print("Encode 1 error ", encerr)
// }
// encerr = enc.Encode(vlib.GenType("LinkInfo", RxLinkInfo))
// if encerr != nil {
// log.Print("Encode 2 error ", encerr)
// }
// for _, nodetype := range txNodeNames {
// encerr = enc.Encode(vlib.GenType(nodetype, singlecell.Locations3D(nodetype)))
// }
// for _, nodetype := range rxNodeNames {
// encerr = enc.Encode(vlib.GenType(nodetype, singlecell.Locations3D(nodetype)))
// }
// defer fd.Close()
matlab.Flush()
matlab.Close()
fmt.Println("\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment