/// <summary> /// (!) Leidžia vartotojui įvesti savo norimą G matricą. (!) /// Per vartotojo sąsaja priima vektorius, kuriais pasinaudodami keisime /// '_tempMatrix' ir sukursime '_matrixG'. /// </summary> private void LetUserEnterGMatrix() { _tempMatrix = new List <List <byte> >(_rows); var row = new List <byte>(); for (var r = 0; r < _rows; r++) { DisplayCurrentInformation(); Console.Write($"Įveskite {r + 1}-ąjį vektorių: "); var input = Console.ReadLine(); try { row = _validator.ValidateGMatrixRow(input); _tempMatrix.Add(row); } catch (Exception e) { _errorMessage = e.Message; r--; } } try { _matrixG = new MatrixG(_cols, _rows, _tempMatrix); } catch (Exception e) { _errorMessage = e.Message; LetUserEnterGMatrix(); } }
/// <summary> /// Funkcija iškviečiama, kuomet vartotojas paspaudžia "Pateikti" mygtuką. /// </summary> private void ButtonSubmitVariables_Click(object sender, RoutedEventArgs e) { var inputIsValid = false; if (_errorChance == -1) { _errorMessage = "Neteisingai įvesta klaidos tikimybė (p)."; } else if (_cols == -1) { _errorMessage = "Neteisingai įvestas matricos ilgis (n)."; } else if (_rows == -1) { _errorMessage = "Neteisingai įvesta matricos dimensija (k)."; } else { inputIsValid = true; } if (!inputIsValid) { ShowErrorMessage(); return; } // Paslepiame kodo ilgį. InputCols.Visibility = Visibility.Hidden; LabelCols.Visibility = Visibility.Hidden; // Paslepiame kodo dimensiją. InputRows.Visibility = Visibility.Hidden; LabelRows.Visibility = Visibility.Hidden; // Paslepiame kas liko. CheckBoxOwnMatrix.Visibility = Visibility.Hidden; // Varnelė savo matricos pateikimui. ButtonSubmitVariables.Visibility = Visibility.Hidden; // Mygtukas visko pateikimui. if (_entersOwnMatrix) { LetUserEnterGMatrix(); } else { _matrixG = new MatrixG(_cols, _rows); _matrixH = _matrixG.GetMatrixH(); // Paslepiame nebeaktualius langelius ir parodome aktualius. HideInputFieldsAndShowChooseImage(); } }
/// <summary> /// Funkcija iškviečiama, kuomet vartotojas paspaudžia "Enter" klavišą įvedęs vektorių matricai. /// </summary> private void InputMatrixRow_KeyUp(object sender, KeyEventArgs e) { // Reiškia matrica jau sukurta - nebereikia čia nieko daryti. if (_matrixG != null) { return; } if (e.Key != Key.Enter) { return; } try { // Patikriname ar įvestas vektorius tinkamas matricai. var row = _validator.ValidateGMatrixRow(InputMatrixRow.Text); _tempMatrix.Add(row); InputMatrixRow.Text = string.Empty; LabelInputMatrixRow.Content = $"Įveskite {_tempMatrix.Count + 1}-ąjį vektorių: "; // Jeigu jau turime pakankamai matricos eilučių. if (_tempMatrix.Count == _rows) { try { _matrixG = new MatrixG(length: _cols, dimension: _rows, matrix: _tempMatrix); _matrixH = _matrixG.GetMatrixH(); // Paslepiame nebeaktualius langelius ir parodome aktualius. HideInputFieldsAndShowChooseImage(); } catch (Exception ex) { _errorMessage = ex.Message; ShowErrorMessage(); // Nepavyko sukurti matricos iš paduotų vektorių tad išvalome esamą matricą. _tempMatrix.Clear(); LabelInputMatrixRow.Content = $"Įveskite {_tempMatrix.Count + 1}-ąjį vektorių: "; } } } // Jeigu buvo įvestas netinkamas vektorius. catch (Exception ex) { _errorMessage = ex.Message; ShowErrorMessage(); } }
private IList <byte> _receivedVector; // '_decodedVector' dekoduotas G matrica. // PUBLIC /// <summary> /// Įjungia vartotojo sąsają. /// </summary> public void Start() { _errorProbability = GetErrorProbabilty(); _channel = new Channel(_errorProbability); _cols = GetNumberOfCols(); _rows = GetNumberOfRows(); if (AskYesOrNoQuestion("Ar norite įvesti generuojančią matricą patys (jeigu ne - ji bus sugeneruota už jus)?")) { LetUserEnterGMatrix(); } else { _matrixG = new MatrixG(_cols, _rows); } _matrixH = _matrixG.GetMatrixH(); while (true) { _originalVector = GetVectorToSend(); _encodedVector = _matrixG.Encode(_originalVector); _distortedVector = _channel.SendVectorThrough(_encodedVector); _errorVector = _channel.FindDifferences(_encodedVector, _distortedVector); if (AskYesOrNoQuestion("Ar norite keisti iš kanalo gautą vektorių?")) { LetUserEnterErrorVector(); } _decodedVector = _matrixH.Decode(_distortedVector); _receivedVector = _matrixG.Decode(_decodedVector); if (!AskYesOrNoQuestion("Ar norite siųsti dar vieną vektorių?")) { break; } _originalVector = null; _encodedVector = null; _distortedVector = null; _errorVector = null; _decodedVector = null; _receivedVector = null; } }
private static void TestMatrixG() { // Todo: patikrinti, kuomet matrica G = (1 1 1). // G = 1 0 1 1 0 // 0 1 0 1 1 var matrix = new int[2][]; matrix[0] = new int[5] { 1, 0, 1, 1, 0 }; matrix[1] = new int[5] { 0, 1, 0, 1, 1 }; var matrixG = new MatrixG(length: matrix[0].GetUpperBound(0) + 1, dimension: matrix.GetUpperBound(0) + 1, matrix: matrix); // ENCODING var toEncode1 = new int[2] { 0, 0 }; var toEncode2 = new int[2] { 0, 1 }; var toEncode3 = new int[2] { 1, 0 }; var toEncode4 = new int[2] { 1, 1 }; var encoded1 = string.Join("", matrixG.Encode(toEncode1)); var encoded2 = string.Join("", matrixG.Encode(toEncode2)); var encoded3 = string.Join("", matrixG.Encode(toEncode3)); var encoded4 = string.Join("", matrixG.Encode(toEncode4)); var encoded = new List <string> { encoded1, encoded2, encoded3, encoded4 }; var outcome = new List <string> { "00000", "01011", "10110", "11101" }; for (var i = 0; i < encoded.Count; i++) { if (encoded[i] != outcome[i]) { ConsoleHelper.WriteError("'MatrixG' encodes vectors improperly."); } } // DECODING var toDecode1 = new int[5] { 0, 0, 0, 0, 0 }; var toDecode2 = new int[5] { 0, 1, 0, 1, 1 }; var toDecode3 = new int[5] { 1, 0, 1, 1, 0 }; var toDecode4 = new int[5] { 1, 1, 1, 0, 1 }; var decode1 = string.Join("", matrixG.Decode(toDecode1)); var decode2 = string.Join("", matrixG.Decode(toDecode2)); var decode3 = string.Join("", matrixG.Decode(toDecode3)); var decode4 = string.Join("", matrixG.Decode(toDecode4)); var decoded = new List <string> { decode1, decode2, decode3, decode4 }; outcome = new List <string> { "00", "01", "10", "11" }; for (var i = 0; i < decoded.Count; i++) { if (decoded[i] != outcome[i]) { ConsoleHelper.WriteError("'MatrixG' decodes vectors improperly."); } } //var matrixH = matrixG.GetMatrixH(); //matrixG.DisplayMatrix(); //matrixH.DisplayMatrix(); }
private static void TestDecodingOfCorruptedVectors() { // G = 1 1 0 1 0 0 // 0 1 1 0 1 0 // 1 0 1 0 0 1 var matrix = new int[3][]; matrix[0] = new int[6] { 1, 1, 0, 1, 0, 0 }; matrix[1] = new int[6] { 0, 1, 1, 0, 1, 0 }; matrix[2] = new int[6] { 1, 0, 1, 0, 0, 1 }; var matrixG = new MatrixG( length: matrix[0].GetUpperBound(0) + 1, dimension: matrix.GetUpperBound(0) + 1, matrix: matrix); // H = 1 0 0 1 0 1 // 0 1 0 1 1 0 // 0 0 1 0 1 1 matrix = new int[3][]; matrix[0] = new int[6] { 1, 0, 0, 1, 0, 1 }; matrix[1] = new int[6] { 0, 1, 0, 1, 1, 0 }; matrix[2] = new int[6] { 0, 0, 1, 0, 1, 1 }; var matrixH = new MatrixH(matrix); // 1. Word to encode var clean = new int[3] { 0, 1, 0 }; var dirty = new int[3] { 0, 1, 0 }; // 2. Encoded word with 'G' var encoded = matrixG.Encode(clean); // Encodes to 011010 var corrupted = matrixG.Encode(dirty); // Encodes to 011010 // 3. Corrupt it var channel = new Channel(0.500121); corrupted = channel.SendVectorThrough(corrupted); // 4. Decode it var decoded = matrixH.Decode(corrupted); if (string.Join("", encoded) != string.Join("", decoded)) { ConsoleHelper.WriteError($"{string.Join("", encoded)} does not equal {string.Join("", decoded)}."); } else { ConsoleHelper.WriteInformation("Matches."); } }
private static void TestMatrixH() { // G = 1 1 0 1 0 0 // 0 1 1 0 1 0 // 1 0 1 0 0 1 var matrix = new int[3][]; matrix[0] = new int[6] { 1, 1, 0, 1, 0, 0 }; matrix[1] = new int[6] { 0, 1, 1, 0, 1, 0 }; matrix[2] = new int[6] { 1, 0, 1, 0, 0, 1 }; var matrixG = new MatrixG( length: matrix[0].GetUpperBound(0) + 1, dimension: matrix.GetUpperBound(0) + 1, matrix: matrix); // H = 1 0 0 1 0 1 // 0 1 0 1 1 0 // 0 0 1 0 1 1 //var matrixH = matrixG.GetMatrixH(); matrix = new int[3][]; matrix[0] = new int[6] { 1, 0, 0, 1, 0, 1 }; matrix[1] = new int[6] { 0, 1, 0, 1, 1, 0 }; matrix[2] = new int[6] { 0, 0, 1, 0, 1, 1 }; // Todo: the GetMatrixH() cannot properly convert from this 'G'. var matrixH = new MatrixH(matrix); matrixG.DisplayMatrix(); matrixH.DisplayMatrix(); var toSyndrome1 = new int[6] { 0, 0, 0, 0, 0, 0 }; var toSyndrome2 = new int[6] { 0, 0, 0, 0, 0, 1 }; var toSyndrome3 = new int[6] { 0, 0, 0, 0, 1, 0 }; var toSyndrome4 = new int[6] { 0, 0, 0, 1, 0, 0 }; var toSyndrome5 = new int[6] { 0, 0, 1, 0, 0, 0 }; var toSyndrome6 = new int[6] { 0, 1, 0, 0, 0, 0 }; var toSyndrome7 = new int[6] { 1, 0, 0, 0, 0, 0 }; var toSyndrome8 = new int[6] { 0, 0, 1, 1, 0, 0 }; var syndrome1 = string.Join("", matrixH.GetSyndrome(toSyndrome1)); var syndrome2 = string.Join("", matrixH.GetSyndrome(toSyndrome2)); var syndrome3 = string.Join("", matrixH.GetSyndrome(toSyndrome3)); var syndrome4 = string.Join("", matrixH.GetSyndrome(toSyndrome4)); var syndrome5 = string.Join("", matrixH.GetSyndrome(toSyndrome5)); var syndrome6 = string.Join("", matrixH.GetSyndrome(toSyndrome6)); var syndrome7 = string.Join("", matrixH.GetSyndrome(toSyndrome7)); var syndrome8 = string.Join("", matrixH.GetSyndrome(toSyndrome8)); var syndromes = new List <string> { syndrome1, syndrome2, syndrome3, syndrome4, syndrome5, syndrome6, syndrome7, syndrome8 }; var outcomes = new List <string> { "000", "101", "011", "110", "001", "010", "100", "111" }; for (var i = 0; i < syndromes.Count; i++) { if (syndromes[i] != outcomes[i]) { ConsoleHelper.WriteError("'MatrixH' calculates syndromes improperly."); } } var toEncode1 = new int[] { 0, 0, 0 }; var toEncode2 = new int[] { 1, 0, 0 }; var toEncode3 = new int[] { 0, 1, 0 }; var toEncode4 = new int[] { 0, 0, 1 }; var toEncode5 = new int[] { 1, 1, 0 }; var toEncode6 = new int[] { 0, 1, 1 }; var toEncode7 = new int[] { 1, 0, 1 }; var toEncode8 = new int[] { 1, 1, 1 }; var encoded1 = string.Join("", matrixG.Encode(toEncode1)); var encoded2 = string.Join("", matrixG.Encode(toEncode2)); var encoded3 = string.Join("", matrixG.Encode(toEncode3)); var encoded4 = string.Join("", matrixG.Encode(toEncode4)); var encoded5 = string.Join("", matrixG.Encode(toEncode5)); var encoded6 = string.Join("", matrixG.Encode(toEncode6)); var encoded7 = string.Join("", matrixG.Encode(toEncode7)); var encoded8 = string.Join("", matrixG.Encode(toEncode8)); var encoded = new List <string> { encoded1, encoded2, encoded3, encoded4, encoded5, encoded6, encoded7, encoded8 }; outcomes = new List <string> { "000000", "110100", "011010", "101001", "101110", "110011", "011101", "000111" }; for (var i = 0; i < encoded.Count; i++) { if (encoded[i] != outcomes[i]) { ConsoleHelper.WriteError("'MatrixG' encodes vectors improperly."); } } var decoded1 = string.Join("", matrixG.Decode(matrixH.Decode(matrixG.Encode(toEncode1)))); var decoded2 = string.Join("", matrixG.Decode(matrixH.Decode(matrixG.Encode(toEncode2)))); var decoded3 = string.Join("", matrixG.Decode(matrixH.Decode(matrixG.Encode(toEncode3)))); var decoded4 = string.Join("", matrixG.Decode(matrixH.Decode(matrixG.Encode(toEncode4)))); var decoded5 = string.Join("", matrixG.Decode(matrixH.Decode(matrixG.Encode(toEncode5)))); var decoded6 = string.Join("", matrixG.Decode(matrixH.Decode(matrixG.Encode(toEncode6)))); var decoded7 = string.Join("", matrixG.Decode(matrixH.Decode(matrixG.Encode(toEncode7)))); var decoded8 = string.Join("", matrixG.Decode(matrixH.Decode(matrixG.Encode(toEncode8)))); var decoded = new List <string> { decoded1, decoded2, decoded3, decoded4, decoded5, decoded6, decoded7, decoded8 }; outcomes = new List <string> { "000", "100", "010", "001", "110", "011", "101", "111" }; for (var i = 0; i < decoded.Count; i++) { if (decoded[i] != outcomes[i]) { ConsoleHelper.WriteError("'MatrixH' decodes vectors improperly."); } } }
public static void Main(string[] args) { var vector = File.ReadAllBytes("C:\\test.bmp"); int i = 5; #region Creation of matrices. var matrix = new List <List <byte> >(2) { new List <byte> { 1, 0, 1, 1, 0 }, new List <byte> { 0, 1, 0, 1, 1 } }; var matrixG = new MatrixG(length: matrix[0].Count, dimension: matrix.Count, matrix: matrix); matrix = new List <List <byte> >(3) { new List <byte> { 1, 0, 1, 0, 0 }, new List <byte> { 1, 1, 0, 1, 0 }, new List <byte> { 0, 1, 0, 0, 1 } }; var matrixH = new MatrixH(matrix); #endregion //var channel = new Channel(0.09); //var text = "Some sample text."; //// This will contain something like: 89, 112, 201, 5, ... //var textAsBytes = Encoding.ASCII.GetBytes(text); //var stringBuilder = new StringBuilder(); //// 1. Convert the text into a string made up of binary symbols. //foreach (var word in textAsBytes) //{ // var @byte = Convert.ToString(value: word, toBase: 2) // .PadLeft(totalWidth: 8, paddingChar: '0'); // stringBuilder.Append(@byte); //} //// 2. Split it into length that matches the dimension of the matrix. //var addedExtra = 0; //var rows = 2; //var textInBinary = stringBuilder.ToString(); //stringBuilder = stringBuilder.Clear(); //for (var c = 0; c < textInBinary.Length;) //{ // var toEncodeAsString = string.Empty; // for (var r = 0; r < rows; r++) // { // if (c == textInBinary.Length) // { // toEncodeAsString += '0'; // addedExtra++; // } // else // { // toEncodeAsString += textInBinary[c]; // c++; // Move to the next bit. // } // } // // 3. Encode the word. // var toEncodeAsList = ConvertStringToByteList(toEncodeAsString); // var encoded = matrixG.Encode(toEncodeAsList); // // 4. Send it through the channel. // var deformed = channel.SendVectorThrough(encoded); // // 5. Decode the vector. // var decoded = matrixH.Decode(deformed); // // 6. Get the original word. // var fullyDecoded = matrixG.Decode(decoded); // // 7. Put it all into a string. // stringBuilder.Append(ConvertByteListToString(fullyDecoded)); //} //textInBinary = stringBuilder.ToString(); //var index = 0; //var decodedTextAsList = new List<byte>(); //// 8. Convert it back in to numbers. //for (var i = 0; i < textInBinary.Length;) //{ // // 8.1 Put it in to groups of 8 bits. // var byteAsBinaryString = string.Empty; // for (var c = 0; c < 8; c++) // { // if (i == textInBinary.Length) // { // //byteAsBinaryString += '0'; // c++; // } // else // { // byteAsBinaryString += textInBinary[i]; // i++; // } // } // // 8.2 Convert it to a decimal number. // var byteAsDecimalString = Convert.ToByte(byteAsBinaryString, 2); // decodedTextAsList.Add(byteAsDecimalString); //} //var revertedText = Encoding.ASCII.GetString(decodedTextAsList.ToArray()); //Console.WriteLine(revertedText); Console.ReadKey(); }