{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "59938c83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 192260 characters]",
      "text/plain": [
       "<Figure size 1500x1200 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 39256 characters]",
      "text/plain": [
       "<Figure size 1500x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot DSP Output\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "PLOT = False\n",
    "\n",
    "dsp_desired_signal_r11 = np.genfromtxt(\"./simulation_data/complex_dsp_desired_signal_r11.txt\", dtype=int)/(2**(15)-1)\n",
    "dsp_noise_signal_r11 = np.genfromtxt(\"./simulation_data/complex_dsp_noise_signal_r11.txt\", dtype=int)/(2**(15)-1)\n",
    "dsp_noise_signal_vpu = np.genfromtxt(\"./simulation_data/complex_dsp_noise_signal_vpu.txt\", dtype=int)/(2**(15)-1)\n",
    "dsp_corrupted_signal = np.genfromtxt(\"./simulation_data/complex_dsp_corrupted_signal.txt\", dtype=int)/(2**(15)-1)\n",
    "t = np.linspace(0, len(dsp_corrupted_signal), len(dsp_corrupted_signal))/20000\n",
    "\n",
    "output = np.genfromtxt(\"./filter_output/complex_dsp_output.txt\", dtype=int)[:-1]/(2**(15)-1) # letzte Zeile löschen\n",
    "\n",
    "error_signal = (output - dsp_desired_signal_r11)\n",
    "t2 = np.linspace(0, len(error_signal), len(error_signal))/20000\n",
    "\n",
    "# SNR davor/danach in dB berechnen, SNR Ratio berechnen, \n",
    "snr_before = 10 * np.log10(np.trapz(dsp_desired_signal_r11**2, t) / np.trapz(dsp_noise_signal_r11**2, t))\n",
    "snr_after = 10 * np.log10(np.trapz(dsp_desired_signal_r11**2, t) / np.trapz(error_signal**2, t2))\n",
    "delta_snr = round(snr_after - snr_before, 2)\n",
    "\n",
    "# Plots des Filterprozesses\n",
    "figure1, (ax0, ax1, ax2, ax3) = plt.subplots(4, 1, figsize=(15, 12), sharex=True, sharey=True)\n",
    "ax0.set_ylim(-1, 1)\n",
    "ax0.plot(t, dsp_desired_signal_r11, c='deepskyblue', label='Desired signal')\n",
    "ax1.plot(t, dsp_corrupted_signal, c='royalblue', label='Corrupted signal')\n",
    "ax2.plot(t, dsp_noise_signal_r11, c='chocolate', label='Reference noise signal')\n",
    "ax3.plot(t, output, c='green', label=f'SNR Gain = {delta_snr} dB')\n",
    "\n",
    "ax0.text(0.5, -0.3, '(a) Desired signal',\n",
    "         transform=ax0.transAxes,\n",
    "         fontsize=25,\n",
    "         fontweight='normal',\n",
    "         ha='center',\n",
    "         va='bottom')\n",
    "\n",
    "ax1.text(0.5, -0.3, '(b) Corrupted signal',\n",
    "         transform=ax1.transAxes,\n",
    "         fontsize=25,\n",
    "         fontweight='normal',\n",
    "         ha='center',\n",
    "         va='bottom')\n",
    "\n",
    "ax2.text(0.5, -0.3, '(c) Reference noise signal',\n",
    "         transform=ax2.transAxes,\n",
    "         fontsize=25,\n",
    "         fontweight='normal',\n",
    "         ha='center',\n",
    "         va='bottom')\n",
    "\n",
    "ax3.text(0.5, -0.5, f'(d) DSP Filter output (SNR Gain = {delta_snr} dB)',\n",
    "         transform=ax3.transAxes,\n",
    "         fontsize=25,\n",
    "         fontweight='normal',\n",
    "         ha='center',\n",
    "         va='bottom')\n",
    "\n",
    "ax3.set_xlabel('time(s)', x=0.05)\n",
    "ax0.set_ylabel('Amplitude')\n",
    "ax1.set_ylabel('Amplitude')\n",
    "ax2.set_ylabel('Amplitude')\n",
    "ax3.set_ylabel('Amplitude')\n",
    "\n",
    "# Plots der Filterperfomanz\n",
    "figure2, (ax4) = plt.subplots(1, 1, figsize=(15, 4), sharex=True)\n",
    "ax4.set_ylim(-1, 1)\n",
    "ax4.plot(t2, error_signal, c='purple', label='Error (Desired signal - Filter output)')\n",
    "\n",
    "ax4.text(0.5, -0.30, 'Error signal',\n",
    "         transform=ax4.transAxes,\n",
    "         fontsize=25,\n",
    "         fontweight='normal',\n",
    "         ha='center',\n",
    "         va='bottom')\n",
    "\n",
    "ax4.set_xlabel('time(s)', x=0.05)\n",
    "ax4.set_ylabel('Amplitude')\n",
    "\n",
    "#Grids direkt auf Subplots anwenden\n",
    "ax0.grid(True, linestyle='--', alpha=0.4)\n",
    "ax1.grid(True, linestyle='--', alpha=0.4)\n",
    "ax2.grid(True, linestyle='--', alpha=0.4)\n",
    "ax3.grid(True, linestyle='--', alpha=0.4)\n",
    "ax4.grid(True, linestyle='--', alpha=0.4)\n",
    "\n",
    "#Spines direkt auf Subplots anwenden\n",
    "ax0.spines['top'].set_visible(False)\n",
    "ax1.spines['top'].set_visible(False)\n",
    "ax2.spines['top'].set_visible(False)\n",
    "ax3.spines['top'].set_visible(False)\n",
    "ax4.spines['top'].set_visible(False)\n",
    "ax0.spines['right'].set_visible(False)\n",
    "ax1.spines['right'].set_visible(False)\n",
    "ax2.spines['right'].set_visible(False)\n",
    "ax3.spines['right'].set_visible(False)\n",
    "ax4.spines['right'].set_visible(False)\n",
    "\n",
    "# Schriftgrößen für LaTeX-Dokument\n",
    "plt.rcParams.update({\n",
    "    \"text.usetex\": True,\n",
    "    \"font.family\": \"serif\",\n",
    "    'font.size': 16,          # Standardtext\n",
    "    'axes.labelsize': 30,     # Achsenbeschriftungen\n",
    "    'xtick.labelsize': 25,    # Tick-Beschriftungen\n",
    "    'ytick.labelsize': 25,\n",
    "    'legend.fontsize': 15     # Legende\n",
    "})\n",
    "\n",
    "figure1.tight_layout()\n",
    "figure2.tight_layout()\n",
    "\n",
    "if PLOT == True:\n",
    "    figure1.savefig(f'plots/fig_plot_1_dsp_complex', dpi=600)\n",
    "    figure2.savefig(f'plots/fig_plot_2_dsp_complex', dpi=600)\n",
    "\n",
    "plt.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.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
