Function bodies 11 total
calculate_max_path_loss function · python · L46-L48 (3 LOC)app.py
def calculate_max_path_loss(p_tx, g_tx, l_tx, g_rx, l_rx, l_fade, l_misc, p_rx_sensitivity):
"""Calculates the maximum allowable path loss in the link budget."""
return p_tx + g_tx + g_rx - l_tx - l_rx - l_fade - l_misc - p_rx_sensitivitycalculate_distance_from_pl function · python · L50-L68 (19 LOC)app.py
def calculate_distance_from_pl(max_path_loss, freq_mhz, n, model_choice):
"""Calculates distance based on path loss, frequency, exponent, and model choice."""
if max_path_loss < 0:
return None, None
try:
if model_choice == "1m":
# Using 1-meter reference model: PL(d) = (20log10(f) - 27.55) + 10n*log10(d_m)
log_term = (max_path_loss - 20 * math.log10(freq_mhz) + 27.55) / (10 * n)
distance_m = 10 ** log_term
distance_km = distance_m / 1000
else: # "1km"
# Using 1-km reference model: PL(d) = 20log10(f) + 10n*log10(d_km) + 32.44
log_term = (max_path_loss - 20 * math.log10(freq_mhz) - 32.44) / (10 * n)
distance_km = 10 ** log_term
return distance_km
except (ValueError, ZeroDivisionError):
return Nonecreate_plot function · python · L70-L100 (31 LOC)app.py
def create_plot(max_path_loss, freq_mhz, current_n, current_dist_ft, model_choice, x_scale='linear', y_scale='linear'):
"""Creates and returns a matplotlib figure of distance vs. FSPL exponent."""
fig, ax = plt.subplots(figsize=(6, 4))
n_values = np.linspace(2.0, 10.0, 100)
distances_ft = []
for n_val in n_values:
dist_km = calculate_distance_from_pl(max_path_loss, freq_mhz, n_val, model_choice)
if dist_km is not None and dist_km > 0:
distances_ft.append(dist_km * 1000 * 3.28084)
else:
# Use MIN_POSITIVE_VALUE for log scale compatibility
distances_ft.append(MIN_POSITIVE_VALUE)
ax.plot(n_values, distances_ft, label="Max Distance vs. FSPL Exponent")
if current_dist_ft is not None:
ax.plot(current_n, current_dist_ft, 'ro', label=f'Current: {current_dist_ft:,.0f} ft')
ax.set_xlabel("FSPL Path Loss Exponent (n)")
ax.set_ylabel("Maximum Distance (ft)")
ax.set_title("Impact of Path LinkBudgetCalculator class · python · L10-L292 (283 LOC)link_budget_calculator.py
class LinkBudgetCalculator(tk.Tk):
"""
An interactive GUI application for calculating the maximum communication
distance based on link budget parameters.
The user provides transmitter power, gains, losses, frequency,
receiver sensitivity, and a path loss exponent. The application
calculates the maximum achievable distance.
"""
def __init__(self):
super().__init__()
self.title("Link Budget Distance Calculator")
self.geometry("1050x700")
self.resizable(True, True)
# Style configuration
style = ttk.Style(self)
style.configure("TLabel", padding=5, font=('Helvetica', 10))
style.configure("TEntry", padding=5, font=('Helvetica', 10))
style.configure("TButton", padding=5, font=('Helvetica', 10, 'bold'))
style.configure("Header.TLabel", font=('Helvetica', 12, 'bold'))
style.configure("Result.TLabel", font=('Helvetica', 11, 'bold'), foreground='blue')
style.configure(__init__ method · python · L19-L46 (28 LOC)link_budget_calculator.py
def __init__(self):
super().__init__()
self.title("Link Budget Distance Calculator")
self.geometry("1050x700")
self.resizable(True, True)
# Style configuration
style = ttk.Style(self)
style.configure("TLabel", padding=5, font=('Helvetica', 10))
style.configure("TEntry", padding=5, font=('Helvetica', 10))
style.configure("TButton", padding=5, font=('Helvetica', 10, 'bold'))
style.configure("Header.TLabel", font=('Helvetica', 12, 'bold'))
style.configure("Result.TLabel", font=('Helvetica', 11, 'bold'), foreground='blue')
style.configure("Small.TButton", padding=(1, 1), font=('Helvetica', 8))
self.input_vars = {}
self.result_km_var = tk.StringVar(value="---")
self.result_m_var = tk.StringVar(value="---")
self.result_mi_var = tk.StringVar(value="---")
self.result_ft_var = tk.StringVar(value="---")
self.fspl_exponent_var = tk.DoubleVar(value=2create_widgets method · python · L48-L161 (114 LOC)link_budget_calculator.py
def create_widgets(self):
"""Creates and arranges all the GUI widgets in the main window."""
# Main layout frames
left_frame = ttk.Frame(self, padding="10")
left_frame.pack(side=tk.LEFT, fill=tk.Y, padx=(10,0), pady=10)
right_frame = ttk.Frame(self, padding="10")
right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=10, pady=10)
# --- Input Fields ---
# Use a separate frame for inputs for better organization
input_frame = ttk.LabelFrame(left_frame, text="Parameters", padding="10")
input_frame.pack(fill=tk.X)
# Inputs with +/- steppers
stepper_inputs = {
# Label: (Default Value, Increment Step)
"Transmit Power (dBm)": ("20", 0.2),
"Transmit Antenna Gain (dBi)": ("2", 0.2),
"Transmit Cable Loss (dB)": ("1", 0.2),
"Frequency (MHz)": ("2400", 10.0),
"Receiver Antenna Gain (dBi)": ("2", 0.2),
"Rec_increment_value method · python · L163-L176 (14 LOC)link_budget_calculator.py
def _increment_value(self, var: tk.StringVar, amount: float):
"""Helper function to increment/decrement a value in an Entry."""
try:
current_value = float(var.get())
new_value = current_value + amount
# Format as integer if the step is a whole number, otherwise as float
if amount % 1 == 0:
var.set(f"{new_value:.0f}")
else:
var.set(f"{new_value:.1f}")
self.calculate_distance()
except ValueError:
# Handle cases where the entry is empty or not a number
passSame scanner, your repo: https://repobility.com — Repobility
_on_slider_change method · python · L178-L185 (8 LOC)link_budget_calculator.py
def _on_slider_change(self, value):
"""Rounds slider value to 0.1, updates label, and recalculates."""
# Round the raw slider value to one decimal place to create 0.1 increments
rounded_value = round(float(value), 1)
self.fspl_exponent_var.set(rounded_value)
self.fspl_value_label.config(text=f"{rounded_value:.2f}")
self.calculate_distance()_on_input_change method · python · L187-L189 (3 LOC)link_budget_calculator.py
def _on_input_change(self, event=None):
"""Callback for when an entry value is changed by typing."""
self.calculate_distance()update_plot method · python · L191-L220 (30 LOC)link_budget_calculator.py
def update_plot(self, max_path_loss, freq_mhz, current_n, current_dist_ft, model_choice):
"""Updates the matplotlib plot with new data."""
self.ax.clear()
n_values = np.linspace(2, 10, 100)
if model_choice == "1m":
# 1-meter reference model
# d_m = 10 ^ ((PL - 20log10(f) + 27.55) / (10n))
log_term_numerator = max_path_loss - 20 * math.log10(freq_mhz) + 27.55
distances_m = 10 ** (log_term_numerator / (10 * n_values))
distances_km = distances_m / 1000
else:
# 1-km reference model
# d_km = 10^((PL - 20log10(f) - 32.44) / (10n))
log_term_numerator = max_path_loss - 20 * math.log10(freq_mhz) - 32.44
distances_km = 10 ** (log_term_numerator / (10 * n_values))
# Convert to feet for plotting
distances_ft = distances_km * 1000 * 3.28084
self.ax.plot(n_values, distances_ft, label="Max Distance vs. FSPL Exponent")
calculate_distance method · python · L222-L292 (71 LOC)link_budget_calculator.py
def calculate_distance(self):
"""
Performs the link budget calculation and updates the result display.
It reads all input values, validates them, computes the distance,
and displays it in the GUI.
"""
try:
# --- Gather and validate inputs ---
p_tx = float(self.input_vars["Transmit Power (dBm)"].get())
g_tx = float(self.input_vars["Transmit Antenna Gain (dBi)"].get())
l_tx = float(self.input_vars["Transmit Cable Loss (dB)"].get())
freq_mhz = float(self.input_vars["Frequency (MHz)"].get())
g_rx = float(self.input_vars["Receiver Antenna Gain (dBi)"].get())
l_rx = float(self.input_vars["Receiver Cable Loss (dB)"].get())
l_fade = float(self.input_vars["Fade Margin (dB)"].get())
l_misc = float(self.input_vars["Misc. Losses (dB)"].get())
p_rx_sensitivity = float(self.input_vars["Receiver Sensitivity (dBm)"].get())