{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "45d4745d",
   "metadata": {},
   "source": [
    "# Pulse Transmission between Software-Defined Radios (SDRs)\n",
    "\n",
    "In this notebook, we will walk through setup and operation of SDRs to transmit pulse train data for pulsar signal simulation.\n",
    "\n",
    "## Checking SDR Connectivity\n",
    "\n",
    "To start, it is necessary to make sure you have successfully mounted the SDRs to your PC via USB. You can check this by executing the following commands in a command prompt:\n",
    "\n",
    "```\n",
    "# ping 192.168.2.1\n",
    "# ping 192.168.2.2\n",
    "```\n",
    "\n",
    "For each of these commands, you should see four data packets successfully sent to the SDR and received back by the PC.\n",
    "\n",
    "Next, it will be necessary to check that both SDRs can transmit and receive data <i>to themselves</i>. You can do so by executing the following code. Before running this code, make sure both SDRs are mounted to your PC and have been pinged successfully, and that each one has a BNC cable connecting its Rx port to its Tx port."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "6a9ead3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import adi\n",
    "import matplotlib.pyplot as plt\n",
    "import sdr_io\n",
    "\n",
    "sample_rate = 1e6 # Hz\n",
    "center_freq = 915e6 # Hz\n",
    "\n",
    "tx_data = np.load('test_vec.npy')\n",
    "tx_length = 5000\n",
    "\n",
    "plot_rx_pulse_train = True\n",
    "plot_rx_samples = True\n",
    "plot_fft = True\n",
    "check_fid = True\n",
    "\n",
    "sdr1 = \"ip:192.168.2.1\"\n",
    "sdr2 = \"ip:192.168.2.2\"\n",
    "\n",
    "sdr_io.sdr_tx_rx(sample_rate,center_freq,\n",
    "                 sdr1,sdr1,\n",
    "                 tx_data,tx_length,\n",
    "                 False,False, False, False)\n",
    "\n",
    "sdr_io.sdr_tx_rx(sample_rate,center_freq,\n",
    "                 sdr2,sdr2,\n",
    "                 tx_data,tx_length,\n",
    "                 False,False, False, False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c48f4a2",
   "metadata": {},
   "source": [
    "Now that you have verified both SDRs can transmit and receive data to and from your PC, connect the Tx port of the \"X-RAY EMITTER\" SDR to the Rx port of the \"X_RAY DETECTOR\" SDR and execute the following code. This will transmit the same test pulse as above, but between the two SDRs instead of within each one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "23806a18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31088 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 22068 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 18504 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Out of phase by 1141\n",
      "Success! Perfect Transmission!\n"
     ]
    }
   ],
   "source": [
    "sdr_io.sdr_tx_rx(sample_rate,center_freq,\n",
    "                 sdr1,sdr2,\n",
    "                 tx_data,tx_length,\n",
    "                 plot_rx_pulse_train,plot_rx_samples,plot_fft,check_fid)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94d84352",
   "metadata": {},
   "source": [
    "Once you have verified that the SDRs can communicate with one another, you are ready to proceed with data transmission.\n",
    "\n",
    "## Transmitting Pulsar Simulation Data\n",
    "\n",
    "After generating a pulse train from the OpenXNAV software, transmit it from the \"Emitter\" SDR to the \"Detector\" SDR by loading it through the code below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "1e218e8f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Transmitted data\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 6348 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Received data\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 11028 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 7536 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 17300 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Out of phase by 2890\n",
      "Success! Perfect Transmission!\n"
     ]
    }
   ],
   "source": [
    "tx_data_name = \"pulsar_vec.npy\" # insert filename for pulse train here\n",
    "tx_data = np.load(tx_data_name)\n",
    "tx_data = tx_data[:5000]\n",
    "tx_length = len(tx_data)\n",
    "\n",
    "plot_rx_samples = True # once connectivity and fidelic transmission is verified,\n",
    "                        # this is redundant\n",
    "plot_fft = True # no longer concerned with freq domain - only concerned \n",
    "                        # with transmitted pulses\n",
    "check_fid = True # we have already verified connectivity - no need to check again\n",
    "print(\"Transmitted data\")\n",
    "plt.plot(tx_data)\n",
    "plt.show()\n",
    "print(\"Received data\")\n",
    "sdr_io.sdr_tx_rx(sample_rate,center_freq,\n",
    "                 sdr1,sdr2,\n",
    "                 tx_data,tx_length,\n",
    "                 plot_rx_pulse_train,plot_rx_samples,plot_fft,check_fid)"
   ]
  }
 ],
 "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.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
