{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47ca90c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "%config InlineBackend.figure_format = 'svg'  # don't scale plots\n",
    "\n",
    "from importlib import reload\n",
    "import plot\n",
    "reload(plot)\n",
    "import ubitstr\n",
    "reload(ubitstr)\n",
    "import defs\n",
    "reload(defs)\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import math\n",
    "PI = math.pi"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d869a04",
   "metadata": {},
   "source": [
    "# Leitungskodierung\n",
    "\n",
    "## Grundimpulse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d01da95",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(1, len(defs.IMPULSES))\n",
    "for ax, impulse in zip(axes, defs.IMPULSES):\n",
    "    plot.impulse(impulse, ax=ax)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4342733",
   "metadata": {},
   "source": [
    "## Leitungscodes mit Bitfolge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f821ef28",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"This was a triumph\\0\"\n",
    "bitstr = ubitstr.from_ascii(text)\n",
    "print(f\"Text          {text}\")\n",
    "print(f\"ASCII encoded {bitstr}\")\n",
    "\n",
    "defs.visualize_ascii(text)\n",
    "\n",
    "linecode_signals = {}\n",
    "for linecode in defs.LINECODES:\n",
    "    print(linecode)\n",
    "    linecode_signals[linecode] = plot.linecode(bitstr, linecode)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9dd5b45",
   "metadata": {},
   "source": [
    "## Invertierte Signaldefinition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "301d6c45",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Manchester Regular\")\n",
    "plot.linecode(bitstr, \"Manchester\")\n",
    "print(\"Manchester Inverted\")\n",
    "plot.linecode(bitstr, \"Manchester\", invert=True)\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a196fde",
   "metadata": {},
   "source": [
    "## Vergleich Spektren"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44417c6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "freq_filter = lambda f: defs.lowpass(f, 2)\n",
    "\n",
    "\n",
    "linecode_signals[\"NRZ\"] = plot.linecode(bitstr, \"NRZ\")\n",
    "plot.spectrum(linecode_signals[\"NRZ\"])\n",
    "\n",
    "for name, baseband_signal in linecode_signals.items():\n",
    "    print(name)\n",
    "    plot.spectrum(baseband_signal, freq_filter=freq_filter)\n",
    "    plot.filtered(baseband_signal, freq_filter, xscale=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a91c1d6",
   "metadata": {},
   "source": [
    "## Taktrückgewinnung\n",
    "\n",
    "Schematisches beispiel für clockdrift und die Notwendigkeit von Taktsynchronisierung.\n",
    "Empfängerclock ist leicht schneller, wertet also in der selben Zeit mehr Symbole aus, als der Sender sendet."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0af6220b",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "BITSTR = \"00001010 00000010\"\n",
    "LINECODE = \"Manchester\"\n",
    "DRIFT = 0.1\n",
    "SAMPLING_OFFSET=0.7\n",
    "\n",
    "sampled = plot.linecode(BITSTR, LINECODE)\n",
    "sampled = plot.linecode_drift(BITSTR, LINECODE, drift = DRIFT)\n",
    "sampled = plot.linecode_drift(BITSTR, LINECODE, drift = DRIFT, sampling=True, sampling_offset=SAMPLING_OFFSET)\n",
    "received_bitstr = ubitstr.to_bitstr([s > 0 for s in sampled], n=8)\n",
    "\n",
    "print(f\"Sent:     {BITSTR}\")\n",
    "print(f\"Received: {received_bitstr}\")\n",
    "\n",
    "\n",
    "sampled = plot.linecode_drift(BITSTR, LINECODE, drift = DRIFT, sampling=True, sampling_offset=SAMPLING_OFFSET, repair_clock=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv (3.13.5)",
   "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
