{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "784861c5-1f40-498f-a2b5-c1f62221dbef",
   "metadata": {},
   "source": [
    "# Determination of $W$, $L$ and $I_{DS}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "4baa2ae6-93a7-4be9-aaf1-e39cf2baaa94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Compiling library: SLiCAP.lib.\n",
      "Compiling library: SLiCAPmodels.lib.\n"
     ]
    }
   ],
   "source": [
    "import SLiCAP as sl\n",
    "import sympy as sp\n",
    "from scipy.integrate import quad\n",
    "%store -r DIN_A\n",
    "sl.initProject(\"WLID\", notebook = True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c293de4-040d-4132-9cf9-6cf9bfc59fc5",
   "metadata": {},
   "source": [
    "## Specifications"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "77b14d9e-c9bf-4021-9bd9-8063090efaca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h3>design specification</h3>\n",
       "<table><a id=\"table_design\"></a><caption>Table design specification </caption><tr><th class=\"left\">symbol</th><th class=\"left\">description</th><th class=\"left\">value</th><th class=\"left\">units</th></tr>\n",
       "<td class=\"left\">$C_{i}$</td><td class=\"left\">Required value of integrator capacitor</td><td class=\"left\">$1.798 \\cdot 10^{-9}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$IC_{N}$</td><td class=\"left\">Inversion coefficient input stage NMOS</td><td class=\"left\">$0.001167$</td><td></td></tr>\n",
       "<td class=\"left\">$IC_{P}$</td><td class=\"left\">Inversion coefficient input stage PMOS</td><td class=\"left\">$1.197$</td><td></td></tr>\n",
       "<td class=\"left\">$ID_{N}$</td><td class=\"left\">Operating current input stage NMOS</td><td class=\"left\">$2.828 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{A}$</td></tr>\n",
       "<td class=\"left\">$ID_{P}$</td><td class=\"left\">Operating current input stage PMOS</td><td class=\"left\">$-4.36 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{A}$</td></tr>\n",
       "<td class=\"left\">$I_{fb}$</td><td class=\"left\">Maximum peak-peak current through feedback network</td><td class=\"left\">$4.542 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{A}$</td></tr>\n",
       "<td class=\"left\">$M_{N}$</td><td class=\"left\">Number of fingers input stage NMOS</td><td class=\"left\">$47$</td><td></td></tr>\n",
       "<td class=\"left\">$M_{P}$</td><td class=\"left\">Number of fingers input stage PMOS</td><td class=\"left\">$1$</td><td></td></tr>\n",
       "<td class=\"left\">$R_{i}$</td><td class=\"left\">Selected value of integrator resistor</td><td class=\"left\">$8900$</td><td class=\"left\">$\\mathrm{\\Omega}$</td></tr>\n",
       "<td class=\"left\">$Ri_{max}$</td><td class=\"left\">Max. value of integrator resistor</td><td class=\"left\">$8945.0$</td><td class=\"left\">$\\mathrm{\\Omega}$</td></tr>\n",
       "<td class=\"left\">$Ri_{min}$</td><td class=\"left\">Min. value of integrator resistor</td><td class=\"left\">$3380$</td><td class=\"left\">$\\mathrm{\\Omega}$</td></tr>\n",
       "<td class=\"left\">$SRCnoise$</td><td class=\"left\">DIN A weighted RMS output noise, source + termination</td><td class=\"left\">$1.571 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$S_{Ri}$</td><td class=\"left\">Squared DIN A weighted RMS output noise due to source and feedback network</td><td class=\"left\">$2.491 \\cdot 10^{-11}$</td><td class=\"left\">$\\mathrm{\\frac{V^{2}}{Hz}}$</td></tr>\n",
       "<td class=\"left\">$W_{F N}$</td><td class=\"left\">Finger width input stage NMOS</td><td class=\"left\">$5.0 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{m}$</td></tr>\n",
       "<td class=\"left\">$W_{F P}$</td><td class=\"left\">Finger width input stage PMOS</td><td class=\"left\">$1.0 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{m}$</td></tr>\n",
       "<td class=\"left\">$W_{N}$</td><td class=\"left\">Selected width input stage NMOS</td><td class=\"left\">$0.00235$</td><td class=\"left\">$\\mathrm{m}$</td></tr>\n",
       "<td class=\"left\">$W_{P}$</td><td class=\"left\">Selected width input stage PMOS</td><td class=\"left\">$9.848 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{m}$</td></tr>\n",
       "<td class=\"left\">$c_{issN}$</td><td class=\"left\">Selected input capacitance NMOS</td><td class=\"left\">$6.912 \\cdot 10^{-12}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{issP}$</td><td class=\"left\">Selected input capacitance PMOS</td><td class=\"left\">$2.41 \\cdot 10^{-14}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss N o}$</td><td class=\"left\">Obtained input capacitance input stage NMOS</td><td class=\"left\">$6.888 \\cdot 10^{-12}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss P o}$</td><td class=\"left\">Obtained input capacitance input stage PMOS</td><td class=\"left\">$2.41 \\cdot 10^{-14}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss costs minN}$</td><td class=\"left\">Required c_iss NMOS for minimum costs</td><td class=\"left\">$6.912 \\cdot 10^{-12}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss costs minP}$</td><td class=\"left\">Required c_iss PMOS for minimum costs</td><td class=\"left\">$2.41 \\cdot 10^{-14}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss g m minN}$</td><td class=\"left\">Required c_issN at g_m_minN</td><td class=\"left\">$9.87 \\cdot 10^{-10}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss g m minP}$</td><td class=\"left\">Required c_issN at g_m_minP</td><td class=\"left\">$8.736 \\cdot 10^{-10}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$g_{mN}$</td><td class=\"left\">Selected transconductance input stage NMOS</td><td class=\"left\">$7.854 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{mP}$</td><td class=\"left\">Selected transconductance input stage PMOS</td><td class=\"left\">$7.537 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m N o}$</td><td class=\"left\">Obtained transconductance input stage NMOS</td><td class=\"left\">$7.854 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m P o}$</td><td class=\"left\">Obtained transconductance input stage PMOS</td><td class=\"left\">$7.535 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m costs minN}$</td><td class=\"left\">g_m NMOS for minimum costs</td><td class=\"left\">$7.854 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m costs minP}$</td><td class=\"left\">g_m PMOS for minimum costs</td><td class=\"left\">$7.537 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m minN}$</td><td class=\"left\">Minimum transconductance input stage NMOS</td><td class=\"left\">$3.926 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m minP}$</td><td class=\"left\">Minimum transconductance input stage PMOS</td><td class=\"left\">$3.827 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$n_{Ri obtained}$</td><td class=\"left\">Relative contribution of source and feedback network to squared DIN A weighted output noise</td><td class=\"left\">$0.199$</td><td></td></tr>\n",
       "</table>\n",
       "<h3>performance specification</h3>\n",
       "<table><a id=\"table_performance\"></a><caption>Table performance specification </caption><tr><th class=\"left\">symbol</th><th class=\"left\">description</th><th class=\"left\">value</th><th class=\"left\">units</th></tr>\n",
       "<td class=\"left\">$L_{s}$</td><td class=\"left\">Source inductance</td><td class=\"left\">$0.12$</td><td class=\"left\">$\\mathrm{H}$</td></tr>\n",
       "<td class=\"left\">$P_{max}$</td><td class=\"left\">Maximum power dissipation at maximum supply voltage</td><td class=\"left\">$0.001$</td><td class=\"left\">$\\mathrm{W}$</td></tr>\n",
       "<td class=\"left\">$R_{s}$</td><td class=\"left\">Source resistance</td><td class=\"left\">$875$</td><td class=\"left\">$\\mathrm{\\Omega}$</td></tr>\n",
       "<td class=\"left\">$R_{t}$</td><td class=\"left\">Termination resistance</td><td class=\"left\">$1.0 \\cdot 10^{4}$</td><td class=\"left\">$\\mathrm{\\Omega}$</td></tr>\n",
       "<td class=\"left\">$V_{DD}$</td><td class=\"left\">Maximum supply voltage</td><td class=\"left\">$1.3$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$V_{onoise}$</td><td class=\"left\">DIN A weighted output voltage noise</td><td class=\"left\">$1.062 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$Vi_{ADC}$</td><td class=\"left\">Peak-peak input voltage ADC</td><td class=\"left\">$0.9$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$Vi_{pp}$</td><td class=\"left\">Maximum peak-peak input voltage</td><td class=\"left\">$0.2932$</td><td></td></tr>\n",
       "<td class=\"left\">$f_{fp}$</td><td class=\"left\">Maximum full-power frequency</td><td class=\"left\">$5000$</td><td class=\"left\">$\\mathrm{Hz}$</td></tr>\n",
       "<td class=\"left\">$f_{max}$</td><td class=\"left\">-3dB low-pass cut-off frequency</td><td class=\"left\">$6000$</td><td class=\"left\">$\\mathrm{Hz}$</td></tr>\n",
       "<td class=\"left\">$f_{min}$</td><td class=\"left\">-3dB high-pass cut-off frequency</td><td class=\"left\">$600$</td><td class=\"left\">$\\mathrm{Hz}$</td></tr>\n",
       "<td class=\"left\">$\\tau_{i}$</td><td class=\"left\">Integration time constant</td><td class=\"left\">$1.6 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{s}$</td></tr>\n",
       "</table>\n",
       "<h3>achievements specification</h3>\n",
       "<table><a id=\"table_achievements\"></a><caption>Table achievements specification </caption><tr><th class=\"left\">symbol</th><th class=\"left\">description</th><th class=\"left\">value</th><th class=\"left\">units</th></tr>\n",
       "<td class=\"left\">$V_{onoise WLI N}$</td><td class=\"left\">DIN A weighted RMS output noise with NMOS, W, L, and ID</td><td class=\"left\">$8.075 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$V_{onoise WLI P}$</td><td class=\"left\">DIN A weighted RMS output noise with PMOS, W, L, and ID</td><td class=\"left\">$1.071 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$V_{onoise gmCissN}$</td><td class=\"left\">DIN A weighted RMS output noise with NMOS and selected g_m and c_iss</td><td class=\"left\">$9.021 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$V_{onoise gmCissP}$</td><td class=\"left\">DIN A weighted RMS output noise with PMOS and selected g_m and c_iss</td><td class=\"left\">$9.02 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "</table>\n",
       "<h3>budgets specification</h3>\n",
       "<table><a id=\"table_budgets\"></a><caption>Table budgets specification </caption><tr><th class=\"left\">symbol</th><th class=\"left\">description</th><th class=\"left\">value</th><th class=\"left\">units</th></tr>\n",
       "<td class=\"left\">$n_{MN}$</td><td class=\"left\">Relative contribution controller NMOS input stagenoise to the squared DIN-A weighted RMS output noise</td><td class=\"left\">$0.5$</td><td></td></tr>\n",
       "<td class=\"left\">$n_{MP}$</td><td class=\"left\">Relative contribution controller PMOS input stagenoise to the squared DIN-A weighted RMS output noise</td><td class=\"left\">$0.5$</td><td></td></tr>\n",
       "<td class=\"left\">$n_{Ri}$</td><td class=\"left\">Relative contribution of R_i noise to the squared DIN-A weighted RMS output noise</td><td class=\"left\">$0.2$</td><td></td></tr>\n",
       "<td class=\"left\">$n_{SRC}$</td><td class=\"left\">Relative contribution of source noise to the squared DIN-A weighted RMS output noise</td><td class=\"left\">$0.02189$</td><td></td></tr>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "specs = sl.csv2specs('A1specs.csv')\n",
    "sl.specs2html(specs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60f9d485-12c0-4949-8dd8-5f52b71958cd",
   "metadata": {},
   "source": [
    "## Circuit model definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "aa832340-f422-41d5-b385-d47a69a7570c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Please select N or P type input CS stage:\n",
      ">>>  P\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating netlist of kicad/noiseWLI_p/noiseWLI_p.kicad_sch using KiCAD\n",
      "Creating drawing-size SVG and PDF images of kicad/noiseWLI_p/noiseWLI_p.kicad_sch\n",
      "Plotted to '/home/anton/DATA/www/SEDwebsite/_build/SEDwebsite/EE4109-2024-2025/designExample/HearingLoop/SLiCAP/img/noiseWLI_p.svg'.\n",
      "Done\n",
      "Checking netlist: cir/noiseWLI_p.cir\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<figure><img src=\"img/noiseWLI_p.svg\" alt=\"\" style=\"width:600px\">\n",
       "</figure>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mosType = False\n",
    "while not mosType:\n",
    "    mosType = input(\"Please select N or P type input CS stage:\\n>>> \").upper()\n",
    "    if mosType != \"N\" and mosType != \"P\":\n",
    "        mosType = False\n",
    "if mosType == \"N\":\n",
    "    cir = sl.makeCircuit(\"kicad/noiseWLI_n/noiseWLI_n.kicad_sch\")\n",
    "    img = \"noiseWLI_n.svg\"\n",
    "elif mosType == \"P\":\n",
    "    cir = sl.makeCircuit(\"kicad/noiseWLI_p/noiseWLI_p.kicad_sch\")\n",
    "    img = \"noiseWLI_p.svg\"\n",
    "sl.img2html(img, 600)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "599d000a-f07d-4ff5-9e75-61e5bf154ba8",
   "metadata": {},
   "source": [
    "## Maximum channel (finger) width definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "138518d7-c726-4991-a559-d01a02f0a1a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "MOS maximum finger width in [um] (>= 0.18 and <=50):\n",
      ">>>  10\n"
     ]
    }
   ],
   "source": [
    "W_finger = 0\n",
    "while W_finger < 0.18 or W_finger > 50:\n",
    "    try:\n",
    "        W_finger = float(input(\"MOS maximum finger width in [um] (>= 0.18 and <=50):\\n>>> \"))\n",
    "    except ValueError:\n",
    "        W_finger = 0\n",
    "W_finger *= 1e-6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "508c047b-c8a7-4d1c-8493-f254bd421437",
   "metadata": {},
   "source": [
    "## Maximum channel (finger) length definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "3ca13898-35e9-4549-a837-ffcab67fc5ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "MOS channel length in [um] (> 0.18):\n",
      ">>>  0.35\n"
     ]
    }
   ],
   "source": [
    "Le=0\n",
    "while Le < 0.18:\n",
    "    try:\n",
    "        Le = float(input(\"MOS channel length in [um] (> 0.18):\\n>>> \"))\n",
    "    except ValueError:\n",
    "        Le=0\n",
    "Le *= 1e-6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "091ecab7-d877-4d8a-8935-ad607aa3b636",
   "metadata": {},
   "source": [
    "## Define parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "f526bbcb-6b3d-4e38-8917-0f221a53e314",
   "metadata": {},
   "outputs": [],
   "source": [
    "sl.specs2circuit(specs, cir)\n",
    "IC_old = cir.getParValue('IC_X1') # Store the symbolic definition of the inversion coefficient\n",
    "ICinit = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcced736-0ef9-4f80-bce7-f6a556730ff6",
   "metadata": {},
   "source": [
    "## Determine $W$ and $I_{DS}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "f873a63d-f527-4090-ac59-56af97174886",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IC = 0.984\n",
      "IC = 0.980\n",
      "IC = 0.979\n"
     ]
    }
   ],
   "source": [
    "correction = True\n",
    "while correction:\n",
    "    # Delete definitions from previous runs\n",
    "    cir.delPar('IC_X1')\n",
    "    cir.delPar('W')\n",
    "    cir.delPar('L')\n",
    "    cir.delPar('ID')\n",
    "    IC_X1   = sp.Symbol('IC_X1')\n",
    "    L       = sp.Symbol('L')\n",
    "    x_X1    = sp.Symbol('x_X1')\n",
    "\n",
    "    # Get c_iss as a function of W, L and IC\n",
    "    ciss_IC  = cir.getParValue('c_iss_X1')\n",
    "    ciss_W   = ciss_IC.subs({IC_X1: ICinit, L: Le})\n",
    "    ciss     = cir.getParValue('c_iss%s'%(mosType))\n",
    "    W        = sp.Symbol('W')\n",
    "\n",
    "    width    = sp.solve(ciss_W-ciss)[0]\n",
    "\n",
    "    # round to W_finger\n",
    "    Mf       = int(width/W_finger)\n",
    "    if Mf > 0:\n",
    "        W_sel    = Mf*W_finger\n",
    "    else:\n",
    "        W_sel = width\n",
    "        Mf = 1\n",
    "\n",
    "    gm       = cir.getParValue('g_m_X1').subs({IC_X1: ICinit, L: Le})\n",
    "    g_m      = cir.getParValue('g_m%s'%(mosType))\n",
    "    ID       = sp.solve(gm-g_m)[0]\n",
    "\n",
    "    # Check the inversion coefficient\n",
    "    cir.defPar('IC_X1', IC_old)\n",
    "    cir.defPar('W', W_sel)\n",
    "    cir.defPar('L', Le)\n",
    "    cir.defPar('ID', ID)\n",
    "    IC = cir.getParValue('IC_X1')\n",
    "    print(\"IC =\", sp.N(IC,3))\n",
    "    if sp.Abs(1-IC/ICinit) < 0.001:\n",
    "        correction = False\n",
    "    else:\n",
    "        ICinit = IC"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddc91d82-15c8-4e9d-948e-e257a3ff570b",
   "metadata": {},
   "source": [
    "## Design verification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "a24ae63e-f98e-44bf-b86b-66ccc8800a3d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PMOS design results\n",
      "----------------------------\n",
      "RMS unweighted obtained     : 3.19e-5 V\n",
      "RMS DIN A weighted obtained : 1.09e-5 V\n",
      "DIN A RMS correction factor : 2.93\n",
      "Required value of c_iss     : 2.41e-14 F\n",
      "Obtained value of c_iss     : 2.18e-14 F\n",
      "Required value of g_m       : 7.54e-5 S\n",
      "Obtained value of g_m       : 7.54e-5 S\n",
      "Selected value of L         : 0.350 um\n",
      "Obtained value of W         : 0.0100 mm\n",
      "Obtained value of M         : 1.00\n",
      "Selected max. finger width  : 10.0 um\n",
      "Obtained value of ID        : -4.14 uA\n",
      "Obtained value of IC        : 0.979\n"
     ]
    }
   ],
   "source": [
    "f_min          = cir.getParValue('f_min')\n",
    "f_max          = cir.getParValue('f_max')\n",
    "noiseResult    = sl.doNoise(cir, pardefs=\"circuit\", numeric=True)\n",
    "RMS_unweigted  = sl.rmsNoise(noiseResult, 'onoise', 200, 6000)\n",
    "f              = sl.ini.frequency\n",
    "func_OnoiseVar = sp.lambdify(f, sp.N(noiseResult.onoise * DIN_A**2))\n",
    "N_var_dinA     = quad(func_OnoiseVar, f_min, f_max)[0]\n",
    "RMS_dinA       = sp.sqrt(N_var_dinA)\n",
    "print(\"%sMOS design results\\n----------------------------\"%(mosType))\n",
    "print(\"RMS unweighted obtained     :\", sp.N(RMS_unweigted, 3), 'V')\n",
    "print(\"RMS DIN A weighted obtained :\", sp.N(RMS_dinA, 3), 'V')\n",
    "print(\"DIN A RMS correction factor :\", sp.N(RMS_unweigted/RMS_dinA, 3))\n",
    "print(\"Required value of c_iss     :\", sp.N(cir.getParValue('c_iss%s'%(mosType)), 3), 'F')\n",
    "print(\"Obtained value of c_iss     :\", sp.N(cir.getParValue('c_iss_X1'), 3), 'F')\n",
    "print(\"Required value of g_m       :\", sp.N(cir.getParValue('g_m%s'%(mosType)), 3), 'S')\n",
    "print(\"Obtained value of g_m       :\", sp.N(cir.getParValue('g_m_X1'), 3), 'S')\n",
    "print(\"Selected value of L         :\", sp.N(cir.getParValue('L')*1e6, 3), 'um')\n",
    "print(\"Obtained value of W         :\", sp.N(cir.getParValue('W')*1e3, 3), 'mm')\n",
    "print(\"Obtained value of M         :\", sp.N(Mf, 3))\n",
    "print(\"Selected max. finger width  :\", sp.N(W_finger*1e6, 3), \"um\")\n",
    "print(\"Obtained value of ID        :\", sp.N(cir.getParValue('ID')*1e6, 3), 'uA')\n",
    "print(\"Obtained value of IC        :\", sp.N(cir.getParValue('IC_X1'), 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0335141f-d169-41ea-b636-f06892f2875b",
   "metadata": {},
   "source": [
    "## Update design specifications"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "e51e0e5c-b544-4a5b-9363-958089b32fbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Warning: symbol W_P already used, its definition will be overwritten!\n",
      "Warning: symbol g_m_P_o already used, its definition will be overwritten!\n",
      "Warning: symbol c_iss_P_o already used, its definition will be overwritten!\n",
      "Warning: symbol ID_P already used, its definition will be overwritten!\n",
      "Warning: symbol IC_P already used, its definition will be overwritten!\n",
      "Warning: symbol M_P already used, its definition will be overwritten!\n",
      "Warning: symbol W_F_P already used, its definition will be overwritten!\n",
      "Warning: symbol V_onoise_WLI_P already used, its definition will be overwritten!\n",
      "Warning: symbol W_P already used, its definition will be overwritten!\n",
      "Warning: symbol g_m_P_o already used, its definition will be overwritten!\n",
      "Warning: symbol c_iss_P_o already used, its definition will be overwritten!\n",
      "Warning: symbol ID_P already used, its definition will be overwritten!\n",
      "Warning: symbol IC_P already used, its definition will be overwritten!\n",
      "Warning: symbol M_P already used, its definition will be overwritten!\n",
      "Warning: symbol W_F_P already used, its definition will be overwritten!\n",
      "Warning: symbol V_onoise_WLI_P already used, its definition will be overwritten!\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<h3>design specification</h3>\n",
       "<table><a id=\"table_design\"></a><caption>Table design specification </caption><tr><th class=\"left\">symbol</th><th class=\"left\">description</th><th class=\"left\">value</th><th class=\"left\">units</th></tr>\n",
       "<td class=\"left\">$C_{i}$</td><td class=\"left\">Required value of integrator capacitor</td><td class=\"left\">$1.798 \\cdot 10^{-9}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$IC_{N}$</td><td class=\"left\">Inversion coefficient input stage NMOS</td><td class=\"left\">$0.001167$</td><td></td></tr>\n",
       "<td class=\"left\">$IC_{P}$</td><td class=\"left\">Inversion coefficient input stage PMOS</td><td class=\"left\">$0.9796$</td><td></td></tr>\n",
       "<td class=\"left\">$ID_{N}$</td><td class=\"left\">Operating current input stage NMOS</td><td class=\"left\">$2.828 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{A}$</td></tr>\n",
       "<td class=\"left\">$ID_{P}$</td><td class=\"left\">Operating current input stage PMOS</td><td class=\"left\">$-4.142 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{A}$</td></tr>\n",
       "<td class=\"left\">$I_{fb}$</td><td class=\"left\">Maximum peak-peak current through feedback network</td><td class=\"left\">$4.542 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{A}$</td></tr>\n",
       "<td class=\"left\">$M_{N}$</td><td class=\"left\">Number of fingers input stage NMOS</td><td class=\"left\">$47$</td><td></td></tr>\n",
       "<td class=\"left\">$M_{P}$</td><td class=\"left\">Number of fingers input stage PMOS</td><td class=\"left\">$1$</td><td></td></tr>\n",
       "<td class=\"left\">$R_{i}$</td><td class=\"left\">Selected value of integrator resistor</td><td class=\"left\">$8900$</td><td class=\"left\">$\\mathrm{\\Omega}$</td></tr>\n",
       "<td class=\"left\">$Ri_{max}$</td><td class=\"left\">Max. value of integrator resistor</td><td class=\"left\">$8945.0$</td><td class=\"left\">$\\mathrm{\\Omega}$</td></tr>\n",
       "<td class=\"left\">$Ri_{min}$</td><td class=\"left\">Min. value of integrator resistor</td><td class=\"left\">$3380$</td><td class=\"left\">$\\mathrm{\\Omega}$</td></tr>\n",
       "<td class=\"left\">$SRCnoise$</td><td class=\"left\">DIN A weighted RMS output noise, source + termination</td><td class=\"left\">$1.571 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$S_{Ri}$</td><td class=\"left\">Squared DIN A weighted RMS output noise due to source and feedback network</td><td class=\"left\">$2.491 \\cdot 10^{-11}$</td><td class=\"left\">$\\mathrm{\\frac{V^{2}}{Hz}}$</td></tr>\n",
       "<td class=\"left\">$W_{F N}$</td><td class=\"left\">Finger width input stage NMOS</td><td class=\"left\">$5.0 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{m}$</td></tr>\n",
       "<td class=\"left\">$W_{F P}$</td><td class=\"left\">Finger width input stage PMOS</td><td class=\"left\">$1.0 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{m}$</td></tr>\n",
       "<td class=\"left\">$W_{N}$</td><td class=\"left\">Selected width input stage NMOS</td><td class=\"left\">$0.00235$</td><td class=\"left\">$\\mathrm{m}$</td></tr>\n",
       "<td class=\"left\">$W_{P}$</td><td class=\"left\">Selected width input stage PMOS</td><td class=\"left\">$1.0 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{m}$</td></tr>\n",
       "<td class=\"left\">$c_{issN}$</td><td class=\"left\">Selected input capacitance NMOS</td><td class=\"left\">$6.912 \\cdot 10^{-12}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{issP}$</td><td class=\"left\">Selected input capacitance PMOS</td><td class=\"left\">$2.41 \\cdot 10^{-14}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss N o}$</td><td class=\"left\">Obtained input capacitance input stage NMOS</td><td class=\"left\">$6.888 \\cdot 10^{-12}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss P o}$</td><td class=\"left\">Obtained input capacitance input stage PMOS</td><td class=\"left\">$2.176 \\cdot 10^{-14}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss costs minN}$</td><td class=\"left\">Required c_iss NMOS for minimum costs</td><td class=\"left\">$6.912 \\cdot 10^{-12}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss costs minP}$</td><td class=\"left\">Required c_iss PMOS for minimum costs</td><td class=\"left\">$2.41 \\cdot 10^{-14}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss g m minN}$</td><td class=\"left\">Required c_issN at g_m_minN</td><td class=\"left\">$9.87 \\cdot 10^{-10}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$c_{iss g m minP}$</td><td class=\"left\">Required c_issN at g_m_minP</td><td class=\"left\">$8.736 \\cdot 10^{-10}$</td><td class=\"left\">$\\mathrm{F}$</td></tr>\n",
       "<td class=\"left\">$g_{mN}$</td><td class=\"left\">Selected transconductance input stage NMOS</td><td class=\"left\">$7.854 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{mP}$</td><td class=\"left\">Selected transconductance input stage PMOS</td><td class=\"left\">$7.537 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m N o}$</td><td class=\"left\">Obtained transconductance input stage NMOS</td><td class=\"left\">$7.854 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m P o}$</td><td class=\"left\">Obtained transconductance input stage PMOS</td><td class=\"left\">$7.539 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m costs minN}$</td><td class=\"left\">g_m NMOS for minimum costs</td><td class=\"left\">$7.854 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m costs minP}$</td><td class=\"left\">g_m PMOS for minimum costs</td><td class=\"left\">$7.537 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m minN}$</td><td class=\"left\">Minimum transconductance input stage NMOS</td><td class=\"left\">$3.926 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$g_{m minP}$</td><td class=\"left\">Minimum transconductance input stage PMOS</td><td class=\"left\">$3.827 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{\\mathtt{\\text{S}}}$</td></tr>\n",
       "<td class=\"left\">$n_{Ri obtained}$</td><td class=\"left\">Relative contribution of source and feedback network to squared DIN A weighted output noise</td><td class=\"left\">$0.199$</td><td></td></tr>\n",
       "</table>\n",
       "<h3>performance specification</h3>\n",
       "<table><a id=\"table_performance\"></a><caption>Table performance specification </caption><tr><th class=\"left\">symbol</th><th class=\"left\">description</th><th class=\"left\">value</th><th class=\"left\">units</th></tr>\n",
       "<td class=\"left\">$L_{s}$</td><td class=\"left\">Source inductance</td><td class=\"left\">$0.12$</td><td class=\"left\">$\\mathrm{H}$</td></tr>\n",
       "<td class=\"left\">$P_{max}$</td><td class=\"left\">Maximum power dissipation at maximum supply voltage</td><td class=\"left\">$0.001$</td><td class=\"left\">$\\mathrm{W}$</td></tr>\n",
       "<td class=\"left\">$R_{s}$</td><td class=\"left\">Source resistance</td><td class=\"left\">$875$</td><td class=\"left\">$\\mathrm{\\Omega}$</td></tr>\n",
       "<td class=\"left\">$R_{t}$</td><td class=\"left\">Termination resistance</td><td class=\"left\">$1.0 \\cdot 10^{4}$</td><td class=\"left\">$\\mathrm{\\Omega}$</td></tr>\n",
       "<td class=\"left\">$V_{DD}$</td><td class=\"left\">Maximum supply voltage</td><td class=\"left\">$1.3$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$V_{onoise}$</td><td class=\"left\">DIN A weighted output voltage noise</td><td class=\"left\">$1.062 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$Vi_{ADC}$</td><td class=\"left\">Peak-peak input voltage ADC</td><td class=\"left\">$0.9$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$Vi_{pp}$</td><td class=\"left\">Maximum peak-peak input voltage</td><td class=\"left\">$0.2932$</td><td></td></tr>\n",
       "<td class=\"left\">$f_{fp}$</td><td class=\"left\">Maximum full-power frequency</td><td class=\"left\">$5000$</td><td class=\"left\">$\\mathrm{Hz}$</td></tr>\n",
       "<td class=\"left\">$f_{max}$</td><td class=\"left\">-3dB low-pass cut-off frequency</td><td class=\"left\">$6000$</td><td class=\"left\">$\\mathrm{Hz}$</td></tr>\n",
       "<td class=\"left\">$f_{min}$</td><td class=\"left\">-3dB high-pass cut-off frequency</td><td class=\"left\">$600$</td><td class=\"left\">$\\mathrm{Hz}$</td></tr>\n",
       "<td class=\"left\">$\\tau_{i}$</td><td class=\"left\">Integration time constant</td><td class=\"left\">$1.6 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{s}$</td></tr>\n",
       "</table>\n",
       "<h3>achievements specification</h3>\n",
       "<table><a id=\"table_achievements\"></a><caption>Table achievements specification </caption><tr><th class=\"left\">symbol</th><th class=\"left\">description</th><th class=\"left\">value</th><th class=\"left\">units</th></tr>\n",
       "<td class=\"left\">$V_{onoise WLI N}$</td><td class=\"left\">DIN A weighted RMS output noise with NMOS, W, L, and ID</td><td class=\"left\">$8.075 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$V_{onoise WLI P}$</td><td class=\"left\">DIN A weighted RMS output noise with PMOS, W, L, and ID</td><td class=\"left\">$1.088 \\cdot 10^{-5}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$V_{onoise gmCissN}$</td><td class=\"left\">DIN A weighted RMS output noise with NMOS and selected g_m and c_iss</td><td class=\"left\">$9.021 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "<td class=\"left\">$V_{onoise gmCissP}$</td><td class=\"left\">DIN A weighted RMS output noise with PMOS and selected g_m and c_iss</td><td class=\"left\">$9.02 \\cdot 10^{-6}$</td><td class=\"left\">$\\mathrm{V}$</td></tr>\n",
       "</table>\n",
       "<h3>budgets specification</h3>\n",
       "<table><a id=\"table_budgets\"></a><caption>Table budgets specification </caption><tr><th class=\"left\">symbol</th><th class=\"left\">description</th><th class=\"left\">value</th><th class=\"left\">units</th></tr>\n",
       "<td class=\"left\">$n_{MN}$</td><td class=\"left\">Relative contribution controller NMOS input stagenoise to the squared DIN-A weighted RMS output noise</td><td class=\"left\">$0.5$</td><td></td></tr>\n",
       "<td class=\"left\">$n_{MP}$</td><td class=\"left\">Relative contribution controller PMOS input stagenoise to the squared DIN-A weighted RMS output noise</td><td class=\"left\">$0.5$</td><td></td></tr>\n",
       "<td class=\"left\">$n_{Ri}$</td><td class=\"left\">Relative contribution of R_i noise to the squared DIN-A weighted RMS output noise</td><td class=\"left\">$0.2$</td><td></td></tr>\n",
       "<td class=\"left\">$n_{SRC}$</td><td class=\"left\">Relative contribution of source noise to the squared DIN-A weighted RMS output noise</td><td class=\"left\">$0.02189$</td><td></td></tr>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "specs.append(\n",
    "        sl.specItem('W_%s'%(mosType),\n",
    "        description = \"Selected width input stage %sMOS\"%(mosType),\n",
    "        value       = sp.N(cir.getParValue('W')),\n",
    "        units       = \"m\",\n",
    "        specType    = \"design\",\n",
    "        )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "        sl.specItem('g_m_%s_o'%(mosType),\n",
    "        description = \"Obtained transconductance input stage %sMOS\"%(mosType),\n",
    "        value       = sp.N(cir.getParValue('g_m_X1')),\n",
    "        units       = \"S\",\n",
    "        specType    = \"design\",\n",
    "        )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "        sl.specItem('c_iss_%s_o'%(mosType),\n",
    "        description = \"Obtained input capacitance input stage %sMOS\"%(mosType),\n",
    "        value       = sp.N(cir.getParValue('c_iss_X1')),\n",
    "        units       = \"F\",\n",
    "        specType    = \"design\",\n",
    "        )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "        sl.specItem('ID_%s'%(mosType),\n",
    "        description = \"Operating current input stage %sMOS\"%(mosType),\n",
    "        value       = sp.N(cir.getParValue('ID')),\n",
    "        units       = \"A\",\n",
    "        specType    = \"design\",\n",
    "        )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "        sl.specItem('IC_%s'%(mosType),\n",
    "        description = \"Inversion coefficient input stage %sMOS\"%(mosType),\n",
    "        value       = sp.N(cir.getParValue('IC_X1')),\n",
    "        units       = \"\",\n",
    "        specType    = \"design\",\n",
    "        )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "        sl.specItem('M_%s'%(mosType),\n",
    "        description = \"Number of fingers input stage %sMOS\"%(mosType),\n",
    "        value       = Mf,\n",
    "        units       = \"\",\n",
    "        specType    = \"design\",\n",
    "        )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "        sl.specItem('W_F_%s'%(mosType),\n",
    "        description = \"Finger width input stage %sMOS\"%(mosType),\n",
    "        value       = W_finger,\n",
    "        units       = \"m\",\n",
    "        specType    = \"design\",\n",
    "        )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "        sl.specItem('V_onoise_WLI_%s'%(mosType),\n",
    "        description = \"DIN A weighted RMS output noise with %sMOS, W, L, and ID\"%(mosType),\n",
    "        value       = RMS_dinA,\n",
    "        units       = \"V\",\n",
    "        specType    = \"achievements\",\n",
    "        )\n",
    ")\n",
    "\n",
    "sl.specs2csv(specs, 'A1specs.csv')\n",
    "sl.specs2html(specs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
