{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Loading a Hadamard Ramp Simulation\n\nBuilds on...\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import cbadc\nimport cbadc.datasets.hadamard\nimport scipy.signal\nimport numpy as np\nimport matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create a Simulation Wrapper\n\nWe load the PCB A prototype by instantiating\nthe wrapper class as\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "simulation_wrapper = cbadc.datasets.hadamard.HadamardPCB('B')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Load a specific simulation\n\nIn this case we load\n:py:func:`cbadc.datasets.hadamard.HadamardPCB.simulation_ramp_1_B`\nsimulation by invoking\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "control_signal, ideal_control_signal, simulator, size = simulation_wrapper.simulation_ramp_1_B()\n\nsize = 1 << 12"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Configure a Digital Estimator\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "eta2 = 1e5\nL1 = 1 << 10\nL2 = 1 << 10\nOSR = 1 << 5\n\n\ndigital_estimator = cbadc.digital_estimator.FIRFilter(\n    simulator.analog_system,\n    simulator.digital_control,\n    eta2,\n    L1,\n    L2,\n    downsample=OSR)\n\nprint(digital_estimator, \"\\n\")\n\ndigital_estimator(control_signal)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Post filtering with FIR\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "numtaps = 1 << 10\nf_cutoff = 1.0 / OSR\nfir_filter = scipy.signal.firwin(numtaps, f_cutoff)\n\ndigital_estimator.convolve((fir_filter))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Filtering Estimate\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "u_hat = np.zeros(size // OSR)\nfor index in cbadc.utilities.show_status(range(size // OSR)):\n    u_hat[index] = next(digital_estimator)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Visualize Estimate\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "t = np.arange(size // OSR) * OSR\nplt.plot(t, u_hat, label=\"$\\hat{u}(t)$\")\nplt.xlabel('$t / T$')\nplt.legend()\nplt.title(\"Estimated input signal\")\nplt.grid(which='both')\n# offset = (L1 + L2) * 4\n# plt.xlim((offset, offset + 1000))\nplt.ylim((-0.6, 0.6))\nplt.tight_layout()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Visualize Estimate Spectrum\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure()\nu_hat_clipped = u_hat[(L1 + L2) // OSR:]\nfreq, psd = cbadc.utilities.compute_power_spectral_density(\n    u_hat_clipped, fs=1.0/(simulator.digital_control.T * OSR))\nplt.semilogx(freq, 10 * np.log10(psd), label=\"$\\hat{U}(f)$\")\nplt.legend()\nplt.ylim((-300, 50))\n# plt.xlim((f_ref[1], f_ref[-1]))\nplt.xlabel('$f$ [Hz]')\nplt.ylabel('$ \\mathrm{V}^2 \\, / \\, (1 \\mathrm{Hz})$')\nplt.grid(which='both')\nplt.show()"
      ]
    }
  ],
  "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.8.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}