Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
from scipy.signal import square, sawtooth
|
| 5 |
+
|
| 6 |
+
# Diode voltage drops
|
| 7 |
+
DIODE_VOLTAGES = {
|
| 8 |
+
"Ideal": 0.0,
|
| 9 |
+
"Silicon": 0.7,
|
| 10 |
+
"Germanium": 0.3
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def generate_waveform(wave_type, freq, amplitude, duration=0.05):
|
| 14 |
+
t = np.linspace(0, duration, 1000)
|
| 15 |
+
if wave_type == "Sine":
|
| 16 |
+
y = amplitude * np.sin(2 * np.pi * freq * t)
|
| 17 |
+
elif wave_type == "Square":
|
| 18 |
+
y = amplitude * square(2 * np.pi * freq * t)
|
| 19 |
+
elif wave_type == "Triangular":
|
| 20 |
+
y = amplitude * sawtooth(2 * np.pi * freq * t, width=0.5)
|
| 21 |
+
return t, y
|
| 22 |
+
|
| 23 |
+
def simulate_circuit(input_wave, diode_type, resistance):
|
| 24 |
+
v_diode = DIODE_VOLTAGES[diode_type]
|
| 25 |
+
output_wave = np.zeros_like(input_wave)
|
| 26 |
+
|
| 27 |
+
for i in range(len(input_wave)):
|
| 28 |
+
if input_wave[i] > v_diode:
|
| 29 |
+
output_wave[i] = input_wave[i] - v_diode
|
| 30 |
+
elif input_wave[i] < -v_diode:
|
| 31 |
+
output_wave[i] = input_wave[i] + v_diode
|
| 32 |
+
else:
|
| 33 |
+
output_wave[i] = 0
|
| 34 |
+
|
| 35 |
+
current = output_wave / resistance # I = V/R
|
| 36 |
+
return output_wave, current
|
| 37 |
+
|
| 38 |
+
# Streamlit UI
|
| 39 |
+
st.title("📈 Electronic Waveform Simulator")
|
| 40 |
+
st.markdown("Simulate circuit behavior with different diodes and waveforms")
|
| 41 |
+
|
| 42 |
+
col1, col2 = st.columns(2)
|
| 43 |
+
with col1:
|
| 44 |
+
diode_type = st.selectbox("Diode Type", ["Ideal", "Silicon", "Germanium"])
|
| 45 |
+
waveform_type = st.selectbox("Waveform Type", ["Sine", "Square", "Triangular"])
|
| 46 |
+
|
| 47 |
+
with col2:
|
| 48 |
+
freq = st.slider("Frequency (Hz)", 1, 1000, 50)
|
| 49 |
+
amplitude = st.slider("Amplitude (V)", 1.0, 20.0, 12.0)
|
| 50 |
+
resistance = st.slider("Resistance (Ω)", 1, 1000, 100)
|
| 51 |
+
|
| 52 |
+
# Generate waveforms
|
| 53 |
+
t, input_wave = generate_waveform(waveform_type, freq, amplitude)
|
| 54 |
+
output_wave, current = simulate_circuit(input_wave, diode_type, resistance)
|
| 55 |
+
|
| 56 |
+
# Create plots
|
| 57 |
+
fig = go.Figure()
|
| 58 |
+
fig.add_trace(go.Scatter(x=t, y=input_wave, name="Input Voltage", line=dict(color='blue')))
|
| 59 |
+
fig.add_trace(go.Scatter(x=t, y=output_wave, name="Output Voltage", line=dict(color='red')))
|
| 60 |
+
fig.update_layout(title="Voltage Waveforms",
|
| 61 |
+
xaxis_title="Time (s)",
|
| 62 |
+
yaxis_title="Voltage (V)",
|
| 63 |
+
hovermode="x unified")
|
| 64 |
+
|
| 65 |
+
fig2 = go.Figure()
|
| 66 |
+
fig2.add_trace(go.Scatter(x=t, y=current*1000, name="Current", line=dict(color='green')))
|
| 67 |
+
fig2.update_layout(title="Circuit Current",
|
| 68 |
+
xaxis_title="Time (s)",
|
| 69 |
+
yaxis_title="Current (mA)",
|
| 70 |
+
hovermode="x unified")
|
| 71 |
+
|
| 72 |
+
# Display results
|
| 73 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 74 |
+
st.plotly_chart(fig2, use_container_width=True)
|
| 75 |
+
|
| 76 |
+
# Circuit analysis
|
| 77 |
+
st.subheader("Circuit Analysis")
|
| 78 |
+
col3, col4, col5 = st.columns(3)
|
| 79 |
+
with col3:
|
| 80 |
+
st.metric("Peak Input Voltage", f"{np.max(input_wave):.2f} V")
|
| 81 |
+
with col4:
|
| 82 |
+
st.metric("Peak Output Voltage", f"{np.max(output_wave):.2f} V")
|
| 83 |
+
with col5:
|
| 84 |
+
st.metric("Max Current", f"{np.max(current)*1000:.2f} mA")
|
| 85 |
+
|
| 86 |
+
# Circuit diagram
|
| 87 |
+
st.subheader("Circuit Diagram")
|
| 88 |
+
st.code(f"""
|
| 89 |
+
AC Source ({amplitude}V {waveform_type})
|
| 90 |
+
────┤ Diode ({diode_type}) ├─── Resistor ({resistance}Ω) ────
|
| 91 |
+
""")
|
| 92 |
+
|
| 93 |
+
# Theory explanation
|
| 94 |
+
st.subheader("Theory Overview")
|
| 95 |
+
st.markdown(f"""
|
| 96 |
+
This simulator shows a simple diode-resistor circuit:
|
| 97 |
+
- **Input**: {waveform_type} wave at {freq}Hz with {amplitude}V amplitude
|
| 98 |
+
- **Diode**: {diode_type} (Forward voltage: {DIODE_VOLTAGES[diode_type]}V)
|
| 99 |
+
- **Load**: {resistance}Ω resistor
|
| 100 |
+
|
| 101 |
+
The output waveform shows:
|
| 102 |
+
- Positive half-cycles reduced by {DIODE_VOLTAGES[diode_type]}V diode drop
|
| 103 |
+
- Negative half-cycles {'clipped' if diode_type == 'Ideal' else 'partially conducted'}
|
| 104 |
+
- Current calculated using Ohm's Law (I = V/R)
|
| 105 |
+
""")
|