fork download
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5.  
  6. # -----------------------------
  7. # Matplotlib キャッシュディレクトリ設定(警告回避)
  8. # -----------------------------
  9. # 書き込み可能なディレクトリを指定
  10. os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib_config' # Linux/macOS
  11. # Windowsの場合:
  12. # os.environ['MPLCONFIGDIR'] = r'C:\Temp\matplotlib_config'
  13.  
  14. # -----------------------------
  15. # Received power calculation (Free-space)
  16. # -----------------------------
  17. def received_power(distance_m, frequency_hz, tx_power_dbm=20):
  18. c = 3.0e8 # speed of light [m/s]
  19. wavelength = c / frequency_hz
  20. fspl_db = 20 * np.log10(4 * np.pi * distance_m / wavelength)
  21. rx_power_dbm = tx_power_dbm - fspl_db
  22. return rx_power_dbm
  23.  
  24. # -----------------------------
  25. # Settings
  26. # -----------------------------
  27. distances = np.linspace(1, 200, 200) # distance [m]
  28. tx_power_dbm = 20 # transmit power [dBm]
  29.  
  30. rx_24 = received_power(distances, 2.4e9, tx_power_dbm)
  31. rx_5 = received_power(distances, 5.0e9, tx_power_dbm)
  32.  
  33. # -----------------------------
  34. # Plot
  35. # -----------------------------
  36. plt.figure(figsize=(8,5))
  37. plt.plot(distances, rx_24, label="2.4 GHz", color="green", linewidth=2)
  38. plt.plot(distances, rx_5, label="5 GHz", color="blue", linestyle="--", linewidth=2)
  39.  
  40. plt.title("Received Power vs Distance (Free Space)")
  41. plt.xlabel("Distance [m]")
  42. plt.ylabel("Received Power [dBm]")
  43. plt.grid(True, linestyle="--", alpha=0.7)
  44. plt.legend()
  45. plt.tight_layout()
  46. plt.show()
  47.  
Success #stdin #stdout #stderr 4.82s 71308KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Fontconfig error: No writable cache directories