hi
Printable View
hi
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.
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
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.
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;
}
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?
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 beforeCode: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;
I'm not understanding - :confused:Quote:
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
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:
So if I replace it in the first code instead of that if-else series, I will have the following 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;
}
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.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;
}
}
}
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.