hussain2010 commited on
Commit
86383ac
·
verified ·
1 Parent(s): a47b35c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -37
app.py CHANGED
@@ -16,7 +16,7 @@ def calculate_microstrip_patch(frequency, permittivity, thickness, tangent_loss)
16
  effective_wavelength = wavelength / np.sqrt(permittivity)
17
  patch_length = effective_wavelength / 2
18
  patch_width = wavelength / (2 * np.sqrt(1 + permittivity))
19
- return patch_length, patch_width, thickness
20
 
21
  def calculate_dipole(frequency):
22
  c = 3e8 # Speed of light in m/s
@@ -25,8 +25,7 @@ def calculate_dipole(frequency):
25
  return dipole_length
26
 
27
  def calculate_s11(frequency):
28
- # Simulate S11 (just an example function for demonstration)
29
- s11 = -20 + 5 * np.cos(2 * np.pi * frequency / 10)
30
  return s11
31
 
32
  def calculate_directivity_and_gain(frequency):
@@ -41,25 +40,11 @@ def radiation_pattern(theta, frequency):
41
  # Graphing Functions
42
  def plot_3d_microstrip_patch(patch_length, patch_width, thickness):
43
  fig = go.Figure()
44
- # Patch
45
  fig.add_trace(go.Surface(
46
  z=[[0, 0], [0, 0]], x=[[0, patch_width], [0, patch_width]],
47
  y=[[0, 0], [patch_length, patch_length]], colorscale="Viridis", name="Patch"
48
  ))
49
- # Substrate
50
- fig.add_trace(go.Surface(
51
- z=[[-thickness, -thickness], [-thickness, -thickness]],
52
- x=[[0, patch_width], [0, patch_width]],
53
- y=[[0, 0], [patch_length, patch_length]], colorscale="Blues", name="Substrate"
54
- ))
55
- # Ground
56
- fig.add_trace(go.Surface(
57
- z=[[-thickness, -thickness], [-thickness, -thickness]],
58
- x=[[0, patch_width], [0, patch_width]],
59
- y=[[0, 0], [patch_length, patch_length]], colorscale="Greens", name="Ground"
60
- ))
61
- fig.update_traces(showscale=False)
62
- fig.update_layout(title="3D Microstrip Patch Antenna", showlegend=False)
63
  return fig
64
 
65
  def plot_s11_graph(frequencies, s11_values):
@@ -72,7 +57,7 @@ def plot_directivity_and_gain(frequencies, directivities, gains):
72
  fig = go.Figure()
73
  fig.add_trace(go.Scatter(x=frequencies, y=directivities, mode='lines', name="Directivity"))
74
  fig.add_trace(go.Scatter(x=frequencies, y=gains, mode='lines', name="Realized Gain"))
75
- fig.update_layout(title="Frequency vs. Directivity and Realized Gain",
76
  xaxis_title="Frequency (GHz)", yaxis_title="Gain (dBi)")
77
  return fig
78
 
@@ -85,25 +70,26 @@ def plot_radiation_pattern(theta, gain_pattern):
85
  # Main Function
86
  def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_loss, impedance):
87
  frequency_hz = frequency * 1e9
88
- frequencies = np.linspace(frequency - 0.5, frequency + 0.5, 100)
89
-
90
  if antenna_type == "Microstrip Patch":
91
- patch_length, patch_width, thickness = calculate_microstrip_patch(frequency_hz, permittivity, thickness, tangent_loss)
 
 
92
  s11_values = [calculate_s11(f) for f in frequencies]
93
  directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
94
  theta = np.linspace(-180, 180, 360)
95
  gain_pattern = radiation_pattern(theta, frequency_hz)
96
-
97
- s11_graph = plot_s11_graph(frequencies, s11_values)
98
- directivity_gain_graph = plot_directivity_and_gain(frequencies, directivities, gains)
99
  radiation_graph = plot_radiation_pattern(theta, gain_pattern)
100
  antenna_3d = plot_3d_microstrip_patch(patch_length, patch_width, thickness)
101
-
102
  output = (
103
  f"Design Type: Microstrip Patch Antenna\n"
104
  f"Operating Frequency: {frequency:.2f} GHz\n"
105
- f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
106
- f"Patch Dimensions: {patch_length:.2f} m x {patch_width:.2f} m x {thickness:.2f} m\n"
107
  f"Input Impedance: {impedance} Ohms"
108
  )
109
  elif antenna_type == "Dipole":
@@ -112,17 +98,15 @@ def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_los
112
  directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
113
  theta = np.linspace(-180, 180, 360)
114
  gain_pattern = radiation_pattern(theta, frequency_hz)
115
-
116
- s11_graph = plot_s11_graph(frequencies, s11_values)
117
- directivity_gain_graph = plot_directivity_and_gain(frequencies, directivities, gains)
118
  radiation_graph = plot_radiation_pattern(theta, gain_pattern)
119
- antenna_3d = None # Dipole does not have a 3D plot
120
-
121
  output = (
122
  f"Design Type: Dipole Antenna\n"
123
  f"Operating Frequency: {frequency:.2f} GHz\n"
124
- f"S11 at Operating Frequency: {s11_values[len(s11_values)//2]:.2f} dB\n"
125
- f"Dipole Length: {dipole_length:.2f} m\n"
126
  f"Input Impedance: {impedance} Ohms"
127
  )
128
 
@@ -130,12 +114,12 @@ def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_los
130
 
131
  # Gradio Interface
132
  with gr.Blocks() as demo:
133
- gr.Markdown("# Antenna Design Tool with Groq API")
134
  antenna_type = gr.Dropdown(["Microstrip Patch", "Dipole"], label="Select Antenna Type")
135
  frequency = gr.Slider(1.0, 10.0, step=0.1, label="Operating Frequency (GHz)")
136
  permittivity = gr.Number(value=4.4, label="Substrate Permittivity")
137
  thickness = gr.Number(value=0.01, label="Substrate Thickness (m)")
138
- tangent_loss = gr.Number(value=0.02, label="Substrate Tangent Loss (tan δ)", step=0.01)
139
  impedance = gr.Dropdown([50, 73], label="Input Impedance (Ohms)", value=50)
140
  design_button = gr.Button("Design Antenna")
141
  output_text = gr.Textbox(label="Design Results")
 
16
  effective_wavelength = wavelength / np.sqrt(permittivity)
17
  patch_length = effective_wavelength / 2
18
  patch_width = wavelength / (2 * np.sqrt(1 + permittivity))
19
+ return patch_length, patch_width, thickness, tangent_loss
20
 
21
  def calculate_dipole(frequency):
22
  c = 3e8 # Speed of light in m/s
 
25
  return dipole_length
26
 
27
  def calculate_s11(frequency):
28
+ s11 = -20 + 5 * np.cos(2 * np.pi * frequency / 10) # Mock S11 values
 
29
  return s11
30
 
31
  def calculate_directivity_and_gain(frequency):
 
40
  # Graphing Functions
41
  def plot_3d_microstrip_patch(patch_length, patch_width, thickness):
42
  fig = go.Figure()
 
43
  fig.add_trace(go.Surface(
44
  z=[[0, 0], [0, 0]], x=[[0, patch_width], [0, patch_width]],
45
  y=[[0, 0], [patch_length, patch_length]], colorscale="Viridis", name="Patch"
46
  ))
47
+ fig.update_layout(title="3D Microstrip Patch Antenna")
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  return fig
49
 
50
  def plot_s11_graph(frequencies, s11_values):
 
57
  fig = go.Figure()
58
  fig.add_trace(go.Scatter(x=frequencies, y=directivities, mode='lines', name="Directivity"))
59
  fig.add_trace(go.Scatter(x=frequencies, y=gains, mode='lines', name="Realized Gain"))
60
+ fig.update_layout(title="Frequency vs. Directivity and Gain",
61
  xaxis_title="Frequency (GHz)", yaxis_title="Gain (dBi)")
62
  return fig
63
 
 
70
  # Main Function
71
  def design_antenna(antenna_type, frequency, permittivity, thickness, tangent_loss, impedance):
72
  frequency_hz = frequency * 1e9
73
+ frequencies = np.linspace(frequency - 0.5, frequency + 0.5, 100) * 1e9 # Adjust to Hz
74
+
75
  if antenna_type == "Microstrip Patch":
76
+ patch_length, patch_width, thickness, tangent_loss = calculate_microstrip_patch(
77
+ frequency_hz, permittivity, thickness, tangent_loss
78
+ )
79
  s11_values = [calculate_s11(f) for f in frequencies]
80
  directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
81
  theta = np.linspace(-180, 180, 360)
82
  gain_pattern = radiation_pattern(theta, frequency_hz)
83
+ s11_graph = plot_s11_graph(frequencies / 1e9, s11_values) # Convert to GHz
84
+ directivity_gain_graph = plot_directivity_and_gain(frequencies / 1e9, directivities, gains)
 
85
  radiation_graph = plot_radiation_pattern(theta, gain_pattern)
86
  antenna_3d = plot_3d_microstrip_patch(patch_length, patch_width, thickness)
87
+
88
  output = (
89
  f"Design Type: Microstrip Patch Antenna\n"
90
  f"Operating Frequency: {frequency:.2f} GHz\n"
91
+ f"Patch Dimensions: {patch_length:.3f} m x {patch_width:.3f} m x {thickness:.3f} m\n"
92
+ f"Tangent Loss: {tangent_loss}\n"
93
  f"Input Impedance: {impedance} Ohms"
94
  )
95
  elif antenna_type == "Dipole":
 
98
  directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
99
  theta = np.linspace(-180, 180, 360)
100
  gain_pattern = radiation_pattern(theta, frequency_hz)
101
+ s11_graph = plot_s11_graph(frequencies / 1e9, s11_values) # Convert to GHz
102
+ directivity_gain_graph = plot_directivity_and_gain(frequencies / 1e9, directivities, gains)
 
103
  radiation_graph = plot_radiation_pattern(theta, gain_pattern)
104
+ antenna_3d = None # No 3D visualization for dipole
105
+
106
  output = (
107
  f"Design Type: Dipole Antenna\n"
108
  f"Operating Frequency: {frequency:.2f} GHz\n"
109
+ f"Dipole Length: {dipole_length:.3f} m\n"
 
110
  f"Input Impedance: {impedance} Ohms"
111
  )
112
 
 
114
 
115
  # Gradio Interface
116
  with gr.Blocks() as demo:
117
+ gr.Markdown("# Antenna Design Tool")
118
  antenna_type = gr.Dropdown(["Microstrip Patch", "Dipole"], label="Select Antenna Type")
119
  frequency = gr.Slider(1.0, 10.0, step=0.1, label="Operating Frequency (GHz)")
120
  permittivity = gr.Number(value=4.4, label="Substrate Permittivity")
121
  thickness = gr.Number(value=0.01, label="Substrate Thickness (m)")
122
+ tangent_loss = gr.Number(value=0.02, label="Tangent Loss (tan δ)", step=0.01)
123
  impedance = gr.Dropdown([50, 73], label="Input Impedance (Ohms)", value=50)
124
  design_button = gr.Button("Design Antenna")
125
  output_text = gr.Textbox(label="Design Results")