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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -26
app.py CHANGED
@@ -33,18 +33,66 @@ def calculate_directivity_and_gain(frequency):
33
  realized_gain = directivity - 1.5 # Efficiency loss
34
  return directivity, realized_gain
35
 
36
- def radiation_pattern(theta, frequency):
37
- gain = 10 * np.log10(np.abs(np.sin(np.radians(theta))) + 1e-9)
 
 
 
38
  return gain
39
 
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):
@@ -71,44 +119,35 @@ def plot_radiation_pattern(theta, gain_pattern):
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":
96
  dipole_length = calculate_dipole(frequency_hz)
97
- s11_values = [calculate_s11(f) for f in frequencies]
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
 
113
  return output, s11_graph, directivity_gain_graph, radiation_graph, antenna_3d
114
 
 
33
  realized_gain = directivity - 1.5 # Efficiency loss
34
  return directivity, realized_gain
35
 
36
+ def radiation_pattern(theta, frequency, antenna_type):
37
+ if antenna_type == "Microstrip Patch":
38
+ gain = 10 * np.log10(np.abs(np.sin(np.radians(theta))) + 1e-9) * frequency / 1e9
39
+ elif antenna_type == "Dipole":
40
+ gain = 10 * np.log10(np.abs(np.cos(np.radians(theta))) ** 2 + 1e-9)
41
  return gain
42
 
43
  # Graphing Functions
44
  def plot_3d_microstrip_patch(patch_length, patch_width, thickness):
45
  fig = go.Figure()
46
+
47
+ # Substrate
48
+ fig.add_trace(go.Surface(
49
+ z=[[0, 0], [0, 0]],
50
+ x=[[0, patch_width], [0, patch_width]],
51
+ y=[[0, 0], [patch_length, patch_length]],
52
+ colorscale="Blues", name="Substrate"
53
+ ))
54
+
55
+ # Patch (on upper layer of substrate)
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]],
60
+ colorscale="Viridis", name="Radiation Patch"
61
  ))
62
+
63
+ # Ground (on lower layer of substrate)
64
+ fig.add_trace(go.Surface(
65
+ z=[[-thickness, -thickness], [-thickness, -thickness]],
66
+ x=[[0, patch_width], [0, patch_width]],
67
+ y=[[0, 0], [patch_length, patch_length]],
68
+ colorscale="Greens", name="Ground Plane"
69
+ ))
70
+
71
+ fig.update_traces(showscale=False)
72
+ fig.update_layout(title="3D Microstrip Patch Antenna", showlegend=True)
73
+ return fig
74
+
75
+ def plot_3d_dipole(dipole_length):
76
+ fig = go.Figure()
77
+
78
+ # Dipole elements
79
+ fig.add_trace(go.Scatter3d(
80
+ x=[0, dipole_length / 2, 0, -dipole_length / 2],
81
+ y=[0, 0, 0, 0],
82
+ z=[0, 0, 0, 0],
83
+ mode="lines+markers",
84
+ line=dict(color="blue", width=5),
85
+ name="Dipole Elements"
86
+ ))
87
+
88
+ fig.update_layout(
89
+ title="3D Dipole Antenna",
90
+ scene=dict(
91
+ xaxis_title="X-axis",
92
+ yaxis_title="Y-axis",
93
+ zaxis_title="Z-axis"
94
+ )
95
+ )
96
  return fig
97
 
98
  def plot_s11_graph(frequencies, s11_values):
 
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) * 1e9 # Adjust to Hz
122
+ theta = np.linspace(-180, 180, 360)
123
 
124
  if antenna_type == "Microstrip Patch":
125
  patch_length, patch_width, thickness, tangent_loss = calculate_microstrip_patch(
126
  frequency_hz, permittivity, thickness, tangent_loss
127
  )
128
+ radiation_gain = radiation_pattern(theta, frequency_hz, antenna_type)
 
 
 
 
 
 
129
  antenna_3d = plot_3d_microstrip_patch(patch_length, patch_width, thickness)
 
130
  output = (
131
+ f"Microstrip Patch Antenna\n"
 
132
  f"Patch Dimensions: {patch_length:.3f} m x {patch_width:.3f} m x {thickness:.3f} m\n"
133
  f"Tangent Loss: {tangent_loss}\n"
134
  f"Input Impedance: {impedance} Ohms"
135
  )
136
  elif antenna_type == "Dipole":
137
  dipole_length = calculate_dipole(frequency_hz)
138
+ radiation_gain = radiation_pattern(theta, frequency_hz, antenna_type)
139
+ antenna_3d = plot_3d_dipole(dipole_length)
 
 
 
 
 
 
 
140
  output = (
141
+ f"Dipole Antenna\n"
 
142
  f"Dipole Length: {dipole_length:.3f} m\n"
143
  f"Input Impedance: {impedance} Ohms"
144
  )
145
+
146
+ s11_values = [calculate_s11(f) for f in frequencies]
147
+ directivities, gains = zip(*[calculate_directivity_and_gain(f) for f in frequencies])
148
+ s11_graph = plot_s11_graph(frequencies / 1e9, s11_values)
149
+ directivity_gain_graph = plot_directivity_and_gain(frequencies / 1e9, directivities, gains)
150
+ radiation_graph = plot_radiation_pattern(theta, radiation_gain)
151
 
152
  return output, s11_graph, directivity_gain_graph, radiation_graph, antenna_3d
153