## Convert between Power and dBm

Many devices that send & receive data via antennas give you information about their power in units of dBm. If you wan to know how to convert this unit to a power value given in mW (and vice versa) you may read on.

The formulae found in the Wikipedia article on dBm expressed in Python:

from math import log10

def mw_to_dbm(mW):
"""This function converts a power given in mW to a power given in dBm."""
return 10.*log10(mW)

def dbm_to_mw(dBm):
"""This function converts a power given in dBm to a power given in mW."""
return 10**((dBm)/10.)


Tests of the functions:

print "%.2f mW are equivalent to %.2f dBm" % (10, mw_to_dbm(10))
print "%.2f mW are equivalent to %.2f dBm" % (30, mw_to_dbm(30))
print "%.2f mW are equivalent to %.2f dBm" % (250, mw_to_dbm(250))
print "%.2f mW are equivalent to %.2f dBm" % (200, mw_to_dbm(200))
print "%.2f mW are equivalent to %.2f dBm" % (1.3, mw_to_dbm(1.3))

print "%.2f dBm are equivalent to %.2f mW" % (12, dbm_to_mw(12))
print "%.2f dBm are equivalent to %.2f mW" % (mw_to_dbm(10), dbm_to_mw(mw_to_dbm(10)))
print "%.2f dBm are equivalent to %.2f mW" % (mw_to_dbm(0.01), dbm_to_mw(mw_to_dbm(0.01)))


Output:

10.00 mW are equivalent to 10.00 dBm
30.00 mW are equivalent to 14.77 dBm
250.00 mW are equivalent to 23.98 dBm
200.00 mW are equivalent to 23.01 dBm
1.30 mW are equivalent to 1.14 dBm
12.00 dBm are equivalent to 15.85 mW
10.00 dBm are equivalent to 10.00 mW
-20.00 dBm are equivalent to 0.01 mW