Skip to content

Instantly share code, notes, and snippets.

@korjaa
Last active September 9, 2022 11:10
Show Gist options
  • Save korjaa/2eb357a6a0bafd64a03711f4bfe6e004 to your computer and use it in GitHub Desktop.
Save korjaa/2eb357a6a0bafd64a03711f4bfe6e004 to your computer and use it in GitHub Desktop.
Average dBm
def average_dbm(dbms):
# https://en.wikipedia.org/wiki/Decibel
# https://en.wikipedia.org/wiki/LogSumExp
# Less numerically stable version:
# 10*numpy.log10(numpy.mean(numpy.power(10, (dbms-30)/10)))
bel_np = 0.5 * numpy.log(10) # Bel & Neper by definition
dbel_np = bel_np / 10
np_to_db = 1 / dbel_np
# B = 1/2 * ln(10)
# Lp = 1/2 * ln(P/P0) * Np
sum_dbm = 1/2 * np_to_db * scipy.special.logsumexp(dbms / (1/2 * np_to_db))
avg_dbm = sum_dbm - 10 * numpy.log10(len(dbms)) # linear division is log subtraction
return avg_db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment