-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrssi.py
More file actions
113 lines (75 loc) · 2.9 KB
/
rssi.py
File metadata and controls
113 lines (75 loc) · 2.9 KB
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
import numpy
import math
import random
import json
TRANSMITTED_POWER = -4
COUPLING_FACTOR = 1
WAVE_FACTOR = 40
PATH_LOSS_PERIM = -14
PATH_LOSS_INTERNAL = -3
NOISE = (-30, 0)
THRESHOLD = -95
PACKET_LOSS = 0.1
DELAY = 0.3 # seconds
SIM_WINDOW = 3
def load_settings(path):
"""
Loads the settings from a json file
"""
with open(path) as f:
settings = json.loads(f.read())
global TRANSMITTED_POWER, COUPLING_FACTOR, WAVE_FACTOR, \
PATH_LOSS_PERIM, PATH_LOSS_INTERNAL, NOISE, THRESHOLD, \
PACKET_LOSS, DELAY, SIM_WINDOW
TRANSMITTED_POWER = settings["TRASMITTED_POWER"]
COUPLING_FACTOR = settings["COUPLING_FACTOR"]
WAVE_FACTOR = settings["WAVE_FACTOR"]
PATH_LOSS_PERIM = settings["PATH_LOSS_PERIM"]
PATH_LOSS_INTERNAL = settings["PATH_LOSS_INTERNAL"]
NOISE = settings["NOISE"]
THRESHOLD = settings["THRESHOLD"]
PACKET_LOSS = settings["PACKET_LOSS"]
DELAY = settings["DELAY"]
SIM_WINDOW = settings["SIM_WINDOW"]
def get_distance_from_RSSI(received_RSSI):
"""
Gets distance given RSSI value
"""
return 10 ** ((TRANSMITTED_POWER - received_RSSI - WAVE_FACTOR + COUPLING_FACTOR) / 20)
def get_ideal_RSSI(distance, walls=[0, 0]):
"""
Gets ideal RSSI given distance and wall path loss
"""
if distance == 0:
return TRANSMITTED_POWER
return TRANSMITTED_POWER - WAVE_FACTOR + COUPLING_FACTOR - 20 * math.log10(distance) + walls[1]
def get_error(received, saved_RSSI):
"""
Generates error given a list of recieved (real) RSSI values and saved ideal RSSI values
(Works only if both vectors are with the same magnitude)
"""
return numpy.square(numpy.subtract(received, saved_RSSI)).sum()
def get_error2(received, saved_RSSI):
"""
Returns absolute error between a received vector of (real) RSSI values and a vector of ideal RSSI values
Vectors can have different magnitudes, and only received antenna values are confronted
"""
return sum([pow(r[1] - saved_RSSI[r[0]], 2) for r in received])
def generate_noise():
"""
Generates a random noise value between given noise constraints
"""
LAMBDA = abs(NOISE[0] - NOISE[1]) / 2
value = 1 / (LAMBDA * math.log(random.random()))
return numpy.clip(value, NOISE[0], NOISE[1])
def is_packet_lost():
"""
Generates a random float between 0 and 1 and confronts it with PACKET_LOSS value
Determines if a packet should be considered lost by accident
"""
return random.random() <= PACKET_LOSS
if __name__ == "__main__":
load_settings("settings.json")
print(TRANSMITTED_POWER, COUPLING_FACTOR, WAVE_FACTOR, PATH_LOSS_PERIM, PATH_LOSS_INTERNAL, NOISE, THRESHOLD,
PACKET_LOSS, DELAY, SIM_WINDOW)
print(get_ideal_RSSI(100))