Ejemplo n.º 1
0
        /// <summary>
        /// Iškraipo paduotą simbolių eilutę (0 gali tapti 1 ir atvirkščiai).
        /// </summary>
        /// <param name="binaryString">Simbolių eilutė, sudaryta iš 0 ir 1.</param>
        /// <returns>Iškraipytą simbolių eilutę.</returns>
        private string SendBinaryStringThroughChannel(string binaryString)
        {
            var stringBuilder = new StringBuilder();

            for (var c = 0; c < binaryString.Length;)
            {
                // 1. Daliname į tokio ilgio dalis, kad sutaptų su kodo dimensija.
                var toEncodeAsString = string.Empty;
                for (var r = 0; r < _rows; r++)
                {
                    // Jeigu pasibaigė tekstas, tačiau mums vis tiek reikia simbolių.
                    if (c == binaryString.Length)
                    {
                        // Pridedame nulį priekyje, kad nepasikeistų dvejetainio skaičiaus reikšmė.
                        toEncodeAsString = '0' + toEncodeAsString;
                    }
                    else
                    {
                        toEncodeAsString += binaryString[c];
                        c++;
                    }
                }

                // 2. Užkoduojame generuojančia matrica.
                var toEncodeAsList = Converter.BinaryStringToBinaryVector(toEncodeAsString);
                var encoded        = _matrixG.Encode(toEncodeAsList);

                // 1104 = (14 baitų (bitmap header) + 124 baitai (DIB header)) * 8 bitai.
                // Jeigu tai kontrolinė informacija tuomet jos nesiunčiame kanalu.
                var deformed = c >= 1104
                                        ? _channel.SendVectorThrough(encoded)
                                        : encoded;

                // 5. Atkoduojame 'Step-by-Step' algoritmu.
                var decoded = _matrixH.Decode(deformed);

                // 6. Atkoduojame generuojančia matrica.
                var fullyDecoded = _matrixG.Decode(decoded);

                // 7. Viską sudedame į vieną ilgą vektorių.
                foreach (var bit in fullyDecoded)
                {
                    stringBuilder.Append(bit);
                }
            }

            return(stringBuilder.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Išsiunčia užkoduotą tekstą kanalu.
        /// </summary>
        /// <param name="text">Tekstas, kurį norima siųsti kanalu.</param>
        /// <returns>Iš kanalo grįžęs rezultatas.</returns>
        private string SendEncodedText(string text)
        {
            var textAsVector = new List <byte>();

            // 1. Konvertuojame tekstą į dvejetainę simbolių eilutę.
            var textInBinary = ConvertTextToBinary(text);

            // 2. Daliname į tokio ilgio dalis, kad sutaptų su kodo dimensija.
            for (var c = 0; c < textInBinary.Length;)
            {
                var toEncodeAsString = string.Empty;
                for (var r = 0; r < _rows; r++)
                {
                    // Jeigu pasibaigė tekstas, tačiau mums vis tiek reikia simbolių.
                    if (c == textInBinary.Length)
                    {
                        // Pridedame nulį priekyje, kad nepasikeistų dvejetainio skaičiaus reikšmė.
                        toEncodeAsString = '0' + toEncodeAsString;
                    }
                    else
                    {
                        toEncodeAsString += textInBinary[c];
                        c++;
                    }
                }

                // 3. Užkoduojame generuojančia matrica.
                var toEncodeAsList = Converter.BinaryStringToBinaryVector(toEncodeAsString);
                var encoded        = _matrixG.Encode(toEncodeAsList);

                // 4. Siunčiame kanalu.
                var deformed = _channel.SendVectorThrough(encoded);

                // 5. Atkoduojame 'Step-by-Step' algoritmu.
                var decoded = _matrixH.Decode(deformed);

                // 6. Atkoduojame generuojančia matrica.
                var fullyDecoded = _matrixG.Decode(decoded);

                // 7. Viską sudedame į vieną ilgą vektorių.
                foreach (var bit in fullyDecoded)
                {
                    textAsVector.Add(bit);
                }
            }

            return(ConvertBinaryVectorToText(textAsVector));
        }
Ejemplo n.º 3
0
        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;
            }
        }
Ejemplo n.º 4
0
        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.");
            }
        }
Ejemplo n.º 5
0
        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.");
                }
            }
        }