Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ load_dotenv()
|
|
| 10 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 11 |
|
| 12 |
# Functions for Antenna Calculations
|
| 13 |
-
def calculate_microstrip_patch(frequency, permittivity, thickness, tangent_loss):
|
| 14 |
c = 3e8 # Speed of light in m/s
|
| 15 |
wavelength = c / frequency
|
| 16 |
effective_wavelength = wavelength / np.sqrt(permittivity)
|
|
@@ -18,17 +18,33 @@ def calculate_microstrip_patch(frequency, permittivity, thickness, tangent_loss)
|
|
| 18 |
patch_width = wavelength / (2 * np.sqrt(1 + permittivity))
|
| 19 |
|
| 20 |
# Adjustments for tangent loss (approximation)
|
| 21 |
-
# The tangent loss will slightly affect the width and length of the patch
|
| 22 |
adjustment_factor = 1 + tangent_loss / 10
|
| 23 |
patch_length *= adjustment_factor
|
| 24 |
patch_width *= adjustment_factor
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
return patch_length, patch_width, thickness
|
| 27 |
|
| 28 |
-
def calculate_dipole_antenna(frequency):
|
| 29 |
c = 3e8 # Speed of light in m/s
|
| 30 |
wavelength = c / frequency
|
| 31 |
dipole_length = wavelength / 2 # Length of dipole antenna is half wavelength
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
return dipole_length
|
| 33 |
|
| 34 |
def calculate_s11(frequency):
|
|
@@ -100,12 +116,12 @@ def plot_radiation_pattern(theta, gain_pattern):
|
|
| 100 |
return fig
|
| 101 |
|
| 102 |
# Main Function
|
| 103 |
-
def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_loss):
|
| 104 |
frequency_hz = frequency * 1e9
|
| 105 |
frequencies = np.linspace(frequency - 0.5, frequency + 0.5, 100)
|
| 106 |
|
| 107 |
if antenna_type == "Microstrip Patch":
|
| 108 |
-
patch_length, patch_width, thickness = calculate_microstrip_patch(frequency_hz, permittivity, thickness, tangent_loss)
|
| 109 |
s11_values = [calculate_s11(f) for f in frequencies]
|
| 110 |
directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
|
| 111 |
theta = np.linspace(-180, 180, 360)
|
|
@@ -122,10 +138,11 @@ def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_los
|
|
| 122 |
f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
|
| 123 |
f"Patch Dimensions: {patch_length:.2f} m x {patch_width:.2f} m x {thickness:.2f} m\n"
|
| 124 |
f"Substrate Tangent Loss: {tangent_loss:.2f}\n"
|
|
|
|
| 125 |
)
|
| 126 |
|
| 127 |
elif antenna_type == "Dipole":
|
| 128 |
-
dipole_length = calculate_dipole_antenna(frequency_hz)
|
| 129 |
s11_values = [calculate_s11(f) for f in frequencies]
|
| 130 |
directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
|
| 131 |
theta = np.linspace(-180, 180, 360)
|
|
@@ -141,6 +158,7 @@ def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_los
|
|
| 141 |
f"Operating Frequency: {frequency:.2f} GHz\n"
|
| 142 |
f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
|
| 143 |
f"Dipole Length: {dipole_length:.2f} m\n"
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
else:
|
|
@@ -157,17 +175,4 @@ with gr.Blocks() as demo:
|
|
| 157 |
permittivity = gr.Number(value=4.4, label="Substrate Permittivity")
|
| 158 |
thickness = gr.Number(value=0.01, label="Substrate Thickness (m)")
|
| 159 |
tangent_loss = gr.Number(value=0.02, label="Substrate Tangent Loss (tan δ)", step=0.01)
|
| 160 |
-
|
| 161 |
-
output_text = gr.Textbox(label="Design Results")
|
| 162 |
-
s11_plot = gr.Plot(label="S11 Plot")
|
| 163 |
-
directivity_gain_plot = gr.Plot(label="Directivity and Gain Plot")
|
| 164 |
-
radiation_pattern_plot = gr.Plot(label="Radiation Pattern")
|
| 165 |
-
antenna_3d_display = gr.Plot(label="3D Antenna Visualization")
|
| 166 |
-
|
| 167 |
-
design_button.click(
|
| 168 |
-
design_antenna,
|
| 169 |
-
inputs=[antenna_type, frequency, permittivity, thickness, tangent_loss],
|
| 170 |
-
outputs=[output_text, s11_plot, directivity_gain_plot, radiation_pattern_plot, antenna_3d_display]
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
demo.launch()
|
|
|
|
| 10 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 11 |
|
| 12 |
# Functions for Antenna Calculations
|
| 13 |
+
def calculate_microstrip_patch(frequency, permittivity, thickness, tangent_loss, impedance):
|
| 14 |
c = 3e8 # Speed of light in m/s
|
| 15 |
wavelength = c / frequency
|
| 16 |
effective_wavelength = wavelength / np.sqrt(permittivity)
|
|
|
|
| 18 |
patch_width = wavelength / (2 * np.sqrt(1 + permittivity))
|
| 19 |
|
| 20 |
# Adjustments for tangent loss (approximation)
|
|
|
|
| 21 |
adjustment_factor = 1 + tangent_loss / 10
|
| 22 |
patch_length *= adjustment_factor
|
| 23 |
patch_width *= adjustment_factor
|
| 24 |
|
| 25 |
+
# Consider impedance adjustment (this will be a simplified factor here)
|
| 26 |
+
if impedance == 50:
|
| 27 |
+
impedance_factor = 1.0
|
| 28 |
+
elif impedance == 73:
|
| 29 |
+
impedance_factor = 1.1
|
| 30 |
+
else:
|
| 31 |
+
impedance_factor = 1.0
|
| 32 |
+
|
| 33 |
+
patch_length *= impedance_factor
|
| 34 |
+
patch_width *= impedance_factor
|
| 35 |
+
|
| 36 |
return patch_length, patch_width, thickness
|
| 37 |
|
| 38 |
+
def calculate_dipole_antenna(frequency, impedance):
|
| 39 |
c = 3e8 # Speed of light in m/s
|
| 40 |
wavelength = c / frequency
|
| 41 |
dipole_length = wavelength / 2 # Length of dipole antenna is half wavelength
|
| 42 |
+
|
| 43 |
+
# Consider impedance adjustments (simplified)
|
| 44 |
+
if impedance == 50:
|
| 45 |
+
dipole_length *= 1.0
|
| 46 |
+
elif impedance == 73:
|
| 47 |
+
dipole_length *= 1.05 # A minor adjustment for 73 ohms impedance
|
| 48 |
return dipole_length
|
| 49 |
|
| 50 |
def calculate_s11(frequency):
|
|
|
|
| 116 |
return fig
|
| 117 |
|
| 118 |
# Main Function
|
| 119 |
+
def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_loss, impedance):
|
| 120 |
frequency_hz = frequency * 1e9
|
| 121 |
frequencies = np.linspace(frequency - 0.5, frequency + 0.5, 100)
|
| 122 |
|
| 123 |
if antenna_type == "Microstrip Patch":
|
| 124 |
+
patch_length, patch_width, thickness = calculate_microstrip_patch(frequency_hz, permittivity, thickness, tangent_loss, impedance)
|
| 125 |
s11_values = [calculate_s11(f) for f in frequencies]
|
| 126 |
directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
|
| 127 |
theta = np.linspace(-180, 180, 360)
|
|
|
|
| 138 |
f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
|
| 139 |
f"Patch Dimensions: {patch_length:.2f} m x {patch_width:.2f} m x {thickness:.2f} m\n"
|
| 140 |
f"Substrate Tangent Loss: {tangent_loss:.2f}\n"
|
| 141 |
+
f"Input Impedance: {impedance} Ohms\n"
|
| 142 |
)
|
| 143 |
|
| 144 |
elif antenna_type == "Dipole":
|
| 145 |
+
dipole_length = calculate_dipole_antenna(frequency_hz, impedance)
|
| 146 |
s11_values = [calculate_s11(f) for f in frequencies]
|
| 147 |
directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
|
| 148 |
theta = np.linspace(-180, 180, 360)
|
|
|
|
| 158 |
f"Operating Frequency: {frequency:.2f} GHz\n"
|
| 159 |
f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
|
| 160 |
f"Dipole Length: {dipole_length:.2f} m\n"
|
| 161 |
+
f"Input Impedance: {impedance} Ohms\n"
|
| 162 |
)
|
| 163 |
|
| 164 |
else:
|
|
|
|
| 175 |
permittivity = gr.Number(value=4.4, label="Substrate Permittivity")
|
| 176 |
thickness = gr.Number(value=0.01, label="Substrate Thickness (m)")
|
| 177 |
tangent_loss = gr.Number(value=0.02, label="Substrate Tangent Loss (tan δ)", step=0.01)
|
| 178 |
+
impedance = gr.Dropdown([50, 73], label="Input Impedance (Ohms)", value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|