Skip to content
Extraits de code Groupes Projets
LoRa_encoding.ipynb 10,2 ko
Newer Older
  • Learn to ignore specific revisions
  • Pol Maistriaux's avatar
    Pol Maistriaux a validé
    {
     "cells": [
      {
       "cell_type": "code",
    
       "execution_count": 10,
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       "id": "3202ec29",
       "metadata": {},
       "outputs": [],
       "source": [
        "\n",
        "\n",
        "import os\n",
        "import sys\n",
        "import numpy as np\n",
        "import matplotlib\n",
        "import scipy.io\n",
        "import matplotlib.pyplot as plt\n",
        "from matplotlib import ticker, cm\n",
        "from matplotlib.colors import LogNorm\n",
        "from matplotlib.ticker import (AutoMinorLocator, MultipleLocator)\n",
        "import importlib\n",
        "import random\n",
        "from scipy import signal\n",
        "from scipy.fft import fft, fftfreq, fftshift\n",
        "from scipy.stats import chi2, ncx2, bernoulli, binom, ncf, norm\n",
        "from scipy.signal import firwin, firwin2\n",
        "from scipy import special\n",
        "\n",
        "\n",
    
        "from lora_coding import *"
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 11,
       "id": "3cccfbaa",
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
         ]
        }
       ],
       "source": [
    
        "text = \"Mais vous savez moi je ne crois pas qu il y ait de bonne ou de ma\"\n",
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
        "\n",
    
        "length_text = 65\n",
        "input_data = np.array([ord(character) for character in text[:length_text]], dtype=np.int8)\n",
        "print(len(input_data))\n",
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
        "\n",
        "CR      = 1\n",
        "CRC_EN  = 1\n",
        "SF      = 7\n",
        "\n",
        "LDRO    = 0\n"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 12,
       "id": "65db24cb",
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
    
          "Number of symbols in payload :108 \n",
          "Number of symbols in packets :122 \n",
          "[ 45  49  33  13 109 101  25  73  85  67   7  74  24  41  57  14 108  77\n",
          "  45  23 101  80  66  70   6  73  59   5  26  84   4  38  23  28  99  62\n",
          "  32  78 126  18  40  62  99 116  84  41  32  88  79  71  84   0  73  41\n",
          " 110  91  71 102  86  29   0  10  14  53  94  26  39  35  96  99  71  93\n",
          "  42   2  64  73  20  20 121  38  73  64 111  37 126  70   2  36  87   7\n",
          " 115  82  34  53  70  87 121  30  52  94  67  49   1   2   0  64  32   1]\n",
          "45, 49, 33, 13, 109, 101, 25, 73, 85, 67, 7, 74, 24, 41, 57, 14, 108, 77, 45, 23, 101, 80, 66, 70, 6, 73, 59, 5, 26, 84, 4, 38, 23, 28, 99, 62, 32, 78, 126, 18, 40, 62, 99, 116, 84, 41, 32, 88, 79, 71, 84, 0, 73, 41, 110, 91, 71, 102, 86, 29, 0, 10, 14, 53, 94, 26, 39, 35, 96, 99, 71, 93, 42, 2, 64, 73, 20, 20, 121, 38, 73, 64, 111, 37, 126, 70, 2, 36, 87, 7, 115, 82, 34, 53, 70, 87, 121, 30, 52, 94, 67, 49, 1, 2, 0, 64, 32, 1, \n",
          "\n"
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
         ]
        }
       ],
       "source": [
    
        "symbols = payload_to_symbol(input_data, SF, CR, CRC_EN, LDRO)\n",
        "print(\"Number of symbols in payload :%d \"%(len(symbols)))\n",
        "print(\"Number of symbols in packets :%d \"%(len(symbols)+10+4.5))\n",
        "\n",
        "print(symbols)\n",
        "text= \"\"\n",
        "for i, sym in enumerate(symbols):\n",
        "    text += str(sym)+\", \"\n",
        "print(text)\n",
        "print()"
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 49,
       "id": "3a370f3b",
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       "metadata": {},
    
       "outputs": [],
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       "source": [
        "\n",
    
        "def symbols_to_payload(symbols,SF,LDRO, verbose = False):\n",
        "    ldro_hdr = 1 if SF>6 else 0\n",
        "    degrayed_symbols = gray_map(symbols,SF)\n",
        "    if verbose:\n",
        "        print(\"Degrayed symbols :\")\n",
        "        print(\"------------------------------\")\n",
        "        print(degrayed_symbols)\n",
        "        print(\"---------------------------------\")\n",
        "\n",
        "    first_block  = deinterleave(degrayed_symbols[:8],SF,4,ldro_hdr)\n",
        "    firstBlen = len(first_block)\n",
        "    decoded_hdr = hamming_decode(first_block,4)\n",
        "\n",
        "    dec_hdr_len = (decoded_hdr[0]<<4)|(decoded_hdr[1])\n",
        "    dec_crc_en  = (decoded_hdr[2] & 0x01)\n",
        "    dec_hdr_cr  = (decoded_hdr[2] >>1) & 0x03\n",
        "    dec_hdr_crc =((decoded_hdr[3] & 0x01) << 4) | decoded_hdr[4]\n",
        "    hdrCRC3,hdrCRC4  = hdr_crc(decoded_hdr[0:3])\n",
        "    rec_hdr_cr = (hdrCRC3<< 4) | hdrCRC4\n",
        "    \n",
        "    \n",
        "    n_received_blocks = int((len(degrayed_symbols)-8)/(4+dec_hdr_cr))\n",
        "\n",
        "    print(\"Nblocks :\")\n",
        "    print(\"%d\"%(n_received_blocks))\n",
        "    print(\"%.2f\"%((dec_hdr_len+2)*2*(4+dec_hdr_cr)/(SF*(4+dec_hdr_cr))))\n",
        "\n",
        "    n_received_blocks = int(np.ceil((dec_hdr_len+2)*2/(SF)))\n",
        "\n",
        "    other_blocks = np.zeros((n_received_blocks*(SF- 2*LDRO)),dtype=np.int16)\n",
        "\n",
        "    sf = SF - 2*LDRO\n",
        "    if verbose:\n",
        "        print(\"Deinterleaved symbols\")\n",
        "        print(\"----------1st block ----------\")\n",
        "        print(first_block)\n",
        "        print(\"----------Other block ----------\")\n",
        "    for i in range(n_received_blocks):\n",
        "        other_blocks[i*sf : (i+1)*sf] = deinterleave(degrayed_symbols[8+i*(4+dec_hdr_cr) : 8+(i+1)*(4+dec_hdr_cr)],SF,dec_hdr_cr,LDRO )\n",
        "        if verbose:\n",
        "            print(other_blocks[i*sf : (i+1)*sf])\n",
        "    \n",
        "    decoded_pay        = np.concatenate((decoded_hdr[5:],hamming_decode(other_blocks,dec_hdr_cr)))\n",
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
        "\n",
    
        "    if verbose:\n",
        "        print(\"Decoded payload\")\n",
        "        print(\"------------------------------\")\n",
        "        print(decoded_pay)\n",
        "        print(\"------------------------------\")\n",
        "    \n",
        "    dewhitened_payload = dewhitening_lfsr(decoded_pay)\n",
        "    \n",
        "    if verbose:\n",
        "        print(\"Dewhitened payload\")\n",
        "        print(\"------------------------------\")\n",
        "        print(dewhitened_payload)\n",
        "        print(\"------------------------------\")\n",
        "\n",
        "    print(\"Payload len : %d  \"%(dec_hdr_len))\n",
        "    print(\"Code rate :   4/%d\"%(4+dec_hdr_cr))\n",
        "    print(\"Header CRC :  %d vs %d\"%(dec_hdr_crc,rec_hdr_cr))\n",
        "    if dec_crc_en : \n",
        "        idx = dec_hdr_len*2\n",
        "        rec_data_crc = compute_crc(dewhitened_payload[:dec_hdr_len])\n",
        "        print(\"CRC is enabled :     %s  vs %s\"%(decoded_pay[idx:idx+4],rec_data_crc))\n",
        "\n",
        "    print(''.join(chr(d) for d in dewhitened_payload[:dec_hdr_len]))\n",
        "\n",
        "#################################################################################"
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 50,
       "id": "18ef971b",
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
    
          "[ 6  5  8 14]\n"
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
         ]
        }
       ],
       "source": [
        "\n",
    
        "print(compute_crc([ 38, 185,114, 228]))"
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 51,
       "id": "062758db",
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
    
          "Degrayed symbols :\n",
          "------------------------------\n",
          "[ 58  40  48  10  90  86  20 108 126  99   5 109  28  60  36  11  94 106\n",
          "  58  29  86 104  97 103   7 108  39   6  21 122   2  55  29  22  83  35\n",
          "  16 107  67  25  52  35  83  74 122  60  16 124 105 101 122  64 108  60\n",
          "  91 119 101  87 127  18  64  13  11  46 115  21  53  51 112  83 101 114\n",
          "  61   1  32 108  26  26  68  55 108  32  89  54  67 103   1  50 125   5\n",
          "  75 121  49  46 103 125  68  19  42 115  99  40   0   1  64  32  16   0\n",
          "  64  64  64  64  64  64  64  64  64  64]\n",
          "---------------------------------\n",
          "Nblocks :\n",
          "22\n",
          "19.14\n",
          "Deinterleaved symbols\n",
          "----------1st block ----------\n",
          "[ 45 139 197 139 116]\n",
          "----------Other block ----------\n",
          "[ 9 27 30 18 20 18 27]\n",
          "[ 3  0 23 29 18 23 10]\n",
          "[ 0 30  3 29 29 24  6]\n",
          "[20 30 24 10  6 23 17]\n",
          "[27  3 24  6 27 10  9]\n",
          "[15 24 29  5 17  9  0]\n",
          "[20 27  0  3 15  9 20]\n",
          "[15 17  9 17 12 12 29]\n",
          "[29 12  3 29  0 20 29]\n",
          "[12 15 30 23 29 27  5]\n",
          "[17 20 24 18 17 27 18]\n",
          "[10 27 29 18  5 27 12]\n",
          "[15 15 15  0 23 24 17]\n",
          "[23  5  0  0  6  5 10]\n",
          "[ 5 15 24  9  5 12 24]\n",
          "[12 29 29  6 20 17  0]\n",
          "[18 15 23 30 17 20 18]\n",
          "[12 29 30 24  5 23 12]\n",
          "[12 30 12 20  0 24 10]\n",
          "[30  0  0  0  0  0  0]\n",
          "Decoded payload\n",
          "------------------------------\n",
          "[ 2 11 15  9  5  9 11  8  0 13  7  9 13 10  0 15  8  7  7  3 12  5 15  3\n",
          " 10 12 13  1 11  8  3 12 11 10  2 14  3  7  4  1  2  0  5 11  0  8 14  2\n",
          "  5 14  1  2  1  6  6  7  7  6  8  7  0  5  7  6 14 15 13  7 11  4  1  5\n",
          "  3  9  1 11  9 10 11  7  9  4 11  6 14 14 14  0 13  3  1 13  4  0  0 12\n",
          "  4 10  4 14  3  2  4  6  3  6  7  7 12  5  1  0  9 14 13 15  1  5  9  6\n",
          "  7 15  3  4 13  6  6 15  6  5  0  3 10 15  0  0  0  0  0  0]\n",
          "------------------------------\n",
          "Dewhitened payload\n",
          "------------------------------\n",
          "[ 77  97 105 115  32 118 111 117 115  32 115  97 118 101 122  32 109 111\n",
          " 105  32 106 101  32 110 101  32  99 114 111 105 115  32 112  97 115  32\n",
          " 113 117  32 105 108  32 121  32  97 105 116  32 100 101  32  98 111 110\n",
          " 110 101  32 111 117  32 100 101  32 109  97  94  38 185 114 228]\n",
          "------------------------------\n",
          "Payload len : 65  \n",
          "Code rate :   4/5\n",
          "Header CRC :  30 vs 30\n",
          "CRC is enabled :     [ 0  3 10 15]  vs [ 0  3 10 15]\n",
          "Mais vous savez moi je ne crois pas qu il y ait de bonne ou de ma\n"
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
         ]
        }
       ],
       "source": [
        "\n",
    
        "new_symbols = np.zeros((len(symbols)+10), dtype =int)\n",
        "new_symbols[:len(symbols)] = symbols\n",
        "symbols_to_payload(new_symbols,SF,LDRO, verbose=True)\n"
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
       ]
      }
     ],
     "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.9.13"
    
    Pol Maistriaux's avatar
    Pol Maistriaux a validé
      }
     },
     "nbformat": 4,
     "nbformat_minor": 5
    }