Advertisement
Elisee_THIBAUT

decode.cpp for task 2

Apr 10th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.27 KB | None | 0 0
  1. #include"unlimited.h"
  2.  
  3. void decode(DoubleArrayPtr l_dmemblock, int l_numberofvalues)
  4. {
  5.    
  6.     int i, j, k = 0;
  7.     int totalnumberofblocks = l_numberofvalues / BUFFER_SIZE;
  8.     cout << "Total number of blocks is: " << totalnumberofblocks << endl;
  9.     // I cannot put in the variable totalnumberofblocks because the declaration
  10.     // of sum will require a constant value -
  11.     // - totalnumberofblocks could easily be updated and this is not allowed!
  12.     // How can you resolve this, bearing in mind that you will bring in a number
  13.     // of values and automatically get the number of blocks according to BUFFER_SIZE?
  14.     // I had to put in 137 after I knew that a buffer size of 80 would return 137 blocks!
  15.     double sum[912] = { 0.0, 0.0, 0.0, 0.0 };
  16.  
  17.     string resultatenbinaire = "";
  18.     string resultatenmorse = "";
  19.     string resultatenlettres = "";
  20.     int g = 0;
  21.  
  22.     for (j = 0; j < totalnumberofblocks; j++)
  23.     {
  24.         for (i = k; i < (k + BUFFER_SIZE); i++)
  25.         {
  26.             sum[j] = sum[j] + abs(l_dmemblock[i]);
  27.         }
  28.         sum[j] = (sum[j] / BUFFER_SIZE);
  29.         //cout << "The mean of the absolute value of data in block " << (j + 1)
  30.         //    << " is: " << sum[j] << endl;
  31.  
  32.  
  33.         if (sum[j] >= 0.5 and j%3 == 0) //because every "lenght of information" is a multiple of 3 as said in the assignment brief
  34.         {
  35.             resultatenbinaire = resultatenbinaire + "1"; // in order to detect if there is a sound emitted,  that will be translated into ones
  36.         }
  37.         else if (j%3 == 0)
  38.         {
  39.             resultatenbinaire = resultatenbinaire + "0"; // if no sound is emittd, then a 0 is written
  40.         }
  41.         k = k + BUFFER_SIZE;
  42.  
  43.     }
  44.     //resultatenbinaire = "11101000101000111010001011101110111000101110000000111010100010000000111010111000111011101110001110100011101110111000101010100010111";
  45.     // that was just to test
  46.  
  47.     resultatenmorse = resultatenbinaire;
  48.    
  49.     for (g = 0; g < resultatenmorse.size();g++) { //This loop finds corresponding morse characters to binary digits sequences
  50.         if (resultatenmorse.substr(g, 3) == "111") {
  51.             resultatenmorse.replace(resultatenmorse.find("111"),3, "-" );
  52.         }
  53.         else if(resultatenmorse.substr(g, 1) == "1"){
  54.             resultatenmorse.replace(resultatenmorse.find("1"), 1, ".");
  55.         }
  56.         else if (resultatenmorse.substr(g, 7) == "0000000") {
  57.             resultatenmorse.replace(resultatenmorse.find("0000000"), 7, " / ");
  58.         }
  59.         else if (resultatenmorse.substr(g, 3) == "000") {
  60.             resultatenmorse.replace(resultatenmorse.find("000"), 3, " ");
  61.         }
  62.     }
  63.  
  64.     for (g = 0; g < resultatenmorse.size();g++) { // this part removes all the left 0
  65.         if (resultatenmorse.substr(g, 1) == "0") {//if it was in the same for loop than the other recognitions, these 0 would have been cancelled
  66.             resultatenmorse.replace(resultatenmorse.find("0"), 1, "");//and there would have been errors because nothing separated the 1 anymore
  67.         }
  68.     }
  69.  
  70.     resultatenlettres = resultatenmorse;
  71.         for (g = 0; g < resultatenlettres.size(); g++) {// the aim of this part of the program is to find the initial sentence
  72.             if (resultatenlettres.substr(g, 5) == "...--") {//we start with longer substring to avoid the program reading a
  73.                 resultatenlettres.replace(resultatenlettres.find("...--"), 5, "3");//5 long substring as a 2 and a 3 long
  74.             }
  75.             else if (resultatenlettres.substr(g, 5) == ".----") {
  76.                 resultatenlettres.replace(resultatenlettres.find(".----"), 5, "1");
  77.             }
  78.             else if (resultatenlettres.substr(g, 4) == ".-..") {
  79.                 resultatenlettres.replace(resultatenlettres.find(".-.."), 4, "L");
  80.             }
  81.             else if (resultatenlettres.substr(g, 4) == "-..-") {
  82.                 resultatenlettres.replace(resultatenlettres.find("-..-"), 4, "X");
  83.             }
  84.             else if (resultatenlettres.substr(g, 4) == "....") {
  85.                 resultatenlettres.replace(resultatenlettres.find("...."), 4, "H");
  86.             }
  87.             else if (resultatenlettres.substr(g, 4) == "-...") {
  88.                 resultatenlettres.replace(resultatenlettres.find("-..."), 4, "B");
  89.             }
  90.             else if (resultatenlettres.substr(g, 4) == "-.--") {
  91.                 resultatenlettres.replace(resultatenlettres.find("-.--"), 4, "Y");
  92.             }
  93.             else if (resultatenlettres.substr(g, 4) == ".---") {
  94.                 resultatenlettres.replace(resultatenlettres.find(".---"), 4, "J");
  95.             }
  96.             else if (resultatenlettres.substr(g, 4) == "-.-.") {
  97.                 resultatenlettres.replace(resultatenlettres.find("-.-."), 4, "C");
  98.             }
  99.             else if (resultatenlettres.substr(g, 3) == "..-") {
  100.                 resultatenlettres.replace(resultatenlettres.find("..-"), 3, "U");
  101.             }
  102.             else if (resultatenlettres.substr(g, 3) == "---") {
  103.                 resultatenlettres.replace(resultatenlettres.find("---"), 3, "O");
  104.             }
  105.             else if (resultatenlettres.substr(g, 3) == ".-.") {
  106.                 resultatenlettres.replace(resultatenlettres.find(".-."), 3, "R");
  107.             }
  108.             else if (resultatenlettres.substr(g, 3) == ".--") {
  109.                 resultatenlettres.replace(resultatenlettres.find(".--"), 3, "W");
  110.             }
  111.             else if (resultatenlettres.substr(g, 3) == "...") {
  112.                 resultatenlettres.replace(resultatenlettres.find("..."), 3, "S");
  113.             }
  114.             else if (resultatenlettres.substr(g, 2) == "..") {
  115.                 resultatenlettres.replace(resultatenlettres.find(".."), 2, "I");
  116.             }
  117.             else if (resultatenlettres.substr(g, 2) == "-.") {
  118.                 resultatenlettres.replace(resultatenlettres.find("-."), 2, "N");
  119.             }
  120.             else if (resultatenlettres.substr(g, 1) == ".") {
  121.                 resultatenlettres.replace(resultatenlettres.find("."), 1, "E");
  122.             }
  123.             else if (resultatenlettres.substr(g, 1) == "-") {
  124.                 resultatenlettres.replace(resultatenlettres.find("-"), 1, "T");
  125.             }
  126.         }
  127.  
  128.         for (g = 0; g < resultatenlettres.size(); g++) { //as for the 0 in line 64, if this part was included in the main transcription loop, errors would have been created
  129.             if (resultatenlettres.substr(g, 1) == " ") {//because the spacing would be incorrect and lead to a wrong transcription of the morse signal
  130.                 resultatenlettres.replace(resultatenlettres.find(" "), 1, "");
  131.             }
  132.         }
  133.  
  134.         for (g = 0; g < resultatenlettres.size(); g++) {//Here again, if the / were replaced by spaces in the previous loop, they would
  135.             if (resultatenlettres.substr(g, 1) == "/") {// also have been deleted, and that would result in a sentence with no spaces at all
  136.                 resultatenlettres.replace(resultatenlettres.find("/"), 1, " ");
  137.                 //int ber = 6;
  138.             }
  139.         }
  140.  
  141.     //the 3 following lines display the final results
  142.     cout << "\n" << "The binary string that will be used for decoding is : \n" << resultatenbinaire << "\n";
  143.     cout << "\n" << "The morse transcription of this signal is : \n" << resultatenmorse << "\n";
  144.     cout << "\n"  << "The text equivalent to this morse sequence is : \n" << resultatenlettres << "\n";
  145.  
  146.     // What you'll get from the code block above is a kind of RMS value for the data-
  147.     // an "average".
  148.  
  149.  
  150.     // Ideally, square each of the values, take the mean, and raise take the square root
  151.     // (raise it to the power of 1/2) is the correct way of finding an RMS value.
  152.     //
  153.     // But, now that you have a value for each block giving something much greater than nought
  154.     // or close to it, you should be able to allocate whether that block represents a
  155.     // tone ON or OFF: chose a threshold, say 0.5, to give you an ON or OFF.
  156.     // if(sum[j] >= 0.5), then you can state ON as a string O/P.  Else, it's OFF
  157.     //
  158.     // Meanwhile, does a BUFFER_SIZE of 80 give too much resolution?
  159.     // Can you increase this number and still identify the dit, dah,
  160.     // inter-element space, inter-character space, and the inter-word space?
  161.    
  162. } // End of decode()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement