Skip to content

Instantly share code, notes, and snippets.

@ericek111
Last active April 15, 2021 20:22
Show Gist options
  • Save ericek111/de12e7dcdb24d2c914c79223f57bc6ef to your computer and use it in GitHub Desktop.
Save ericek111/de12e7dcdb24d2c914c79223f57bc6ef to your computer and use it in GitHub Desktop.
NPN BJT characteristic curves GNUplot graphing macro
set macros
SMOOTH="smooth mcsplines"
set samples 1000
delta_v(x) = ( vD = x - old_v, old_v = x, vD)
old_v = NaN
LOAD_VALS(name, file) = sprintf("%s='%s';%s_S='%s_smoothed.dat';set table %s_S; plot '%s' lw 3 w l sm b;unset table", name, file, name, name, name, file);
CALC_DIFF(name) = sprintf("set table '%s_diff.dat';plot %s_S using 0:($1), '' using 0:(delta_v($1)); unset table", name, name)
eval(LOAD_VALS("FIRST", "first.csv"))
eval(LOAD_VALS("SECOND", "second.csv"))
eval(LOAD_VALS("THIRD", "third.csv"))
eval(LOAD_VALS("FOURTH", "fourth.csv"))
#eval(CALC_DIFF("FIRST"))
#eval(CALC_DIFF("SECOND"))
#eval(CALC_DIFF("THIRD"))
#eval(CALC_DIFF("FOURTH"))
# Margins for each row resp. column
TMARGIN = "set tmargin at screen 0.9; set bmargin at screen 0.5"
BMARGIN = "set tmargin at screen 0.5; set bmargin at screen 0.1"
LMARGIN = "set lmargin at screen 0.1; set rmargin at screen 0.5"
RMARGIN = "set lmargin at screen 0.5; set rmargin at screen 0.9"
# a b 
# c d
# --- GRAPH a
# @TMARGIN; @LMARGIN
# --- GRAPH b
# @TMARGIN; @RMARGIN
# --- GRAPH c
# @BMARGIN; @LMARGIN
# --- GRAPH d
# @BMARGIN; @RMARGIN
set datafile sep ','
set terminal png size 4000,2400 enhanced font "Liberation Sans,32"
set output './char.png'
set style data lines
#set autoscale ymax
#set offset graph 0.1, graph 0.1, graph 0.1, graph 0.1
#set xtics border out scale 1
#set ytics out mirror 1
set mxtics 8
set mytics 8
set grid x y
set ytics nomirror
#set y2tics
set samples 1000
set xzeroaxis
# https://stackoverflow.com/questions/30580471/smooth-peaks-in-gnuplot
set style line 1 lt rgb "dark-green" lw 2 pt 1
set style line 2 lt rgb "dark-violet" lw 2 pt 1
set style line 3 lt rgb "green" lw 2 pt 1
set style line 4 lt rgb "purple" lw 2 pt 1
set style line 5 lt rgb "blue" lw 2 pt 1
set style line 6 lt rgb "#BBFFFF" lw 2 pt 1
# u = using, t = title, w = with, lw = linewidth, lc = linecolor, lt = linetype, ls = linestyle ^, filledcu = filledcurves
# limegreen, darkviolet, green, pink, blue
set multiplot layout 2,2 rowsfirst
stats FIRST u 1 nooutput
MAX_UCE=STATS_max
set label 20 sprintf("%d / 3 = %d", MAX_UCE, MAX_UCE/3) at 0.650000, 0.950000, 0.00000 left norotate back textcolor rgb "#000000" nopoint
stats SECOND u 1 nooutput
MAX_IB=STATS_max
P_UCE = STATS_max / 3
P_IB = MAX_IB / 3
# show variables # < dumps variables into console
set arrow from graph 0,0.92 to graph 0,1.08 filled
set arrow from graph 0.95,0 to graph 1.05,0 filled
#set xlabel "Uce" offset 0,0
#set ylabel "Ic [mA]"
set label at graph 0, 1.12 "Ic [mA]" center
set label at graph 1.06, 0 "Uce [V]" left
set label at graph 1.05, 1.05 "BC547 - NPN BJT characteristic curves" right
# top right
@TMARGIN; @RMARGIN
plot FIRST u 1:2 w lines ls 1 @SMOOTH t " Ib = 50 uA" at end, \
FIRST u 1:3 w lines ls 2 @SMOOTH t " Ib = 150 uA" at end, \
FIRST u 1:4 w lines ls 3 @SMOOTH t " Ib = 300 uA" at end, \
FIRST u 1:5 w lines ls 4 @SMOOTH t " Ib = 400 uA" at end, \
FIRST u 1:6 w lines ls 5 @SMOOTH t " Ib = 450 uA" at end
unset label
unset arrow
unset xtics
set yrange [* : *] reverse
# bottom right
@BMARGIN; @RMARGIN
plot FOURTH u ($1):($2) w lines ls 1 @SMOOTH t " Ib = 50 uA" at end, \
FOURTH u ($1):($3) w lines ls 2 @SMOOTH t " Ib = 150 uA" at end, \
FOURTH u ($1):($4) w lines ls 3 @SMOOTH t " Ib = 300 uA" at end, \
FOURTH u ($1):($5) w lines ls 4 @SMOOTH t " Ib = 400 uA" at end, \
FOURTH u ($1):($6) w lines ls 5 @SMOOTH t " Ib = 450 uA" at end,
unset ytics
set arrow from graph 0.05,0 to graph -0.05,0 filled
set label at graph -0.06, 0 "Ib [uA]" right
set xrange [* : *] reverse
unset yrange
set xtics
# top left
@TMARGIN; @LMARGIN
plot SECOND u 1:2 w lines ls 1 @SMOOTH t " Uce = 2 V" at end, \
SECOND u 1:3 w lines ls 2 @SMOOTH t " Uce = 6 V" at end, \
SECOND u 1:4 w lines ls 3 @SMOOTH t " Uce = 10 V" at end, \
SECOND u 1:5 w lines ls 4 @SMOOTH t " Uce = 14 V" at end, \
SECOND u 1:6 w lines ls 5 @SMOOTH t " Uce = 18 V" at end
unset arrow
unset label
unset xtics
set label at graph 1, -0.12 "Ube [V]" center
set arrow from graph 1,0.08 to graph 1,-0.08 filled
set xrange [* : *] reverse
set yrange [* : *] reverse
# bottom left
@BMARGIN; @LMARGIN
plot THIRD u ($1):($2) w lines ls 1 @SMOOTH t " Uce = 2 V" at end, \
THIRD u ($1):($3) w lines ls 2 @SMOOTH t " Uce = 6 V" at end, \
THIRD u ($1):($4) w lines ls 3 @SMOOTH t " Uce = 10 V" at end, \
THIRD u ($1):($5) w lines ls 4 @SMOOTH t " Uce = 14 V" at end, \
THIRD u ($1):($6) w lines ls 5 @SMOOTH t " UCe = 18 V" at end
# GPVAL_DATA_Y_MAX
# Uce Ic pri Ib 50 150 300 400 450 uA
0 0 0 0 0 0
2 12.3 34 48.6 54 57.2
6 13.2 41.1 65.4 71.5 74.5
10 14 46 73 78.3 80.5
14 15 50.3 76.4 80.5 84.3
18 15.8 54.3 77.5 82.7 85
# Uce Ube pri Ib = 50 uA 150 300 400 450
0 0 0 0 0 0
2 0.722 0.745 0.767 0.773 0.777
6 0.708 0.705 0.704 0.704 0.708
10 0.692 0.653 0.628 0.626 0.633
14 0.674 0.593 0.548 0.537 0.542
18 0.654 0.525 0.460 0.455 0.456
# Ib Ic pri Uce = 2 V 6 10 14 18
0 0 0 0 0 0
50 12.3 13.2 14 15 15.8
150 34 41.1 46 50.3 54.3
300 48.6 65.4 73 76.4 77.5
400 54 71.5 78.3 80.5 82.7
450 57.2 74.5 74.5 84.3 85
# Ib Ube pri Uce = 2 V 6 10 14 18
0 0 0 0 0 0
50 0.722 0.708 0.692 0.674 0.654
150 0.745 0.705 0.653 0.593 0.525
300 0.767 0.704 0.628 0.548 0.460
400 0.773 0.704 0.626 0.537 0.455
450 0.777 0.708 0.633 0.542 0.456
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment