Exemplo n.º 1
0
        /// <summary>
        /// Counts different bits between two streams
        /// </summary>
        /// <param name="bitStream"><see cref="BitStream"/> to compare to</param>
        /// <returns>Number of different bits</returns>
        public int Difference(IBitStream bitStream)
        {
            var errorCount    = 0;
            var bitsToCompare = bitStream.ReadAllBits();

            for (int i = 0; i < _data.Length; i++)
            {
                if (_data[i] != bitsToCompare[i])
                {
                    errorCount++;
                }
            }

            return(errorCount);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Counts different bits between two streams
        /// </summary>
        /// <param name="bitStream"><see cref="BitStream"/> to compare to</param>
        /// <returns>All the positions that are different</returns>
        public List <int> DifferenceWithPositions(IBitStream bitStream)
        {
            var errors        = new List <int>();
            var bitsToCompare = bitStream.ReadAllBits();

            for (int i = 0; i < _data.Length; i++)
            {
                if (_data[i] != bitsToCompare[i])
                {
                    // Add position as human readable number
                    errors.Add(i + 1);
                }
            }

            return(errors);
        }
        /// <summary>
        /// Decodes an encoded <see cref="IBitStream"/> by using predefined convolutional decoder.
        /// </summary>
        /// <param name="encodedStream"><see cref="IBitStream"/> with encoded bits</param>
        /// <returns><see cref="IBitStream"/> of decoded bits</returns>
        public IBitStream Decode(IBitStream encodedStream)
        {
            // Bit ratio is 1:2, thus decoding will result in 2 times less bits
            var decodedBits = new Bit[encodedStream.Length / 2];
            var encodedBits = encodedStream.ReadAllBits();

            var streamPosition        = 0;
            var decodedStreamPosition = 0;

            var upperRegister = new ShiftingRegister(6);
            var lowerRegister = new ShiftingRegister(6);

            while (streamPosition < encodedStream.Length)
            {
                var firstBit  = encodedBits[streamPosition++];
                var secondBit = encodedBits[streamPosition++];

                // Get all bits from upper register BEFORE shifting it
                var firstRegisterBits = upperRegister.GetBits();
                var xoredBit          =
                    firstRegisterBits[1]
                    ^ firstRegisterBits[4]
                    ^ firstRegisterBits[5];

                // Shift the register and get first bit that entered the register
                var fistPartOfDecodedBit = upperRegister.Shift(firstBit);

                var inputForSecondRegister = firstBit ^ secondBit ^ xoredBit;

                // Get all bits from lower register BEFORE shifting it
                var secondRegisterBits = lowerRegister.GetBits();

                var secondPartOfDecodedBit = MajorityDecisionElement(new [] {
                    inputForSecondRegister,
                    secondRegisterBits[0],
                    secondRegisterBits[3],
                    // Shift second register and get first bit that entered the register
                    lowerRegister.Shift(inputForSecondRegister)
                });

                var decodedBit = fistPartOfDecodedBit ^ secondPartOfDecodedBit;
                decodedBits[decodedStreamPosition++] = decodedBit;
            }

            // Skip 6 first bits since they are just initial state information
            return(new BitStream(decodedBits.Skip(6).ToArray()));
        }
Exemplo n.º 4
0
        public string FromBitStream(IBitStream stream)
        {
            var stringBuilder = new StringBuilder();

            foreach (var bit in stream.ReadAllBits())
            {
                if (bit == true)
                {
                    stringBuilder.Append('1');
                }
                else
                {
                    stringBuilder.Append(0);
                }
            }

            return(stringBuilder.ToString());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Transmits <see cref="Bit"/>s through the noisy channel and distorts it with a given error chance
        /// </summary>
        /// <param name="stream"><see cref="IBitStream"/> to distort</param>
        /// <returns>Distorted <see cref="IBitStream"/></returns>
        public IBitStream Transmit(IBitStream stream)
        {
            var randomNumberGenerator = new Random();

            var newValues = new Bit[stream.Length];
            var oldValues = stream.ReadAllBits();

            for (int counter = 0; counter < stream.Length; counter++)
            {
                var bit          = oldValues[counter];
                var randomDouble = randomNumberGenerator.NextDouble();

                var newBit = randomDouble < _errorChance ? !bit : bit;

                newValues[counter] = newBit;
            }

            return(new BitStream(newValues));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Encode bits by using predefined convolutional encoding
        /// </summary>
        /// <param name="stream"><see cref="IBitStream"/> to encode</param>
        /// <returns>Encoded <see cref="IBitStream"/></returns>
        public IBitStream Encode(IBitStream stream)
        {
            // Since bit ratio is 1:2, there will be 2 times more encoded bits
            // To save register state, we will have to encode 6 more 0-bits, resulting in 12 additional bits
            var encodedStream = new Bit[2 * stream.Length + 12];

            // We need to encode 6 more bits, because first 6 bits in register are 0s
            var bitsToEncode = new Bit[stream.Length + 6];

            for (int i = 0; i < 6; i++)
            {
                bitsToEncode[i] = new Bit(false);
            }

            // Copy 6 0-bits to the beginning of the array of bits to encode
            var bits = stream.ReadAllBits();

            Array.Copy(bits, 0, bitsToEncode, 6, bits.Length);

            var position       = 0;
            var streamPosition = 0;
            var bitsCount      = bitsToEncode.Length;

            // This piece of code does not use registers, thus we assume that we are going
            // through imaginary register just by acccessing 6 bits in a row,
            // and those bits are in reverse order
            while (position < bitsCount)
            {
                // 7th bit is the 'input' bit, 6 bits before it - imaginary register
                var inputBit  = bitsToEncode.GetBitOrDefault(position + 6);
                var secondBit = inputBit
                                ^ bitsToEncode.GetBitOrDefault(position + 4) // reversed 2nd bit (6 - 2)
                                ^ bitsToEncode.GetBitOrDefault(position + 1) // reversed 5th bit (6 - 5)
                                ^ bitsToEncode.GetBitOrDefault(position);    // reversed 6th bit (6 - 6)
                encodedStream[streamPosition++] = inputBit;
                encodedStream[streamPosition++] = secondBit;
                position++;
            }
            return(new BitStream(encodedStream));
        }
Exemplo n.º 7
0
 public BitStream(IBitStream stream)
 {
     _data = stream.ReadAllBits();
 }