CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2018
    Posts
    40

    Important question

    hi
    Last edited by Kmilano; April 23rd, 2018 at 08:43 AM.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Important question

    What I gather from your code is that syndrome bits make the index in decodedBits minus 1.
    Code:
    0 0 1 = 1, 1 -1 = 0
    0 1 0 = 2, 2 -1 = 1
    0 1 1 = 3, 3 -1 = 2
    etc.
    Best regards,
    Igor

  3. #3
    Join Date
    Mar 2018
    Posts
    40

    Re: Important question

    for example if a vector decodedBits=[1 1 0 1 0 1 1]
    then we will have : syndrome= [1 1 0 1 0 1 1]* check matrix(which I indicated above in my first post)
    then in the part where if else began, I'm evaluating that if the result of syndrome is:
    [0 0 1] it means syndrome[0]=0, syndrome[1]=0, syndrome[2]=1 then according to binary table [001] is 1(in decimal), so the first bit of vector decodedBits has an error
    [0 1 0]it means syndrome[0]=0, syndrome[1]=1, syndrome[2]=0 then according to binary table [010] is 2(in decimal), so the second bit of vector decodedBits has an error
    .... etc....

    I need a way to implement my code when I extend my matrixes, because when matrixes extend for other classes, using if else in this code will be so long and I must find a more efficient way

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Important question

    I just gave you a hint how you could avoid using if-else construct. Whether or how you will use the hint, it's totally up to you.

    All that your if-else cases do is finding right index in decodedBits where correction needed. The index can be calculated directly, and the logic of calculation was explained in my first message. And please stop repeating "I need" and "I must". Just skip that and get directly to programming.

    Another hint: you need a function that calculates index by the content of syndrome.
    Last edited by Igor Vartanov; April 3rd, 2018 at 05:23 PM.
    Best regards,
    Igor

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Important question

    As Ignor indicates in post #4, a function is needed that calculates the required index. syndrome is effectively a binary representation of the required index with syndrome[0] being the MSB. So the function needs to convert a binary number into decimal. In the code below, this is the purpose of the lambda todec(). Consider (only partly tested)

    Code:
    void D(const std::vector<double> &codedVector, std::vector<int> &decodedVector)
    {
    	const auto todec = [](const std::string& st) {
    		long long unsigned int dec = 0;
    
    		for (const auto& d : st)
    			dec = (dec << 1) | d;
    
    		return dec;
    	};
    
    	const size_t maxbits = 7;
    	const size_t maxrows = 3;
    
    	int decodedBits[maxbits] = {0};
    
    	for (size_t bits = 0, mbit = std::min(maxbits, codedVector.size()); bits < mbit; ++bits)
    		decodedBits[bits] = (codedVector[bits] >= (voltage / 4));
    
    	std::string syndrome (maxrows, 0);
    
    	for (size_t a = 0; a < maxrows; ++a) {
    		char result = 0;
    
    		for (size_t b = 0; b < maxbits; ++b)
    			result += static_cast<char>(check[a][b] * decodedBits[b]);
    
    		syndrome[a] = result % 2;
    	}
    
    	if (auto b = todec(syndrome); (b > 0) && (b <= maxbits))
    		decodedBits[b - 1] ^= 1;
    }
    Last edited by 2kaud; April 4th, 2018 at 05:59 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Mar 2018
    Posts
    40

    Re: Important question

    Hello
    referred to my question above (#1) and according to the whole definition and algorithm that I explained, in your opinion, the following piece of code is correct or not?

    Code:
    unsigned n = 0;
    int nrows = 15;
    for (int i = 0; i < nrows; i++) {
    n = (n << 1) | syndrome[i];
    	if (n != (1 << nrows) - 1)
    		decodedBits[n] ^= 1;
    In fact, this piece of code must be replaced instead of whole part of nested if-else statements in #1 and other parts are still fixed like before
    Last edited by Kmilano; April 17th, 2018 at 11:49 AM.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Important question

    this piece of code must be replaced instead of whole part of nested if-else statements in #1 and other parts are still fixed like before
    I'm not understanding -
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Mar 2018
    Posts
    40

    Re: Important question

    the following code is the first code which I posted above:

    Code:
    void D(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
    	int decodedBits[7]; 
    	for (int bits = 0; bits < 7; bits++) {
    		if (codedVector[bits] < (voltage / 4)) { decodedBits[bits] = 0; }
    		else { decodedBits[bits] = 1; }
    	} 
    	char syndrome[3];
    	for (int a = 0; a < 3; a++) {
    		char result = 0;
    		for (int b = 0; b < 7; b++) {
    			result += (check[a][b] * decodedBits[b]);
    		}
    		syndrome[a] = result % 2;
    	}
    
    	if ((syndrome[2] != 0) || (syndrome[1] != 0) || (syndrome[0] != 0)) { 
    	//The syndrome indicates where the error is
    		if ((syndrome[0] == 0) && (syndrome[1] == 0) && (syndrome[2] == 1)) {
    			
    			decodedBits[0] ^= 1;
    		}
    		else if ((syndrome[0] == 0) && (syndrome[1] == 1) && (syndrome[2] == 0)) {
    			 
    			decodedBits[1] ^= 1;
    		}
    		else if ((syndrome[0] == 0) && (syndrome[1] == 1) && (syndrome[2] == 1)) {
    			 
    			decodedBits[2] ^= 1;
    		}
    		else if ((syndrome[0] == 1) && (syndrome[1] == 0) && (syndrome[2] == 0)) {
    			 
    			decodedBits[3] ^= 1;
    		}
    		else if ((syndrome[0] == 1) && (syndrome[1] == 0) && (syndrome[2] == 1)) {
    			 
    			decodedBits[4] ^= 1;
    		}
    		else if ((syndrome[0] == 1) && (syndrome[1] == 1) && (syndrome[2] == 0)) {
    			 
    			decodedBits[5] ^= 1;
    		}
    		else {
    			 
    			decodedBits[6] ^= 1;
    		}
    	}

    instead of nested if-else I added this piece of code:

    Code:
    unsigned n = 0;
    int nrows = 7;
    for (int i = 0; i < nrows; i++) {
    n = (n << 1) | syndrome[i];
    	if (n != (1 << nrows) - 1)
    		decodedBits[n] ^= 1;
    }
    So if I replace it in the first code instead of that if-else series, I will have the following code:

    Code:
    void D(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
    	int decodedBits[7]; 
    	for (int bits = 0; bits < 7; bits++) {
    		if (codedVector[bits] < (voltage / 4)) { decodedBits[bits] = 0; }
    		else { decodedBits[bits] = 1; }
    	} 
    	char syndrome[3];
    	for (int a = 0; a < 3; a++) {
    		char result = 0;
    		for (int b = 0; b < 7; b++) {
    			result += (check[a][b] * decodedBits[b]);
    		}
    		syndrome[a] = result % 2;
    	}
    
    	if ((syndrome[2] != 0) || (syndrome[1] != 0) || (syndrome[0] != 0)) {
                unsigned n = 0;
                int nrows = 7;
                for (int i = 0; i < nrows; i++) {
                   n = (n << 1) | syndrome[i];
    	         if (n != (1 << nrows) - 1)
    		      decodedBits[n] ^= 1;
                }
            }
    }
    Is it correct or not?, I ask it because I cannot run this part in my project, because I must do framework and then I can compile this part.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Important question

    function D() compiles OK for me using VS2017 (15.6.6). When you are in a position to compile and test the code yourself then you'll be able to use the debugger and check that it is providing the required result.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured