diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a60b85 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +*.pyc diff --git a/README.mk b/README.mk new file mode 100644 index 0000000..0db86ed --- /dev/null +++ b/README.mk @@ -0,0 +1,4 @@ +[UTBM - LO53] - Python fingerprinting +Achille Dantz + +This program aims to compute the approximate location of a mobile terminal for a known set of Wi-Fi accesspoints in an ideal 12x12m space for which we have 9 fingerprinting values (known RSSI for each accesspoint at a given location) diff --git a/main.py b/main.py new file mode 100644 index 0000000..89c79ea --- /dev/null +++ b/main.py @@ -0,0 +1,46 @@ + +from structure import * +import sys + +Tf = [] #cells table +testSample = RSSVector(-26, -42, -13, -46) + + +#cells Table initialization +for i in range (0, 3): + Tf.append([]) + for k in range (0,3): + Tf[i].append([]) + +Tf[0][0] = Cell(RSSVector(-38,-27,-54,-13), Location(2,2)) +Tf[0][1] = Cell(RSSVector(-74,-62,-48,-33), Location(2,6)) +Tf[0][2] = Cell(RSSVector(-13,-28,-12,-40), Location(2,10)) +Tf[1][0] = Cell(RSSVector(-34,-27,-38,-41), Location(6,2)) +Tf[1][1] = Cell(RSSVector(-64,-48,-72,-35), Location(6,6)) +Tf[1][2] = Cell(RSSVector(-45,-37,-20,-15), Location(6,10)) +Tf[2][0] = Cell(RSSVector(-17,-50,-44,-33), Location(10,2)) +Tf[2][1] = Cell(RSSVector(-27,-28,-32,-45), Location(10,6)) +Tf[2][2] = Cell(RSSVector(-30,-20,-60,-40), Location(10,10)) + +def main(args): + + print(Tf[1][1].v.n2) + + + print("\nk neighbors of test sample : ") + neighborsCells = KNeighbors(Tf, testSample) + for k in neighborsCells: + print("(", k.location.x, ";", k.location.y, ")") + + print ("\ndistances : " + str(testSample.distances)) + + testLoc = Location(3,4,5) + loc2 = Location(1,2,3) + print((testLoc+loc2).toString()) + + a = resolve_barycenter(neighborsCells, testSample) + print(a.toString) + return 0; + + +main(sys.argv) \ No newline at end of file diff --git a/structure.py b/structure.py new file mode 100644 index 0000000..5c6aba1 --- /dev/null +++ b/structure.py @@ -0,0 +1,80 @@ +from operator import itemgetter + +class RSSVector(): + distances = [] + def __init__(self, n1, n2, n3, n4): + self.n1 = n1 + self.n2 = n2 + self.n3 = n3 + self.n4 = n4 + +class Location(): + def __init(self): + self.x = self.y = self.z = 0 + + def __init__(self, x, y, z=0): + self.x = x + self.y = y + self.z = z + + def __mul__(self, multiplier): + returnValue = Location(self.x, self.y, self.z) + returnValue.x *= multiplier + returnValue.y *= multiplier + returnValue.z *= multiplier + return returnValue + def __rmul__(self, multiplier): + return self * multiplier + + def __add__(self, added): + returnValue = Location(self.x, self.y, self.z) + returnValue.x += added.x + returnValue.y += added.y + returnValue.z += added.z + return returnValue + + def toString(self): + return "(" + str(self.x) + " ; " + str(self.y) + " ; " + str(self.z) + ")" + + +class Cell(): + def __init__(self, v_, loc): + self.v = v_ + self.location = loc + + +def KNeighbors(fingerprints, sample): + ''' + Returns the 4 closest cells to the given sample and fills sample distance data + :param Cell[3][3] fingerprints: 2D array of all the cells + :param RSSIVector sample: Mobile terminal sample + :return Cell[4]: the 4 nearest cells + ''' + distances = [] + for row in fingerprints: + for currentItem in row: + dist = abs(currentItem.v.n1 - sample.n1) \ + + abs(currentItem.v.n2 - sample.n2) \ + + abs(currentItem.v.n3 - sample.n3) \ + + abs(currentItem.v.n4 - sample.n4) + distances.append((dist, currentItem)) + distances = sorted(distances, key=itemgetter(0)) + neighbours = [] + for k in range (0,4): + neighbours.append(distances[k][1]) + sample.distances.append(distances[k][0]) + return neighbours + + + +def resolve_barycenter(neighbourCells, sample): + ''' + Returns the weighted barycenter of the 4 neighbouring cells + :param Cell[4] neighbourCells: Array containing the 4 closest cells + :param RSSIVector sample: Sample of the mobile terminal + :return Location: Estimated location of the mobile terminal + ''' + d = sample.distances #shorter notation + a1,a2,a3,a4 = 1 / (1+d[0]/d[1]+d[0]/d[2]+d[0]/d[3]), 1 / (1+d[1]/d[0]+d[1]/d[2]+d[1]/d[3]), 1 / (1+d[2]/d[1]+d[2]/d[0]+d[2]/d[3]), 1 / (1+d[3]/d[1]+d[3]/d[2]+d[3]/d[0]) + return a1*neighbourCells[0].location + a2*neighbourCells[1].location + a3*neighbourCells[1].location + a4*neighbourCells[1].location + \ No newline at end of file -- libgit2 0.21.4