Skip to content

Instantly share code, notes, and snippets.

@mstaflex
Created March 10, 2014 11:30
Show Gist options
  • Save mstaflex/9463394 to your computer and use it in GitHub Desktop.
Save mstaflex/9463394 to your computer and use it in GitHub Desktop.
Script to play around with efficient dBm calculations and correct logarithmic averages
from math import pow, log10
# ----------------------------------------------
rss = [rss for rss in range(-100, 21)]
rss_watt = [pow(10, s / 10.) for s in rss]
def get_rss_in_mwatt(rss_in_dbm):
assert 20 >= rss_in_dbm >= -100
return rss_watt[int(round(rss_in_dbm)) + 100]
def get_rss_in_dbm(rss_in_mwatt):
return 10 * log10(rss_in_mwatt)
# ----------------------------------------------
mwatt_sum = 0
n = 0
db_sum= 0
for i in range(0, 10):
db = -60
db_sum += db
mwatt_sum += get_rss_in_mwatt(db)
n += 1.
rss_outlier_dbm = -10
db_sum += rss_outlier_dbm
mwatt_sum += get_rss_in_mwatt(rss_outlier_dbm)
n += 1
avg_mwatt = mwatt_sum/n
print 'simple avg in db (wrong)', db_sum/n
print 'avg in linear mwatt scale (correct)',get_rss_in_dbm(avg_mwatt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment