{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-Port Network: 'BFU520_05V0_010mA_NF_SP',  0.4-2.0 GHz, 1001 pts, z0=[50.+0.j 50.+0.j]"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "import skrf\n",
    "import skrf.media as media\n",
    "import skrf.frequency as freq\n",
    "import skrf.network as net\n",
    "import skrf.util\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "plt.rcParams['figure.figsize'] = [10, 10]\n",
    "\n",
    "f = freq.Frequency(0.4, 2, 1001)\n",
    "tem = media.DistributedCircuit(f, z0=50)\n",
    "\n",
    "bjt = net.Network('BFU520_Spar_NF_400MHz-2GHz/BFU520_05V0_010mA_NF_SP.s2p').interpolate(f)\n",
    "\n",
    "bjt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 143912 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bjt.plot_s_smith()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4.105588512516754, (2.478775227242761+4.2244480827589j))"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# calculate the stability circles for the source and load impedances\n",
    "\n",
    "idx_900mhz = skrf.util.find_nearest_index(bjt.f, 915.e+6)\n",
    "\n",
    "sqabs = lambda x: np.square(np.absolute(x))\n",
    "\n",
    "delta = bjt.s11.s*bjt.s22.s - bjt.s12.s*bjt.s21.s\n",
    "rl = np.absolute((bjt.s12.s * bjt.s21.s)/(sqabs(bjt.s22.s) - sqabs(delta)))\n",
    "cl = np.conj(bjt.s22.s - delta*np.conj(bjt.s11.s))/(sqabs(bjt.s22.s) - sqabs(delta))\n",
    "\n",
    "rl_900mhz = rl[idx_900mhz][0, 0]\n",
    "cl_900mhz = cl[idx_900mhz][0, 0]\n",
    "\n",
    "rl_900mhz, cl_900mhz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 132512 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def calc_circle(c, r):\n",
    "    theta = np.linspace(0, 2*np.pi, 1000)\n",
    "    return c + r*np.exp(1.0j*theta)\n",
    "\n",
    "def plot_smith(pts):\n",
    "    n = net.Network(s=pts)\n",
    "    n.plot_s_smith()\n",
    "    \n",
    "cl_points = calc_circle(cl_900mhz, rl_900mhz)\n",
    "plot_smith(cl_points)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2.8820382027100058, (-3.371227372367411+1.4990710463003132j))"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rs = np.absolute((bjt.s12.s * bjt.s21.s)/(sqabs(bjt.s11.s) - sqabs(delta)))\n",
    "cs = np.conj(bjt.s11.s - delta*np.conj(bjt.s22.s))/(sqabs(bjt.s11.s) - sqabs(delta))\n",
    "\n",
    "rs_900mhz = rs[idx_900mhz][0, 0]\n",
    "cs_900mhz = cs[idx_900mhz][0, 0]\n",
    "\n",
    "rs_900mhz, cs_900mhz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 133032 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cs_points = calc_circle(cs_900mhz, rs_900mhz)\n",
    "plot_smith(cs_points)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 190480 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# let's plot all of them\n",
    "# output stability first\n",
    "\n",
    "for i, f in enumerate(bjt.f):\n",
    "    # decimate it a little\n",
    "    if i % 100 != 0:\n",
    "        continue\n",
    "    n = net.Network(name=str(f/1.e+9), s=calc_circle(cl[i][0, 0], rl[i][0, 0]))\n",
    "    n.plot_s_smith()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 188980 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# input stability\n",
    "for i, f in enumerate(bjt.f):\n",
    "    if i % 100 != 0:\n",
    "        continue\n",
    "    n = net.Network(name=str(f/1.e+9), s=calc_circle(cs[i][0, 0], rs[i][0, 0]))\n",
    "    n.plot_s_smith()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[-0.56992216+0.82169869j]]), array([[0.90081035+0.43421275j]]))"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# let's see what simultaneous conjugate matching gets us\n",
    "b1 = 1 + sqabs(bjt.s11.s) - sqabs(bjt.s22.s) - sqabs(delta)\n",
    "b2 = 1 + sqabs(bjt.s22.s) - sqabs(bjt.s11.s) - sqabs(delta)\n",
    "c1 = bjt.s11.s - delta * np.conj(bjt.s22.s)\n",
    "c2 = bjt.s22.s - delta * np.conj(bjt.s11.s)\n",
    "\n",
    "gamma_ms = (b1 - np.sqrt(np.square(b1) - 4 * sqabs(c1) + 0.j))/(2*c1)\n",
    "gamma_ml = (b2 - np.sqrt(np.square(b2) - 4 * sqabs(c2) + 0.j))/(2*c2)\n",
    "\n",
    "gamma_ms[idx_900mhz], gamma_ml[idx_900mhz]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 129248 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_smith(gamma_ms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 128420 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_smith(gamma_ml)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21.769786511748308"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# so not very useful, because the transistor isn't unconditionally stable\n",
    "# time to draw the circles of constant gain and try to find a useful point\n",
    "\n",
    "K = (1 - sqabs(bjt.s11.s) - sqabs(bjt.s22.s) - sqabs(delta))/(2*np.absolute((bjt.s12.s)*(bjt.s21.s)))\n",
    "G_msg = np.absolute(bjt.s21.s)/np.absolute(bjt.s12.s)\n",
    "10*np.log10(G_msg[idx_900mhz, 0, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 205232 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# let's draw a bunch of curves I guess\n",
    "\n",
    "for G_p in [1, 2, 5, 10, 10**(17/10)]:\n",
    "    g_p = G_p/sqabs(bjt.s21.s)\n",
    "    r_p = (1 - 2*K*np.absolute(bjt.s12.s*bjt.s21.s)*g_p + sqabs(bjt.s12.s*bjt.s21.s)*(g_p**2))**(0.5)/np.absolute(1 + g_p * (sqabs(bjt.s22.s) - sqabs(delta)))\n",
    "    c_p = (g_p * np.conj(c2))/(1 + g_p*(sqabs(bjt.s22.s) - sqabs(delta)))\n",
    "    r, c = r_p[idx_900mhz, 0, 0], c_p[idx_900mhz, 0, 0]\n",
    "    #print(g_p, r, c)\n",
    "    points = calc_circle(c, r)\n",
    "    n = net.Network(name = 'G_p = {}'.format(G_p), s=points)\n",
    "    n.plot_s_smith()\n",
    "    \n",
    "cl_points_net = net.Network(name='stability circle', s=cl_points)\n",
    "cl_points_net.plot_s_smith()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 151492 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "load_points = calc_circle(c_p[idx_900mhz, 0, 0], r_p[idx_900mhz, 0, 0])\n",
    "\n",
    "load_points_net = net.Network(name='gain circle', s=load_points)\n",
    "cl_points_net = net.Network(name='stability circle', s=cl_points)\n",
    "\n",
    "load_points_net.plot_s_smith()\n",
    "cl_points_net.plot_s_smith()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.202730679312596"
      ]
     },
     "execution_count": 217,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.absolute(bjt.s21.s[idx_900mhz, 0, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-0.1682179966821933-0.37825243897096916j)"
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c_p_900mhz, r_p_900mhz = c_p[idx_900mhz, 0, 0], r_p[idx_900mhz, 0, 0]\n",
    "#d = c_p_900mhz - cl_900mhz\n",
    "#gamma_l = c_p_900mhz + r_p_900mhz * d/np.absolute(d)\n",
    "gamma_l = c_p_900mhz + r_p_900mhz * (np.exp(1.0j*np.pi*1.35))\n",
    "\n",
    "gamma_l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 219,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check stability against the stability circle\n",
    "is_gamma_l_stable = np.absolute(gamma_l - cl_900mhz) > rl_900mhz\n",
    "is_gamma_l_stable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(27.477891437216186-25.0862437367737j)"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z_l = net.s2z(np.array([[[gamma_l]]]))[0, 0, 0]\n",
    "z_l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(55.22772863185425, 24.87689799845171)"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q = np.sqrt((50 - np.real(z_l))/np.real(z_l)) \n",
    "x_1 = 50/Q\n",
    "x_2 = Q * np.real(z_l)\n",
    "\n",
    "x_1, x_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((27.477891437216186-25.0862437367737j),\n",
       " (27.477891437216183+24.87689799845171j),\n",
       " (27.477891437216183-25.086243736773696j),\n",
       " (-0-49.963141735225406j))"
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z_tmp = 1./(1./50 + 1./(1.0j * x_1))\n",
    "x_cl = 1.0j*(np.imag(z_l) - np.imag(z_tmp))\n",
    "z_eff = z_tmp + x_cl\n",
    "z_l, z_tmp, z_eff, x_cl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9.606301647538143e-09, (3.481362910233088e-12+0j))"
      ]
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# TODO set fc somewhere up above\n",
    "l_l = x_1/(2*np.pi*915e+6)\n",
    "c_l = 1./(1.0j*2*np.pi*915e+6*x_cl)\n",
    "\n",
    "l_l, c_l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((27.477891437216186-75.04938547199912j),\n",
       " (73.00983704515737+107.89464956975215j))"
      ]
     },
     "execution_count": 224,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# just to double check, let's apply these two components to S22 and see if the load effectively sees 50 ohms\n",
    "z_tmp = z_l - 1.0j/(2*np.pi*915e+6*c_l)\n",
    "z_eff = 1/(1.0/z_tmp + 1.0/(1.0j*2*np.pi*915e+6*l_l))\n",
    "z_tmp, z_eff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-Port Network: '',  0.4-2.0 GHz, 1001 pts, z0=[50.+0.j 50.+0.j]"
      ]
     },
     "execution_count": 225,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_network = tem.capacitor(c_l)**tem.shunt_inductor(l_l)\n",
    "output_network_backwards = tem.shunt_inductor(l_l) ** tem.capacitor(c_l)\n",
    "\n",
    "output_network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 132740 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "(output_network_backwards ** tem.load(np.conj(gamma_l))).plot_s_smith()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-0.2725778760603465+0.15443286430443276j)"
      ]
     },
     "execution_count": 227,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# woo, it probably works! time to work out the source matching network \n",
    "# and then cascade all three of them to see if that works\n",
    "\n",
    "gamma_in = bjt.s11.s + bjt.s12.s*bjt.s21.s*gamma_l/(1 - bjt.s22.s*gamma_l)\n",
    "gamma_s = np.conj(gamma_in)\n",
    "gamma_s = gamma_s[idx_900mhz, 0, 0]\n",
    "gamma_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 228,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check stability against the stability circle\n",
    "is_gamma_s_stable = np.absolute(gamma_s - cs_900mhz) > rs_900mhz\n",
    "is_gamma_s_stable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 229,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(27.440200161961304+9.397705357210816j)"
      ]
     },
     "execution_count": 229,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# time to calculate the source matching network\n",
    "z_s = net.s2z(np.array([[[gamma_s]]]))[0, 0, 0]\n",
    "z_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(55.14371498593006, 24.880623448168738)"
      ]
     },
     "execution_count": 230,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q = np.sqrt((50 - np.real(z_s))/np.real(z_s))\n",
    "x_1 = 50.0/Q\n",
    "x_2 = Q*np.real(z_s)\n",
    "\n",
    "x_1, x_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(55.14371498593006, 15.482918090957922)"
      ]
     },
     "execution_count": 231,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z_tmp = 1./(1./50. + 1./(1.0j * x_1))\n",
    "z_tmp\n",
    "\n",
    "x_l_s = x_1\n",
    "x_c_s = -np.imag(z_s) + np.imag(z_tmp)\n",
    "\n",
    "x_l_s, x_c_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9.59168832837311e-09, 1.1234305283660582e-11)"
      ]
     },
     "execution_count": 232,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#c_shunt_s = -1.0/(2*np.pi*915e+6*x_shunt_s)\n",
    "l_shunt_s = x_l_s/(2*np.pi*915e+6)\n",
    "c_series_s = 1.0/(2*np.pi*915e+6*x_c_s)\n",
    "l_shunt_s, c_series_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(27.440200161961307+9.397705357210816j)"
      ]
     },
     "execution_count": 233,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# double checking to make sure the match works as expected\n",
    "z_tmp = 1.0/(1.0/50.0 + 1/(1.0j*2*np.pi*915e+6*l_shunt_s))\n",
    "z_eff = z_tmp + 1/(1.0j*2*np.pi*915e+6*c_series_s)\n",
    "z_eff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 234,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 132264 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# now to implement it using two port networks\n",
    "input_network = tem.shunt_inductor(l_shunt_s) ** tem.capacitor(c_series_s)\n",
    "net.connect(input_network, 1, tem.load(gamma_in), 0).plot_s_smith()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 140904 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# let's see what the final network looks like\n",
    "output_network = tem.capacitor(c_l) ** tem.shunt_inductor(l_l)\n",
    "input_network = tem.shunt_inductor(l_shunt_s) ** tem.capacitor(c_series_s)\n",
    "\n",
    "amplifier = input_network ** bjt ** output_network\n",
    "amplifier.plot_s_smith()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[16.54391351]]), array([[45.12231266]]))"
      ]
     },
     "execution_count": 236,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gain = np.square(np.absolute(amplifier.s21.s[idx_900mhz]))\n",
    "10*np.log10(gain), gain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.04469069]])"
      ]
     },
     "execution_count": 237,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.absolute(amplifier.s12.s[idx_900mhz])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27316 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "amplifier.s11.plot_s_db()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 239,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31848 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "amplifier.s21.plot_s_db()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 140452 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# what does cascading two of these look like?\n",
    "\n",
    "cascaded = amplifier ** amplifier ** tem.shunt_delay_load(0.8, 0)\n",
    "cascaded.plot_s_smith()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[32.31845037]]), array([[1705.47374199]]))"
      ]
     },
     "execution_count": 241,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gain = np.square(np.absolute(cascaded.s21.s[idx_900mhz]))\n",
    "10*np.log10(gain), gain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27944 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "cascaded.s11.plot_s_db()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27180 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "cascaded.s22.plot_s_db()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3.481362910233088e-12+0j) 9.606301647538143e-09 9.59168832837311e-09 1.1234305283660582e-11\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 141072 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# let's see what the final network looks like with imperfect components\n",
    "print(c_l, l_l, l_shunt_s, c_series_s)\n",
    "output_network2 = tem.capacitor(3.9e-12) ** tem.shunt_inductor(12e-9)\n",
    "input_network2 = tem.shunt_inductor(9.5e-9) ** tem.capacitor(12e-12)\n",
    "\n",
    "amplifier2 = input_network2 ** bjt ** output_network2\n",
    "amplifier2.plot_s_smith()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30912 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "amplifier2.s11.plot_s_db()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[53.72251584]]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 31656 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(np.square(np.absolute(amplifier2.s21.s[idx_900mhz])))\n",
    "amplifier2.s21.plot_s_db()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26912 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "amplifier2.s22.plot_s_db()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
