Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
|
| 6 |
+
# Title of the app
|
| 7 |
+
st.title("Waveform Simulator for Diode Clipping Circuit")
|
| 8 |
+
|
| 9 |
+
# Sidebar for user inputs
|
| 10 |
+
st.sidebar.header("Circuit Parameters")
|
| 11 |
+
battery_voltage = st.sidebar.number_input("DC Battery Voltage (V)", min_value=-20.0, max_value=20.0, value=5.0, step=0.1)
|
| 12 |
+
diode_type = st.sidebar.selectbox("Diode Type", ["Germanium (Ge)", "Silicon (Si)"])
|
| 13 |
+
signal_type = st.sidebar.selectbox("Input Signal Type", ["AC", "DC"])
|
| 14 |
+
|
| 15 |
+
# Diode voltage drops
|
| 16 |
+
V_diode = 0.3 if diode_type == "Germanium (Ge)" else 0.7
|
| 17 |
+
|
| 18 |
+
# Define input voltage points for analysis
|
| 19 |
+
vin_points = [10, 8, 6, 5, 4, 3, 2, 1, 0.7, 0.3, 0]
|
| 20 |
+
if signal_type == "AC":
|
| 21 |
+
vin_full = vin_points + [0] + [-x for x in vin_points[::-1]] # Positive and negative half-cycles
|
| 22 |
+
else:
|
| 23 |
+
vin_full = vin_points # DC only (positive values)
|
| 24 |
+
|
| 25 |
+
# Circuit analysis function
|
| 26 |
+
def analyze_circuit(vin, v_bat, v_diode_drop):
|
| 27 |
+
"""Analyze a simple diode clipping circuit with battery bias."""
|
| 28 |
+
v_out = []
|
| 29 |
+
v_diode = []
|
| 30 |
+
|
| 31 |
+
for v in vin:
|
| 32 |
+
# Positive half-cycle or DC: Diode conducts if Vin > Vbat + Vdiode
|
| 33 |
+
if v > (v_bat + v_diode_drop):
|
| 34 |
+
v_o = v_bat # Clipped at battery voltage
|
| 35 |
+
v_d = v_diode_drop # Diode forward voltage
|
| 36 |
+
elif v < -v_diode_drop and signal_type == "AC":
|
| 37 |
+
# Negative half-cycle: Diode conducts in reverse (idealized model)
|
| 38 |
+
v_o = v # Passes through (no clipping in this simple model)
|
| 39 |
+
v_d = 0 # Diode off
|
| 40 |
+
else:
|
| 41 |
+
# Diode off: Vout = Vin (within battery bounds)
|
| 42 |
+
v_o = v
|
| 43 |
+
v_d = 0
|
| 44 |
+
|
| 45 |
+
v_out.append(v_o)
|
| 46 |
+
v_diode.append(v_d)
|
| 47 |
+
|
| 48 |
+
return v_diode, v_out
|
| 49 |
+
|
| 50 |
+
# Perform analysis
|
| 51 |
+
v_diode_values, v_out_values = analyze_circuit(vin_full, battery_voltage, V_diode)
|
| 52 |
+
|
| 53 |
+
# Create table
|
| 54 |
+
data = {
|
| 55 |
+
"V[in] (V)": vin_full,
|
| 56 |
+
f"V[{diode_type[:2]}] (V)": v_diode_values,
|
| 57 |
+
"V[out] (V)": v_out_values
|
| 58 |
+
}
|
| 59 |
+
df = pd.DataFrame(data)
|
| 60 |
+
st.subheader("Point-to-Point Analysis Table")
|
| 61 |
+
st.dataframe(df.style.format("{:.2f}"))
|
| 62 |
+
|
| 63 |
+
# Waveform plotting
|
| 64 |
+
fig = go.Figure()
|
| 65 |
+
fig.add_trace(go.Scatter(x=np.arange(len(vin_full)), y=vin_full, mode='lines+markers', name='V[in]', line=dict(color='blue')))
|
| 66 |
+
fig.add_trace(go.Scatter(x=np.arange(len(vin_full)), y=v_out_values, mode='lines+markers', name='V[out]', line=dict(color='red')))
|
| 67 |
+
fig.update_layout(
|
| 68 |
+
title="Input and Output Waveforms",
|
| 69 |
+
xaxis_title="Sample Points",
|
| 70 |
+
yaxis_title="Voltage (V)",
|
| 71 |
+
legend=dict(x=0, y=1),
|
| 72 |
+
template="plotly_white"
|
| 73 |
+
)
|
| 74 |
+
st.plotly_chart(fig)
|
| 75 |
+
|
| 76 |
+
# Explanation
|
| 77 |
+
st.subheader("How It Works")
|
| 78 |
+
st.write(f"""
|
| 79 |
+
- **Input Signal**: {signal_type}. For AC, both positive and negative cycles are analyzed; for DC, only positive values are used.
|
| 80 |
+
- **Diode**: {diode_type} with a forward voltage drop of {V_diode}V.
|
| 81 |
+
- **Battery**: Clips the output at {battery_voltage}V when the diode conducts.
|
| 82 |
+
- **Analysis**: For each V[in], the output V[out] is calculated based on diode conduction and battery bias.
|
| 83 |
+
- **Waveform**: Blue line is V[in], red line is V[out].
|
| 84 |
+
""")
|