7Bpencil

Hamming

Dec 22nd, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use srict"
  2. //используются 8-битные кодовые слова, соответственно переходящие в 12-битный код
  3. //возможно кодировать и декодировать сообщения любой длины
  4.  
  5. function Encode(message) {
  6.     let binaryRepresentation = GetBinaryStringRepresentationASCII(message);
  7.     const wordLength = 8;
  8.     let words = SplitMessageIntoWords(binaryRepresentation, wordLength);
  9.     let encodedWords = [];
  10.  
  11.     for (let i = 0; i < words.length; i++) {
  12.         let wordWithZeroControlBits = InsertZeroControlBitsIntoWord(words[i]);
  13.         encodedWords.push(CalculateControlBitsInWord(wordWithZeroControlBits));
  14.     }
  15.     return encodedWords.join("");
  16. }
  17.  
  18.  
  19. function Decode(message) {
  20.     const wordLength = 12;
  21.     let words = SplitMessageIntoWords(message, wordLength);
  22.     let decodedWords = []
  23.  
  24.     for (let i = 0; i < words.length; i++) {
  25.         let wordToDecode = words[i];
  26.         let mistakePosition = GetSingleMistakePosition(wordToDecode);
  27.         if (mistakePosition != 0) wordToDecode = CorrectSingleMistake(wordToDecode, mistakePosition - 1);
  28.         decodedWords.push(DecodeWord(wordToDecode));
  29.     }
  30.     return decodedWords.join("");
  31. }
  32.  
  33.  
  34. function DecodeWord(word) {
  35.     let binaryMessageWord = DeleteControlBits(word);
  36.     let binaryASCIIcode = DeleteLeadingZeros(binaryMessageWord);
  37.     let decimalASCIIcode = parseInt(binaryASCIIcode.join(""), 2);
  38.     let messageWord = String.fromCharCode(decimalASCIIcode);
  39.     return messageWord;
  40. }
  41.  
  42.  
  43. function GetSingleMistakePosition(word) {
  44.     let syndromMatrix = CalculateControlBitsSequence(word);
  45.     let mistakePositionBinaryWithZeros = syndromMatrix.reverse();
  46.     let mistakePositionBinary = DeleteLeadingZeros(mistakePositionBinaryWithZeros);
  47.     return mistakePositionBinary === undefined ? 0 : parseInt(mistakePositionBinary.join(""), 2);
  48. }
  49.  
  50.  
  51. function CorrectSingleMistake(word, mistakePosition) {
  52.     let beforeMistake = word.slice(0, mistakePosition);
  53.     let correctedMistake = (word[mistakePosition] == 1 ? 0 : 1)
  54.     let afterMistake = word.slice(mistakePosition + 1, word.length);
  55.     return (beforeMistake.concat(correctedMistake)).concat(afterMistake);
  56. }
  57.  
  58.  
  59. function InsertZeroControlBitsIntoWord(word) {
  60.     let result = [];
  61.     for (let i = 1, k = 0; k < word.length; i++) {
  62.         if (CanNumberBePowerOfTwo(i)) {
  63.             result.push(0);
  64.         } else {
  65.             result.push(word[k]);
  66.             k++;
  67.         }
  68.     }
  69.     return result;
  70. }
  71.  
  72.  
  73. function CalculateControlBitsSequence(word) {
  74.     let controlBitsSequence = [];
  75.     let transformationMatrix = GetTransformationMartix(word.length);
  76.  
  77.     for (let i = 1; i < word.length + 1; i++) {
  78.         if (CanNumberBePowerOfTwo(i)) {
  79.             let controlBit = 0;
  80.             for (let k = 0; k < word.length; k++) {
  81.                 controlBit += word[k] * transformationMatrix[k][Log2(i)];
  82.             }
  83.             controlBitsSequence.push(controlBit % 2);
  84.         }
  85.     }
  86.     return controlBitsSequence;
  87. }
  88.  
  89.  
  90. function DeleteControlBits(word) {
  91.     let result = [];
  92.     for (let i = 0; i < word.length; i++) {
  93.         if (CanNumberBePowerOfTwo(i + 1)) {
  94.             continue;
  95.         } else {
  96.             result.push(word[i]);
  97.         }
  98.     }
  99.     return result;
  100. }
  101.  
  102.  
  103. function DeleteLeadingZeros(word) {
  104.     for (let i = 0; i < word.length; i++) {
  105.         if (word[i] == 0) continue;
  106.         return word.slice(i, word.length);
  107.     }
  108. }
  109.  
  110.  
  111. function CalculateControlBitsInWord(word) {
  112.     let result = [];
  113.     let controlBitsSequence = CalculateControlBitsSequence(word)
  114.  
  115.     for (let i = 1, k = 0; i < word.length + 1; i++) {
  116.         if (CanNumberBePowerOfTwo(i)) {
  117.             result.push(String(controlBitsSequence[k]));
  118.             k++;
  119.         } else {
  120.             result.push(String(word[i - 1]));
  121.         }
  122.     }
  123.  
  124.     return result.join("");
  125. }
  126.  
  127.  
  128. function GetTransformationMartix(wordLength) {
  129.     let result = [];
  130.     for (let i = 1; i < wordLength + 1; i++) {
  131.         let binaryIndex = GetNumberInBinaryNumberSystem(i, 4);
  132.         result.push(GetArrayFromString(binaryIndex).reverse());
  133.     }
  134.     return result;
  135. }
  136.  
  137.  
  138. function GetBinaryStringRepresentationASCII(string) {
  139.     let result = "";
  140.     let amountOfDigits = 8;
  141.     for (let i = 0 ; i < string.length; i++) {
  142.         let charCode = string.charCodeAt(i);
  143.         result += GetNumberInBinaryNumberSystem(charCode, amountOfDigits);
  144.     }
  145.     return result;
  146. }
  147.  
  148.  
  149. function SplitMessageIntoWords(message, wordLength) {
  150.     let result = [];
  151.     for (let i = 0; i < message.length; i += wordLength) {
  152.         let word = message.slice(i, i + wordLength);
  153.         result.push(GetArrayFromString(word));
  154.     }
  155.     return result;
  156. }
  157.  
  158.  
  159. function GetArrayFromString(string) {
  160.     result = [];
  161.     for (let i = 0; i < string.length; i++) {
  162.         result.push(+string[i]);
  163.     }
  164.     return result;
  165. }
  166.  
  167.  
  168. function CanNumberBePowerOfTwo(number) {
  169.     for (let i = 0; ; i++) {
  170.         if (number == 2**i) return true;
  171.         if (number < 2**i) return false;
  172.     }
  173. }
  174.  
  175.  
  176. function Log2(x) {
  177.     for (let i = 0; ; i++) {
  178.         if (x == 2**i) return i;
  179.     }
  180. }
  181.  
  182.  
  183. function GetNumberInBinaryNumberSystem(number, amountOfDigits) {
  184.     let binary = number.toString(2);
  185.     return binary.length < amountOfDigits ?
  186.             MultiplyString("0", amountOfDigits - binary.length) + binary :
  187.             binary;
  188. }
  189.  
  190.  
  191. function MultiplyString(string, times) {
  192.     let result = "";
  193.     for (let i = 0; i < times; i++) {
  194.         result += string;
  195.     }
  196.     return result;
  197. }
  198.  
  199.  
  200. test();
  201. function test() {
  202.     let test = "k"; //"100111011011"
  203.     let testEncodedFull = Encode(test);
  204.     let testDecodedFull = Decode(testEncodedFull);
  205.     let testDecodeWithSingleMistake = Decode("000111011011")
  206. }
Advertisement
Add Comment
Please, Sign In to add comment