# Render the PM_K-1 ngspice simulation data to PNG plots.
# Run inside the container AFTER the sims have written their .data files:
#   ./run.sh bash -lc 'cd /work/hardware/kicad; for s in ...; do ngspice -b ../eda/sim/$s.cir; done; gnuplot /work/hardware/eda/sim/plots.gp'
set terminal pngcairo size 1000,560 font "Sans,11"
set grid
set key box
P = "/work/hardware/eda/sim/plots/"

set logscale x
set xlabel "Frequency (Hz)"

set output P."input_loading.png"
set title "Input loading: line (25k) vs instrument (1M) input -- why Hi-Z preserves the pickup"
set ylabel "Level (dB)"
plot "/work/hardware/eda/sim/input_loading.csv" u 1:2 w l lw 2 t "line input (25k ohm)", \
     "" u 3:4 w l lw 2 t "instrument input (1M ohm)"

set output P."stage1_cmrr.png"
set title "Stage 1 receiver: common-mode leakage vs resistor match (lower = better; perfect match ~ -inf, off scale)"
set ylabel "Output leakage (dB, Vcm = 1V)"
plot P."stage1_cmrr.data" u 1:2 w l lw 2 t "0.1% mismatch", \
     "" u 3:4 w l lw 2 t "1% mismatch"

set output P."stage2_recon.png"
set title "Stage 2 DAC reconstruction filter -- flat to 20 kHz, -3 dB near 75 kHz"
set ylabel "Gain (dB)"
plot P."stage2_recon.data" u 1:2 w l lw 2 t "filter response"

set output P."stage4_driver.png"
set title "Stage 4 balanced output -- differential response (flat across the audio band)"
set ylabel "Differential gain (dB)"
plot P."stage4_driver.data" u 1:2 w l lw 2 t "hot - cold differential"

unset logscale x
set output P."stage1_phantom.png"
set title "Stage 1 protection: +48V phantom hit at the op-amp input -- clamped, then decays to ~0"
set xlabel "Time (s)"
set ylabel "Op-amp input (V)"
plot P."stage1_phantom.data" u 1:2 w l lw 2 t "V(op-amp input)"
