Spaces:
Sleeping
Sleeping
import streamlit as st | |
import numpy as np | |
import pandas as pd | |
import plotly.graph_objects as go | |
# Title of the app | |
st.title("Waveform Simulator for Diode Clipping Circuit") | |
# Sidebar for user inputs | |
st.sidebar.header("Circuit Parameters") | |
battery_voltage = st.sidebar.number_input("DC Battery Voltage (V)", min_value=-20.0, max_value=20.0, value=5.0, step=0.1) | |
diode_type = st.sidebar.selectbox("Diode Type", ["Germanium (Ge)", "Silicon (Si)"]) | |
signal_type = st.sidebar.selectbox("Input Signal Type", ["AC", "DC"]) | |
# Diode voltage drops | |
V_diode = 0.3 if diode_type == "Germanium (Ge)" else 0.7 | |
# Define input voltage points for analysis | |
vin_points = [10, 8, 6, 5, 4, 3, 2, 1, 0.7, 0.3, 0] | |
if signal_type == "AC": | |
vin_full = vin_points + [0] + [-x for x in vin_points[::-1]] # Positive and negative half-cycles | |
else: | |
vin_full = vin_points # DC only (positive values) | |
# Circuit analysis function | |
def analyze_circuit(vin, v_bat, v_diode_drop): | |
"""Analyze a simple diode clipping circuit with battery bias.""" | |
v_out = [] | |
v_diode = [] | |
for v in vin: | |
# Positive half-cycle or DC: Diode conducts if Vin > Vbat + Vdiode | |
if v > (v_bat + v_diode_drop): | |
v_o = v_bat # Clipped at battery voltage | |
v_d = v_diode_drop # Diode forward voltage | |
elif v < -v_diode_drop and signal_type == "AC": | |
# Negative half-cycle: Diode conducts in reverse (idealized model) | |
v_o = v # Passes through (no clipping in this simple model) | |
v_d = 0 # Diode off | |
else: | |
# Diode off: Vout = Vin (within battery bounds) | |
v_o = v | |
v_d = 0 | |
v_out.append(v_o) | |
v_diode.append(v_d) | |
return v_diode, v_out | |
# Perform analysis | |
v_diode_values, v_out_values = analyze_circuit(vin_full, battery_voltage, V_diode) | |
# Create table | |
data = { | |
"V[in] (V)": vin_full, | |
f"V[{diode_type[:2]}] (V)": v_diode_values, | |
"V[out] (V)": v_out_values | |
} | |
df = pd.DataFrame(data) | |
st.subheader("Point-to-Point Analysis Table") | |
st.dataframe(df.style.format("{:.2f}")) | |
# Waveform plotting | |
fig = go.Figure() | |
fig.add_trace(go.Scatter(x=np.arange(len(vin_full)), y=vin_full, mode='lines+markers', name='V[in]', line=dict(color='blue'))) | |
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'))) | |
fig.update_layout( | |
title="Input and Output Waveforms", | |
xaxis_title="Sample Points", | |
yaxis_title="Voltage (V)", | |
legend=dict(x=0, y=1), | |
template="plotly_white" | |
) | |
st.plotly_chart(fig) | |
# Explanation | |
st.subheader("How It Works") | |
st.write(f""" | |
- **Input Signal**: {signal_type}. For AC, both positive and negative cycles are analyzed; for DC, only positive values are used. | |
- **Diode**: {diode_type} with a forward voltage drop of {V_diode}V. | |
- **Battery**: Clips the output at {battery_voltage}V when the diode conducts. | |
- **Analysis**: For each V[in], the output V[out] is calculated based on diode conduction and battery bias. | |
- **Waveform**: Blue line is V[in], red line is V[out]. | |
""") |