{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4ef639e0-984b-46ae-aa67-ceeb4c96392d",
   "metadata": {},
   "source": [
    "# Feedback network design considering noise and drive requirements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f22b2e03-3909-4a26-910f-deb26a3b1fa8",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "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",
    "import numpy as np\n",
    "from scipy.integrate import quad\n",
    "%store -r DIN_A\n",
    "sl.initProject(\"Feedback network noise\", notebook=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1d94bb7-9ece-45f2-ab60-cf4d1b701caa",
   "metadata": {},
   "source": [
    "# Specifications"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3cb8cdf3-a08c-4013-8bec-09752fd059bc",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>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\">$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",
       "</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_{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": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "specs = sl.csv2specs('A1specs.csv')\n",
    "sl.specs2html(specs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f593bb57-f4f5-409d-bcb2-8352ddc9caa0",
   "metadata": {},
   "source": [
    "# Circuit for evaluation of noise contribution feedback network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "46e8a05b-02a4-4d03-af79-13efa8a1c04b",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating netlist of kicad/noise2/noise2.kicad_sch using KiCAD\n",
      "Creating drawing-size SVG and PDF images of kicad/noise2/noise2.kicad_sch\n",
      "Plotted to '/home/anton/DATA/www/SEDwebsite/_build/SEDwebsite/EE4109-2024-2025/designExample/HearingLoop/SLiCAP/img/noise2.svg'.\n",
      "Done\n",
      "Checking netlist: cir/noise2.cir\n",
      "Warning less than two connections at node: 'out'.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<figure><img src=\"img/noise2.svg\" alt=\"\" style=\"width:500px\">\n",
       "</figure>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cir = sl.makeCircuit(\"kicad/noise2/noise2.kicad_sch\")\n",
    "sl.img2html(\"noise2.svg\", 500)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c16c497c-cacc-4077-8d48-7063cbc15925",
   "metadata": {},
   "source": [
    "# Noise analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3aff1570-b239-4fc9-bed4-3db4407d9013",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Warning less than two connections at node: 'out'.\n",
      "Warning less than two connections at node: 'out'.\n"
     ]
    }
   ],
   "source": [
    "sl.specs2circuit(specs, cir)\n",
    "R_i                 = sp.Symbol(\"R_i\")\n",
    "f_min               = cir.getParValue('f_min')\n",
    "f_max               = cir.getParValue('f_max')\n",
    "V_noise             = cir.getParValue('V_onoise')\n",
    "tau_i               = cir.getParValue('tau_i')\n",
    "f_fp                = cir.getParValue('f_fp')\n",
    "Vi_ADC              = cir.getParValue('Vi_ADC')\n",
    "n_SRC               = cir.getParValue('n_SRC')\n",
    "SRCnoise            = cir.getParValue('SRCnoise')\n",
    "P_max               = cir.getParValue('P_max')\n",
    "V_DD                = cir.getParValue('V_DD')\n",
    "\n",
    "noiseResult2 = sl.doNoise(cir, pardefs=\"circuit\")\n",
    "S_onoise     = noiseResult2.onoise"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dbe25f7-67b4-4ef9-8cf3-84b7860670c2",
   "metadata": {},
   "source": [
    "## Output noise spectrum (unweighted)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f2d7cb5c-702d-4d37-970e-16138a8e8f9a",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{1.639 \\cdot 10^{-12} R_{i}}{f^{2}} + \\frac{1.822 \\cdot 10^{-9} \\left(355.3 f^{2} + 4.785 \\cdot 10^{8}\\right)}{f^{2} \\left(39.48 f^{2} + 8.213 \\cdot 10^{9}\\right)} + \\frac{9.961}{f^{2} \\left(39.48 f^{2} + 8.213 \\cdot 10^{9}\\right)}$"
      ],
      "text/plain": [
       "1.639e-12*R_i/f**2 + 1.822e-9*(355.3*f**2 + 4.785e+8)/(f**2*(39.48*f**2 + 8.213e+9)) + 9.961/(f**2*(39.48*f**2 + 8.213e+9))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.N(S_onoise, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "801cf763-f187-4201-a304-bdac1fd795af",
   "metadata": {},
   "source": [
    "# Assign a budget for the noise contribution of the integrator resistance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fbdbdf8d-2045-4c41-b597-b95f801d2e25",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "\n",
      "Please assign a budget for the relative contribution of the noise of the integrator resistance to the squared DIN-A weighted RMS output noise (> 0 and < 0.978):\n",
      ">>>  0.2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.2"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n_Ri = 0\n",
    "while n_Ri > 1 - n_SRC or n_Ri <= 0:\n",
    "    try:\n",
    "        n_Ri = float(input(\"\\nPlease assign a budget for the relative contribution of \" +\n",
    "                           \"the noise of the integrator resistance to the \"+\n",
    "                           \"squared DIN-A weighted RMS output noise \" +\n",
    "                           \"(> 0 and < %s):\\n>>> \"%(sp.N(1-n_SRC,3))))\n",
    "    except ValueError:\n",
    "        n_Ri = 0\n",
    "n_Ri"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b09cc7d-6027-4b1c-8ae1-5410c988a7ad",
   "metadata": {},
   "source": [
    "## Noise requirement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2f766727-59fb-4b9c-b0a8-28c702b3b541",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2.501 \\cdot 10^{-11}$"
      ],
      "text/plain": [
       "2.501e-11"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "N_var_required  = V_noise**2 * (n_Ri + n_SRC)\n",
    "sp.N(N_var_required, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b15edac-6b6c-49a4-954c-0377922d3945",
   "metadata": {},
   "source": [
    "## Squared unweighted noise obtained versus $R_i$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d4817953-04e2-48a3-acea-892ddd6fca9f",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2.459 \\cdot 10^{-15} R_{i} + 2.347 \\cdot 10^{-12}$"
      ],
      "text/plain": [
       "2.459e-15*R_i + 2.347e-12"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "RMS_2_sq        = sl.rmsNoise(noiseResult2, \"onoise\", f_min, f_max)**2\n",
    "sp.N(RMS_2_sq, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77bd6842-42a0-4bb6-b2c5-00a348c7c18a",
   "metadata": {},
   "source": [
    "## Calculate $R_i$ from unweighted RMS noise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "422aaca4-c2e5-4812-b01a-4420af725dc4",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 9218.0$"
      ],
      "text/plain": [
       "9218."
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "try:\n",
    "    Ri_max      = sp.solve(RMS_2_sq - N_var_required, R_i)[0]\n",
    "except IndexError:\n",
    "    i = 2\n",
    "    Ri_max = 0\n",
    "sp.N(Ri_max, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e4a3a82-28fd-4101-8377-8fc8ce27c341",
   "metadata": {},
   "source": [
    "## Calculate noise spectrum with DIN_A weigthing with obtained $R_i$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "badad202-aa5d-4550-beaa-e2f5ea667f66",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{4.072 \\cdot 10^{-26} f^{6} \\left(2.688 \\cdot 10^{18} f^{2} + 2.917 \\cdot 10^{26}\\right)}{\\left(6.725 \\cdot 10^{-9} f^{2} + 1\\right)^{2} \\left(0.002356 f^{2} + 1\\right)^{2} \\left(39.48 f^{2} + 8.213 \\cdot 10^{9}\\right) \\left(100.0 f^{2} + 1.16 \\cdot 10^{6}\\right) \\left(100.0 f^{2} + 5.445 \\cdot 10^{7}\\right)}$"
      ],
      "text/plain": [
       "4.072e-26*f**6*(2.688e+18*f**2 + 2.917e+26)/((6.725e-9*f**2 + 1)**2*(0.002356*f**2 + 1)**2*(39.48*f**2 + 8.213e+9)*(100.0*f**2 + 1.16e+6)*(100.0*f**2 + 5.445e+7))"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S_weighted      = sp.simplify(S_onoise.subs(R_i, Ri_max) * DIN_A**2)\n",
    "sp.N(S_weighted, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24e170c3-f8d0-4bb7-a9ee-1bec888fe346",
   "metadata": {},
   "source": [
    "## Use numeric integration to find the squared weighted output noise voltage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "36003e36-9bc9-4f58-a929-e2ab54734acc",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2.571 \\cdot 10^{-11}$"
      ],
      "text/plain": [
       "2.571e-11"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "func_S_weighted = sp.lambdify(sl.ini.frequency, S_weighted)\n",
    "N_var_obtained  = quad(func_S_weighted, f_min, f_max)[0]\n",
    "sp.N(N_var_obtained, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05a1e8db-2aaf-45dc-804d-7a67dbefdb92",
   "metadata": {},
   "source": [
    "## Calculate a correction factor for the noise requirement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5386e4da-83ce-4ed6-a908-946812ada9ba",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0.9731$"
      ],
      "text/plain": [
       "0.9731"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "correction      = N_var_required/N_var_obtained\n",
    "sp.N(correction, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69e9f7de-f15b-41ef-ac53-113f8024efa2",
   "metadata": {},
   "source": [
    "## Repeat correcting until result within 0.1% from requirement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "969111ea-6781-48e1-91a5-4364119edee9",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.946885936439611\n",
      "0.999869431846901\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 8945.0$"
      ],
      "text/plain": [
       "8945."
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "N_var_corrected = N_var_obtained\n",
    "while np.abs(1-correction) > 0.001:\n",
    "    Ri_max          = sp.solve(RMS_2_sq - N_var_corrected, R_i)[0]\n",
    "    S_weighted      = S_onoise.subs(R_i, Ri_max) * DIN_A**2\n",
    "    func_S_weighted = sp.lambdify(sl.ini.frequency, S_weighted)\n",
    "    N_var_obtained  = quad(func_S_weighted, f_min, f_max)[0]\n",
    "    correction = N_var_required/N_var_obtained\n",
    "    N_var_corrected *= correction\n",
    "    print(correction)\n",
    "sp.N(Ri_max, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e3735c4-5141-46ef-a597-f3cc4243e631",
   "metadata": {},
   "source": [
    "## Check the maximum power dissiption"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "84d2abcf-3f95-4ad3-a24c-6d9a65351a57",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found no conflict between power dissipation and noise performance\n"
     ]
    }
   ],
   "source": [
    "I_max     = P_max/2/V_DD\n",
    "Ri_min    = V_DD/I_max\n",
    "if Ri_max < Ri_min:\n",
    "    print(\"Conflicting requirements between power dissipation and noise performance\")\n",
    "    conflict = True\n",
    "else:\n",
    "    print(\"Found no conflict between power dissipation and noise performance\")\n",
    "    conflict = False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53e87ffb-7f51-4fab-a343-b386eca97ecf",
   "metadata": {},
   "source": [
    "## Select a value for $R_i$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "cf22b9ba-38c4-462a-93c1-aedc0312e5d7",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "\n",
      "Please assign a value to the integrator resistance R_i (>= 3380.0Ohm, and <= 8944.8 Ohm):\n",
      ">>>  8900\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 8900.0$"
      ],
      "text/plain": [
       "8900."
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Ri = 0\n",
    "while Ri > Ri_max or Ri < Ri_min or conflict:\n",
    "    try:\n",
    "        Ri = float(input(\"\\nPlease assign a value to the integrator resistance R_i \" +\n",
    "                         \"(>= %sOhm, and <= %s Ohm):\\n>>> \"%(sp.N(Ri_min,5), sp.N(Ri_max,5))))\n",
    "    except ValueError:\n",
    "        Ri = 0\n",
    "sp.N(Ri, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44b277a7-bea4-4fe1-9ec3-794effb54f97",
   "metadata": {},
   "source": [
    "# Update the design specification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a5000fb5-925d-4f0a-84d4-094741d5dae4",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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_{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",
       "<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\">$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\">$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\">$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"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Ci = tau_i/Ri\n",
    "ZL_min          = sp.sqrt(Ri**2 + 1/(2*sp.pi*f_fp*Ci)**2)\n",
    "IL_fb_max       = sp.N(Vi_ADC/ZL_min)\n",
    "\n",
    "S_weighted      = S_onoise.subs(R_i, Ri) * DIN_A**2\n",
    "func_S_weighted = sp.lambdify(sl.ini.frequency, S_weighted)\n",
    "N_var_obtained  = quad(func_S_weighted, f_min, f_max)[0]\n",
    "n_Ri_obtained   = (N_var_obtained - SRCnoise**2)/V_noise**2\n",
    "\n",
    "specs.append(\n",
    "    sl.specItem('n_Ri',\n",
    "    description = \"Relative contribution of R_i noise to the \" +\n",
    "                  \"squared DIN-A weighted RMS output noise\",\n",
    "    value       = n_Ri,\n",
    "    units       = \"\",\n",
    "    specType    = \"budgets\",\n",
    "    )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "    sl.specItem('Ri_max',\n",
    "    description = \"Max. value of integrator resistor\",\n",
    "    value       = Ri_max,\n",
    "    units       = \"Omega\",\n",
    "    specType    = \"design\",\n",
    "    )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "    sl.specItem('R_i',\n",
    "    description = \"Selected value of integrator resistor\",\n",
    "    value       = Ri,\n",
    "    units       = \"Omega\",\n",
    "    specType    = \"design\",\n",
    "    )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "    sl.specItem('Ri_min',\n",
    "    description = \"Min. value of integrator resistor\",\n",
    "    value       = Ri_min,\n",
    "    units       = \"Omega\",\n",
    "    specType    = \"design\",\n",
    "    )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "    sl.specItem('C_i',\n",
    "    description = \"Required value of integrator capacitor\",\n",
    "    value       = Ci,\n",
    "    units       = \"F\",\n",
    "    specType    = \"design\",\n",
    "    )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "    sl.specItem('I_fb',\n",
    "    description = \"Maximum peak-peak current through feedback network\",\n",
    "    value       = IL_fb_max,\n",
    "    units       = \"A\",\n",
    "    specType    = \"design\",\n",
    "    )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "    sl.specItem(\"S_Ri\",\n",
    "    description = \"Squared DIN A weighted RMS output noise due to source and feedback network\",\n",
    "    value       = N_var_obtained,\n",
    "    units       = \"V^2/Hz\",\n",
    "    specType    = \"design\",\n",
    "    )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "    sl.specItem(\"n_Ri_obtained\",\n",
    "    description = \"Relative contribution of source and feedback network to squared DIN A weighted output noise\",\n",
    "    value       = n_Ri_obtained,\n",
    "    units       = \"\",\n",
    "    specType    = \"design\",\n",
    "    )\n",
    ")\n",
    "\n",
    "\n",
    "sl.specs2csv(specs, 'A1specs.csv')\n",
    "sl.specs2html(specs, types=[\"budgets\", \"design\"])"
   ]
  }
 ],
 "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
}
