{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "57e9492e-16d3-46cc-841e-cab7af851bce",
   "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 numpy as np\n",
    "import sympy as sp\n",
    "from scipy.integrate import quad\n",
    "%store -r DIN_A\n",
    "sl.initProject(\"Source noise\", notebook=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cbce26ce-8575-4c47-a9a6-fc838b5db7df",
   "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"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Import the specifications\n",
    "specs = sl.csv2specs('A1specs.csv')\n",
    "sl.specs2html(specs, types=[\"performance\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ffa3d354-cd52-4119-b26f-17d27dacd315",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating netlist of kicad/noise1/noise1.kicad_sch using KiCAD\n",
      "Creating drawing-size SVG and PDF images of kicad/noise1/noise1.kicad_sch\n",
      "Plotted to '/home/anton/DATA/www/SEDwebsite/_build/SEDwebsite/EE4109-2024-2025/designExample/HearingLoop/SLiCAPnotebooks/Vamp/img/noise1.svg'.\n",
      "Done\n",
      "Checking netlist: cir/noise1.cir\n",
      "Warning less than two connections at node: 'out'.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<figure><img src=\"img/noise1.svg\" alt=\"\" style=\"width:400px\">\n",
       "</figure>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cir = sl.makeCircuit(\"kicad/noise1/noise1.kicad_sch\")\n",
    "# Load circuit parameters from specifications\n",
    "sl.img2html(\"noise1.svg\", 400)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dd95220d-b46c-4d9b-9b20-6a5f563cfc12",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table><caption>Table: Element data of expanded netlist 'noise1'</caption>\n",
       "<tr><th class=\"left\">RefDes</th><th class=\"left\">Nodes</th><th class=\"left\">Refs</th><th class=\"left\">Model</th><th class=\"left\">Param</th><th class=\"left\">Symbolic</th><th class=\"left\">Numeric</th></tr>\n",
       "<tr><td class=\"left\">E1</td><td class = \"left\">out 0 in 0 </td><td class = \"left\"></td><td class = \"left\">E</td>\n",
       "<td class=\"left\">value</td><td class=\"left\">$\\frac{1}{s \\tau_{i}}$</td><td class=\"left\">$\\frac{6.25 \\cdot 10^{4}}{s}$</td></tr>\n",
       "<tr><td class=\"left\">L1</td><td class = \"left\">1 in </td><td class = \"left\"></td><td class = \"left\">L</td>\n",
       "<td class=\"left\">value</td><td class=\"left\">$L_{s}$</td><td class=\"left\">$0.12$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">iinit</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "<tr><td class=\"left\">R1</td><td class = \"left\">1 2 </td><td class = \"left\"></td><td class = \"left\">R</td>\n",
       "<td class=\"left\">value</td><td class=\"left\">$R_{s}$</td><td class=\"left\">$875.0$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">noisetemp</td><td class=\"left\">$T$</td><td class=\"left\">$300.0$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">noiseflow</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">dcvar</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">dcvarlot</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "<tr><td class=\"left\">R2</td><td class = \"left\">in 0 </td><td class = \"left\"></td><td class = \"left\">R</td>\n",
       "<td class=\"left\">value</td><td class=\"left\">$R_{t}$</td><td class=\"left\">$1.0 \\cdot 10^{4}$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">noisetemp</td><td class=\"left\">$T$</td><td class=\"left\">$300.0$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">noiseflow</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">dcvar</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">dcvarlot</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "<tr><td class=\"left\">V1</td><td class = \"left\">2 0 </td><td class = \"left\"></td><td class = \"left\">V</td>\n",
       "<td class=\"left\">value</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">noise</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">dc</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "<tr><td></td><td></td><td></td><td></td><td class=\"left\">dcvar</td><td class=\"left\">$0$</td><td class=\"left\">$0$</td></tr>\n",
       "</table>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sl.specs2circuit(specs, cir)\n",
    "sl.elementData2html(cir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ed5dbcd1-2f0e-464d-ad63-fc0ede81216b",
   "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"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\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.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": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "noiseResult1 = sl.doNoise(cir, pardefs=\"circuit\")\n",
    "S_onoise     = noiseResult1.onoise\n",
    "sp.N(S_onoise, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e5d803c5-bc5c-41f8-b06b-62df308c368b",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{4.809 \\cdot 10^{-10} f^{6} \\left(118.4 f^{2} + 1.982 \\cdot 10^{9}\\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.809e-10*f**6*(118.4*f**2 + 1.982e+9)/((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": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S_o_weighted = sp.simplify(DIN_A**2*S_onoise)\n",
    "sp.N(S_o_weighted, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c8cbdc21-a308-4f1a-a891-d68cb63cd272",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0.0218900582930662$"
      ],
      "text/plain": [
       "0.0218900582930662"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_min           = cir.getParValue('f_min')\n",
    "f_max           = cir.getParValue('f_max')\n",
    "V_noise         = cir.getParValue('V_onoise')\n",
    "f               = sl.ini.frequency\n",
    "func_OnoiseVar  = sp.lambdify(f, S_o_weighted)\n",
    "N_var_1_dinA    = quad(func_OnoiseVar, f_min, f_max)[0]\n",
    "RMS_1           = np.sqrt(N_var_1_dinA)\n",
    "n_SRC           = N_var_1_dinA/V_noise**2\n",
    "n_SRC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3028a626-4153-4761-b7ad-1bb6cd60a744",
   "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_{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\">$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"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "specs.append(\n",
    "    sl.specItem('SRCnoise',\n",
    "    description = \"DIN A weighted RMS output noise, source + termination\",\n",
    "    value       = RMS_1,\n",
    "    units       = \"V\",\n",
    "    specType    = \"design\",\n",
    "    )\n",
    ")\n",
    "\n",
    "specs.append(\n",
    "    sl.specItem('n_SRC',\n",
    "    description = \"Relative contribution of source noise to the squared \" +\n",
    "                  \"DIN-A weighted RMS output noise\",\n",
    "    value       = n_SRC,\n",
    "    units       = \"\",\n",
    "    specType    = \"budgets\",\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
}
