RF amplifier

load "resistor.osdi"
load "capacitor.osdi"
load "inductor.osdi"
load "spice/bjt.osdi"

model i isource
model v vsource
model r resistor
model c capacitor
model l inductor

model q2sc3358 sp_bjt ( type=1 
    bf=120 br=1 is=16f eg=1.11 cjc=0.5p cje=0.75p vaf=200
    tf=23p  tr=2n  mjc=.330  vjc=.75  mje=.33  vje=.75
    cjs=1p 
)

parameters p=0.25

r1 (vcc 1) r r=470
r2 (1 2) r r=2.2k
c3 (2 0) c c=10n 
rp1 (2 20) r r=(1-p)*5k
rp2 (20 0) r r=p*5k
r3 (20 3) r r=1k
r4 (3 b) r r=1k
c1 (3 0) c c=1n
l3 (1 c) l l=10u 
// c9 (1 0) c c=1n
q1 (c b 0) q2sc3358

l4 (vcc out) l l=10u 

ci1 (in in1) c c=10p
ci2 (in1 b) c c=10p 
li1 (in1 0) l l=20n

co1 (c out1) c c=10p
co2 (out1 out) c c=10p
lo1 (out1 0)  l l=20n

vcc (vcc 0) v dc=12

vin (insrc 0) v dc=0 mag=1
rsrc (insrc in) r r=75

rl (out outsrc) r r=75
vout (outsrc 0) v dc=0 mag=0

control
  abort always

  analysis op1 op
  
  analysis ac1 ac from=100M to=1G mode="dec" points=1000

  analysis sp1 acsp ports=["vin", "rsrc", "vout", "rl"] from=100M to=1G mode="dec" points=1000
  
  postprocess(PYTHON, "runme.py")
endc

embed "runme.py" <<<FILE
from rawfile import rawread
import numpy as np
import matplotlib.pyplot as plt
import skrf  # registers the 'smith' matplotlib projection

raw = rawread('op1.raw')
plot = raw.get()
print("Ic=", plot["l3:flow(br)"])


fig1 = plt.figure(figsize=(10,8), dpi=100)
fig1.suptitle('RF amplifier')
ax_dict = fig1.subplot_mosaic("""
AB
CD
""", per_subplot_kw={"D": {"projection": "polar"}},
    gridspec_kw={"width_ratios": [1, 1.2]})

raw = rawread('ac1.raw')
plot = raw.get()

ax_dict["A"].set_ylabel('Gain [dB]')
ax_dict["A"].semilogx(np.real(plot['frequency'])/1e6, 20*np.log10(np.abs((plot['out']/plot['in']))))
ax_dict["A"].set_ylim(0, 40)

#ax_dict["B"].loglog(np.real(plot['frequency'])/1e6, np.abs(plot['in']/plot['vin:flow(br)']))

raw = rawread('sp1.raw')
plot = raw.get()
print(plot.names)

s11 = plot['s(1,1)']
s12 = plot['s(1,2)']
s21 = plot['s(2,1)']
s22 = plot['s(2,2)']

ax_dict["C"].set_ylabel('Sij [dB]')
ax_dict["C"].set_xlabel('f [MHz]')
ax_dict["C"].semilogx(np.real(plot['frequency'])/1e6, 20*np.log10(np.abs(s11)), "r-", label="s11")
ax_dict["C"].semilogx(np.real(plot['frequency'])/1e6, 20*np.log10(np.abs(s21)), "b-", label="s21")
ax_dict["C"].semilogx(np.real(plot['frequency'])/1e6, 20*np.log10(np.abs(s12)), "r--", label="s12")
ax_dict["C"].semilogx(np.real(plot['frequency'])/1e6, 20*np.log10(np.abs(s22)), "b--", label="s22")
ax_dict["C"].legend(loc="lower left", framealpha=1)
ax_dict["C"].set_ylim(-70, 30)

skrf.plotting.smith(ax=ax_dict["B"], draw_labels=True)
ax_dict["B"].plot(s11.real, s11.imag, "r-", label="s11")
ax_dict["B"].plot(s22.real, s22.imag, "b--", label="s22")
ax_dict["B"].set_aspect('equal')
ax_dict["B"].set_xlim(-1.0, 1.0)
ax_dict["B"].set_ylim(-1.0, 1.0)
ax_dict["B"].legend(loc="lower right", framealpha=1)

ax_dict["D"].plot(np.angle(s21), np.abs(s21), "b-", label="s21")
ax_dict["D"].plot(np.angle(s12), 100*np.abs(s12), "r--", label="100x s12")
ax_dict["D"].yaxis.set_major_locator(plt.MaxNLocator(4))
ax_dict["D"].legend(loc="upper left", framealpha=1)

fig1.tight_layout()

plt.show()
>>>FILE


