private void Decode_Click(object sender, EventArgs e) { _receivedMessage = new Binary(_receivedFrame.Take(_columnsAmount)); _receivedVerification = new Binary(_receivedFrame.Skip(_columnsAmount)); _binaryReceivedHMatrix = GenerateHMatrix(_rowsAmount, _columnsAmount); _receivedMessageVerification = GenerateVerificationBits(_binaryReceivedHMatrix, _receivedMessage); _corrector = _receivedVerification ^ _receivedMessageVerification; txtCorrector.Text = _corrector.ToString(); txtReceivedVerification.Text = _receivedMessageVerification.ToString(); }
Binary GenerateVerificationBits(BinaryMatrix H, Binary message) { Binary verification = new Binary(new bool[H.RowAmount]); for (int i = 0; i < H.RowAmount; i++) { Binary row = H.GetRow(i); Binary addiction = row & message; bool verificationBit = addiction.CountOnes() % 2 == 1; verification[i] = verificationBit; } return(verification); }
static int FindFaultyBit(BinaryMatrix H, Binary s) { for (int i = 0; i < H.ColumnAmount; i++) { Binary column = H.GetColumn(i); Binary check = s ^ column; if (check.Any(b => b)) { continue; } return(i); } throw new WarningException("Faulty bit not found!"); }
BinaryMatrix GenerateHMatrix(int rowsAmount, int columnsAmount) { BinaryMatrix H = new BinaryMatrix(rowsAmount, columnsAmount); int column = 0; for (int i = 1; i <= Math.Pow(2, rowsAmount); i++) { Binary binary = new Binary(i, H.RowAmount); if (binary.CountOnes() >= 2) { for (int row = 0; row < rowsAmount; row++) { H.Set(row, column, binary[row]); } column++; } if (column >= H.ColumnAmount) { break; } } return(H); }
BinaryMatrix GenerateHWithIdentity(BinaryMatrix H) { BinaryMatrix HWithIdentity = new BinaryMatrix(H.RowAmount, H.ColumnAmount + H.RowAmount); for (int y = 0; y < H.RowAmount; y++) { for (int x = 0; x < H.ColumnAmount; x++) { HWithIdentity.Set(y, x, H.Get(y, x)); } } for (int y = 0; y < H.RowAmount; y++) { int n = 0; for (int x = H.ColumnAmount; x < H.ColumnAmount + H.RowAmount; x++) { HWithIdentity.Set(y, x, y == n); n++; } } return(HWithIdentity); }