Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"unlimited.h"
- void decode(DoubleArrayPtr l_dmemblock, int l_numberofvalues)
- {
- int i, j, k = 0;
- int totalnumberofblocks = l_numberofvalues / BUFFER_SIZE;
- cout << "Total number of blocks is: " << totalnumberofblocks << endl;
- // I cannot put in the variable totalnumberofblocks because the declaration
- // of sum will require a constant value -
- // - totalnumberofblocks could easily be updated and this is not allowed!
- // How can you resolve this, bearing in mind that you will bring in a number
- // of values and automatically get the number of blocks according to BUFFER_SIZE?
- // I had to put in 137 after I knew that a buffer size of 80 would return 137 blocks!
- double sum[912] = { 0.0, 0.0, 0.0, 0.0 };
- string resultatenbinaire = "";
- string resultatenmorse = "";
- string resultatenlettres = "";
- int g = 0;
- for (j = 0; j < totalnumberofblocks; j++)
- {
- for (i = k; i < (k + BUFFER_SIZE); i++)
- {
- sum[j] = sum[j] + abs(l_dmemblock[i]);
- }
- sum[j] = (sum[j] / BUFFER_SIZE);
- //cout << "The mean of the absolute value of data in block " << (j + 1)
- // << " is: " << sum[j] << endl;
- 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
- {
- resultatenbinaire = resultatenbinaire + "1"; // in order to detect if there is a sound emitted, that will be translated into ones
- }
- else if (j%3 == 0)
- {
- resultatenbinaire = resultatenbinaire + "0"; // if no sound is emittd, then a 0 is written
- }
- k = k + BUFFER_SIZE;
- }
- //resultatenbinaire = "11101000101000111010001011101110111000101110000000111010100010000000111010111000111011101110001110100011101110111000101010100010111";
- // that was just to test
- resultatenmorse = resultatenbinaire;
- for (g = 0; g < resultatenmorse.size();g++) { //This loop finds corresponding morse characters to binary digits sequences
- if (resultatenmorse.substr(g, 3) == "111") {
- resultatenmorse.replace(resultatenmorse.find("111"),3, "-" );
- }
- else if(resultatenmorse.substr(g, 1) == "1"){
- resultatenmorse.replace(resultatenmorse.find("1"), 1, ".");
- }
- else if (resultatenmorse.substr(g, 7) == "0000000") {
- resultatenmorse.replace(resultatenmorse.find("0000000"), 7, " / ");
- }
- else if (resultatenmorse.substr(g, 3) == "000") {
- resultatenmorse.replace(resultatenmorse.find("000"), 3, " ");
- }
- }
- for (g = 0; g < resultatenmorse.size();g++) { // this part removes all the left 0
- if (resultatenmorse.substr(g, 1) == "0") {//if it was in the same for loop than the other recognitions, these 0 would have been cancelled
- resultatenmorse.replace(resultatenmorse.find("0"), 1, "");//and there would have been errors because nothing separated the 1 anymore
- }
- }
- resultatenlettres = resultatenmorse;
- for (g = 0; g < resultatenlettres.size(); g++) {// the aim of this part of the program is to find the initial sentence
- if (resultatenlettres.substr(g, 5) == "...--") {//we start with longer substring to avoid the program reading a
- resultatenlettres.replace(resultatenlettres.find("...--"), 5, "3");//5 long substring as a 2 and a 3 long
- }
- else if (resultatenlettres.substr(g, 5) == ".----") {
- resultatenlettres.replace(resultatenlettres.find(".----"), 5, "1");
- }
- else if (resultatenlettres.substr(g, 4) == ".-..") {
- resultatenlettres.replace(resultatenlettres.find(".-.."), 4, "L");
- }
- else if (resultatenlettres.substr(g, 4) == "-..-") {
- resultatenlettres.replace(resultatenlettres.find("-..-"), 4, "X");
- }
- else if (resultatenlettres.substr(g, 4) == "....") {
- resultatenlettres.replace(resultatenlettres.find("...."), 4, "H");
- }
- else if (resultatenlettres.substr(g, 4) == "-...") {
- resultatenlettres.replace(resultatenlettres.find("-..."), 4, "B");
- }
- else if (resultatenlettres.substr(g, 4) == "-.--") {
- resultatenlettres.replace(resultatenlettres.find("-.--"), 4, "Y");
- }
- else if (resultatenlettres.substr(g, 4) == ".---") {
- resultatenlettres.replace(resultatenlettres.find(".---"), 4, "J");
- }
- else if (resultatenlettres.substr(g, 4) == "-.-.") {
- resultatenlettres.replace(resultatenlettres.find("-.-."), 4, "C");
- }
- else if (resultatenlettres.substr(g, 3) == "..-") {
- resultatenlettres.replace(resultatenlettres.find("..-"), 3, "U");
- }
- else if (resultatenlettres.substr(g, 3) == "---") {
- resultatenlettres.replace(resultatenlettres.find("---"), 3, "O");
- }
- else if (resultatenlettres.substr(g, 3) == ".-.") {
- resultatenlettres.replace(resultatenlettres.find(".-."), 3, "R");
- }
- else if (resultatenlettres.substr(g, 3) == ".--") {
- resultatenlettres.replace(resultatenlettres.find(".--"), 3, "W");
- }
- else if (resultatenlettres.substr(g, 3) == "...") {
- resultatenlettres.replace(resultatenlettres.find("..."), 3, "S");
- }
- else if (resultatenlettres.substr(g, 2) == "..") {
- resultatenlettres.replace(resultatenlettres.find(".."), 2, "I");
- }
- else if (resultatenlettres.substr(g, 2) == "-.") {
- resultatenlettres.replace(resultatenlettres.find("-."), 2, "N");
- }
- else if (resultatenlettres.substr(g, 1) == ".") {
- resultatenlettres.replace(resultatenlettres.find("."), 1, "E");
- }
- else if (resultatenlettres.substr(g, 1) == "-") {
- resultatenlettres.replace(resultatenlettres.find("-"), 1, "T");
- }
- }
- 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
- if (resultatenlettres.substr(g, 1) == " ") {//because the spacing would be incorrect and lead to a wrong transcription of the morse signal
- resultatenlettres.replace(resultatenlettres.find(" "), 1, "");
- }
- }
- for (g = 0; g < resultatenlettres.size(); g++) {//Here again, if the / were replaced by spaces in the previous loop, they would
- if (resultatenlettres.substr(g, 1) == "/") {// also have been deleted, and that would result in a sentence with no spaces at all
- resultatenlettres.replace(resultatenlettres.find("/"), 1, " ");
- //int ber = 6;
- }
- }
- //the 3 following lines display the final results
- cout << "\n" << "The binary string that will be used for decoding is : \n" << resultatenbinaire << "\n";
- cout << "\n" << "The morse transcription of this signal is : \n" << resultatenmorse << "\n";
- cout << "\n" << "The text equivalent to this morse sequence is : \n" << resultatenlettres << "\n";
- // What you'll get from the code block above is a kind of RMS value for the data-
- // an "average".
- // Ideally, square each of the values, take the mean, and raise take the square root
- // (raise it to the power of 1/2) is the correct way of finding an RMS value.
- //
- // But, now that you have a value for each block giving something much greater than nought
- // or close to it, you should be able to allocate whether that block represents a
- // tone ON or OFF: chose a threshold, say 0.5, to give you an ON or OFF.
- // if(sum[j] >= 0.5), then you can state ON as a string O/P. Else, it's OFF
- //
- // Meanwhile, does a BUFFER_SIZE of 80 give too much resolution?
- // Can you increase this number and still identify the dit, dah,
- // inter-element space, inter-character space, and the inter-word space?
- } // End of decode()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement