Skip to content

Instantly share code, notes, and snippets.

@ifindev
Last active May 7, 2020 17:29
Show Gist options
  • Save ifindev/b52377e4bb325c4744f866c17da40116 to your computer and use it in GitHub Desktop.
Save ifindev/b52377e4bb325c4744f866c17da40116 to your computer and use it in GitHub Desktop.
Min-max algorithm implementation for indoor localization process. Should be rewrite for a cleaner code.
"""
Author : Muhammad Arifin
Date : 8th May, 2020
Institution : Universitas Gadjah Mada, Department of Nuclear Engineering and Engineering Physics
Description : Very rough min-max algorithm implementation for indoor localization problem.
RSSI data is obtained from https://github.com/pspachos/RSSI-Dataset
"""
import numpy as np
import matplotlib.pyplot as plt
from heapq import nlargest
from heapq import nsmallest
#%matplotlib qt #matplotlib plotting outside spyder console
f = open("D:/Skripsweetku/DataPaper/Environment1/WiFi/1D2.txt", "r")
df = []
for x in f:
df.append(x)
f.close()
# Count Each Data
nodeA = []
nodeB = []
nodeC = []
# Variables
# 1:node A, 2: node B, 3: node C
d = 1
xi = [0, d, d]
yi = [0, 0, d]
xreal = (0.5)*d
yreal = (0.5)*d
# RSSI Data
for i in range(len(df)):
if df[i][5:6] == 'A':
nodeA.append(int(df[i][8:11]))
elif df[i][5:6] == 'B':
nodeB.append(int(df[i][8:11]))
elif df[i][5:6] == 'C':
nodeC.append(int(df[i][8:11]))
minLen = [len(nodeA), len(nodeB), len(nodeC)] #Untuk mencari list yang paling pendek.
bench = min(minLen)
# Determine radius using Path Loss
n = 2.013
C = -49.99
r = lambda rssi: 10**((C-rssi)/(10*n))
r1 = [r(i) for i in nodeA]
r2 = [r(i) for i in nodeB]
r3 = [r(i) for i in nodeC]
# Calculating xmax, xmin, ymax, and ymin
# xmax = xcalc + d, xmin = xcalc - d, ymax = ycalc + d, ymin = ycalc - d
maxx = lambda n,r: n + r
minn = lambda n,r: n - r
x1mx = [maxx(xi[0],r1[i]) for i in range(bench)]
x2mx = [maxx(xi[1],r2[i]) for i in range(bench)]
x3mx = [maxx(xi[2],r3[i]) for i in range(bench)]
x1mn = [minn(xi[0],r1[i]) for i in range(bench)]
x2mn = [minn(xi[1],r2[i]) for i in range(bench)]
x3mn = [minn(xi[2],r3[i]) for i in range(bench)]
y1mx = [maxx(yi[0],r1[i]) for i in range(bench)]
y2mx = [maxx(yi[1],r2[i]) for i in range(bench)]
y3mx = [maxx(yi[2],r3[i]) for i in range(bench)]
y1mn = [minn(yi[0],r1[i]) for i in range(bench)]
y2mn = [minn(yi[1],r2[i]) for i in range(bench)]
y3mn = [minn(yi[2],r3[i]) for i in range(bench)]
# all minimum and maximum
xmax = []
xmax.extend(x1mx)
xmax.extend(x2mx)
xmax.extend(x3mx)
xmin = []
xmin.extend(x1mn)
xmin.extend(x2mn)
xmin.extend(x3mn)
ymax = []
ymax.extend(y1mx)
ymax.extend(y2mx)
ymax.extend(y3mx)
ymin = []
ymin.extend(y1mn)
ymin.extend(y2mn)
ymin.extend(y3mn)
# Get n items for maxmin and minmax (n = bench)
xmaxmin = np.array(nlargest(bench, xmin))
xminmax = np.array(nsmallest(bench, xmax))
ymaxmin = np.array(nlargest(bench, ymin))
yminmax = np.array(nsmallest(bench, ymax))
# Get calculated coordinate of x and y
xcoor = (xmaxmin50 + xminmax50)/2
ycoor = (ymaxmin50 + yminmax50)/2
# Plot
plt.scatter(xreal,yreal,label="Real")
plt.scatter(np.array(xmax), np.array(ymax), label="Calculated")
plt.title("MinMax WiFi 1D2")
plt.xlabel('$x$ (m)')
plt.ylabel('$y$ (m)')
plt.xticks(np.arange(0,1.1,0.1))
plt.yticks(np.arange(0,1.1,0.1))
plt.ylim([0,1.0])
plt.legend()
plt.grid(True)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment