A comprehensive toolkit for analog circuit simulation, generation, and AI-powered analysis. This project combines traditional circuit analysis with modern machine learning approaches for signal processing and circuit synthesis.
The codebase consists of three main components that work together to provide end-to-end circuit analysis capabilities:
- Circuit.py: Core circuit simulator using Modified Nodal Analysis (MNA)
- translator.py: SPICE-like netlist parser with subcircuit and function support
- plotter.py: Frequency response visualization (Bode plots)
- Transformer model: Decomposes mixed filter responses into constituent components
- Filter generation: Creates realistic lowpass/highpass filter responses in dB
- Training pipeline: End-to-end training with normalization and caching
- Test scripts: Comprehensive evaluation and demo tools
- Signal-to-circuit mapping:
-
Declare voltage sources (any line that starts with
V)Vin <n_node_no.> <p_node_no.> <value> -
Declare components (
R= resistor,C= capacitor,IOP= Ideal op amp,X= name of sub circuit)R1 <n_node_no.> <p_node_no.> <value> C1 <n_node_no.> <p_node_no.> <value> IOP <V+_no.> <V-_no.> <Vout_no.> X <start_node> <end_node> name -
Specify the output node
Vout <n_node_no.> -
End the netlist
.end
Fixed-component circuit blocks defined in .subckts:
* In .subckts/example.sp:
.declare_subckt in out
R1 in out 1k
R2 out 0 2k
* In main circuit:
X1 1 2 example
Parameterized circuit blocks defined in .functions:
* In .functions/example.sp:
* Parameters: R1, R2
R1 in out R1
R2 out 0 R2
* In main circuit:
.fn 1 2 example R1=1k R2=2k
- Subcircuits: Use
Xprefix for fixed blocks - Functions: Use
.fnfor parameterized blocks - Interface:
- Subcircuits:
.declare_subckt in out - Functions: Use
inandoutnodes
- Subcircuits:
* Input source
V1 1 0 1
* Low-pass filter
.fn 1 2 rc_lowpass R=1k C=1u
* Band-pass filter
.fn 2 3 bandpass R1=1k C1=1u R2=10k C2=0.1u
* Output amplifier
.fn 3 4 non_inverting_amp R1=1k R2=10k
* Function with parameters
Vin 0 1 5
.fn 1 2 voltage_divider R1=10k R2=20k
* Fixed subcircuit
X1 2 3 fixed_filter
Vout 3
.end
- Document parameters and circuit behavior
- Use standard units (k, u, n, p)
- Test with various parameter values
- Verify node connections
Available functions: rc_lowpass, rc_highpass, bandpass, voltage_divider, inverting_amp, non_inverting_amp, sallen_key_lowpass, sallen_key_highpass
analog/
├── src/
│ ├── circuit/ # Circuit simulation engine
│ │ ├── .functions/ # Parameterized circuit blocks
│ │ ├── .subckts/ # Fixed subcircuits
│ │ ├── Circuit.py # MNA solver
│ │ ├── translator.py # Netlist parser
│ │ ├── plotter.py # Bode plot generation
│ │ ├── optimization.py # Parameter optimization
│ │ └── circuitgen.py # Random circuit generation
│ └── transformer/ # AI components
│ ├── signal_decomposition/ # Filter response decomposition
│ │ ├── data/ # Filter response generation
│ │ ├── model/ # Transformer architecture
│ │ ├── train.py # Training pipeline
│ │ └── test_scripts/ # Evaluation and demos
│ └── neural_network/ # Circuit synthesis
│ ├── model.py # Neural architecture
│ └── functional_*.py # Circuit parameter generation
└── README.md
- Circuit Simulation: Complete MNA solver with SPICE-like netlist parsing
- Subcircuits & Functions: Modular circuit blocks (8 function types available)
- Signal Decomposition: Working transformer model with <1.1 dB reconstruction error
- Filter Response Generation: Realistic lowpass/highpass filters in dB domain
- Interactive Optimization: Draw target response and optimize circuit parameters
- Neural Circuit Synthesis: Basic framework exists for mapping responses to parameters
- Advanced Optimization: Gradient-based parameter tuning for circuit components
The ultimate goal is to link all three components into a unified circuit design pipeline:
- User Input → Target frequency response (drawn/specified)
- Signal Decomposition → Break complex response into basic filter components
- Circuit Synthesis → Map decomposed components to actual circuit topologies
- Parameter Optimization → Fine-tune component values using gradient descent
- Validation → Simulate final circuit and verify performance matches target
Integration Points:
- Connect signal decomposition output to circuit synthesis input
- Link circuit synthesis to optimization for parameter refinement
- Create unified API for end-to-end design flow
- Add feedback loop for iterative improvement
This would enable automatic circuit design from high-level specifications - a user could simply draw a desired frequency response and get a complete, optimized circuit implementation.
Signal Decomposition Transformer:
- Model: 768-dim, 12-head, 4-layer transformer
- Training data: 10k samples of mixed filter responses
- Performance: <1.1 dB average reconstruction error
- Input: 128-point frequency response → Output: 512-point decomposed signals
Circuit Simulation:
- Supports complex impedance analysis across frequency ranges
- Handles up to 10+ node circuits with multiple components
- Compatible with standard SPICE component models
Decomposes mixed frequency responses into constituent filter components:
# Generate filter responses (in dB)
from transformer.signal_decomposition.data.generation import generate_signal1, generate_signal2
lowpass = generate_signal1(cutoff_freq=1000, length=128) # 1kHz lowpass
highpass = generate_signal2(cutoff_freq=5000, length=128) # 5kHz highpass
mixed = lowpass + highpass # Combined response in dB domain
# Model predicts original components from mixed signal
model = SignalDecompositionTransformer(seq_len=128, d_model=768, ...)
predicted_lowpass, predicted_highpass = model(mixed)Key Features:
- Works in log-magnitude (dB) domain for realistic filter responses
- Advanced transformer architecture with positional encoding
- Upsampling to higher resolution outputs (128→512 points)
- Normalization for training stability
Maps frequency responses to circuit parameters:
# Generate RC lowpass data: frequency response → R,C values
from transformer.neural_network.model import FunctionalDataGeneration
data_gen = FunctionalDataGeneration("RC_lowpass", num_samples=50000, ...)
X_responses, y_parameters = data_gen.generate_data()
# Train neural network to predict circuit parameters
model = CircuitSynthesisNet(...)
predicted_params = model(frequency_response)from circuit.translator import Translator
from circuit.plotter import Plotter
# Parse and simulate circuit
translator = Translator("my_circuit.sp")
circuit = translator.circuit
# Generate frequency response
plotter = Plotter(circuit, output_node=2, freq_range=(1, 1e5))
plotter.plot_magnitude()from circuit.optimization import InteractiveLogDraw
# Draw target response, optimize circuit to match
optimizer = InteractiveLogDraw(x_sample_points=np.logspace(1, 6, 100))
# Draw on plot, press 's' to sample, then close to start optimization# Train signal decomposition model
cd src/transformer/signal_decomposition
python train.py
# Test decomposition on new signals
python test_scripts/test1.py
python test_scripts/interactive_demo.py