Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Lua Online

local sentText = "abc"

function splitString(s)
	local t = {}
	for i = 1,s:len() do table.insert(t, s:sub(i,i)) end
	return t
end

function toByteForm(c) -- Izveido dotā burta baita reprezentaciju
	local byteString = "00000000"
	local i = c:byte()
	if i >= 128 then
		byteString = "10000000"
		i = i - 128
	end
	if i >= 64 then
		byteString = byteString:sub(1,1)..'1000000'
		i = i - 64
	end
	if i >= 32 then
		byteString = byteString:sub(1,2)..'100000'
		i = i - 32
	end
	if i >= 16 then
		byteString = byteString:sub(1,3)..'10000'
		i = i - 16
	end
	if i >= 8 then
		byteString = byteString:sub(1,4)..'1000'
		i = i - 8
	end
	if i >= 4 then
		byteString = byteString:sub(1,5)..'100'
		i = i - 4
	end
	if i >= 2 then
		byteString = byteString:sub(1,6)..'10'
		i = i - 2
	end
	if i >= 1 then
		byteString = byteString:sub(1,7)..'1'
	end
	return byteString
end

function printTable(t,indent) -- tiek drukāta tabula kopā ar tās iespējām. Tiek izmantota, lai pārbaudītu tabulu struktūru, kas rodas pēc gramatikas pieshkirshanas
	for k,v in pairs(t) do
		if type(v) == "table" then
			s = ""
			for i = 1, indent do s = s.." " end
			print (s..k..": ")
			printTable(v,indent+4)
		else
			s = ""
			for i = 1, indent do s = s.." " end
			print(s..k..":\t"..v)
		end
	end
end
--local t = splitString(sentText)
--for i,c in ipairs(t) do t[i] = toByteForm(c) end
--printTable(t,0)

function toCodeword(s) --s burts ir baita attēlojums
	local codeword = "__"..s:sub(1,1).."_"..s:sub(2,4).."_"..s:sub(5,8)
	local count1 = 0
	local count2 = 0
	local count3 = 0
	local count4 = 0
	if codeword:sub(3,3) == "1" then count1, count2 = count1 + 1, count2 + 1 end
	if codeword:sub(5,5) == "1" then count1, count3 = count1 + 1, count3 + 1 end
	if codeword:sub(6,6) == "1" then count3, count2 = count3 + 1, count2 + 1 end
	if codeword:sub(7,7) == "1" then count1, count2, count3 = count1 + 1, count2 + 1, count3 + 1 end
	if codeword:sub(9,9) == "1" then count1, count4 = count1 + 1, count4 + 1 end
	if codeword:sub(10,10) == "1" then count4, count2 = count4 + 1, count2 + 1 end
	if codeword:sub(11,11) == "1" then count1, count2, count4 = count1 + 1, count2 + 1, count4 + 1 end
	if codeword:sub(12,12) == "1" then count4, count3 = count4 + 1, count3 + 1 end
	count1 = count1 % 2
	count2 = count2 % 2
	count3 = count3 % 2
	count4 = count4 % 2
	codeword = count1..count2..s:sub(1,1)..count3..s:sub(2,4)..count4..s:sub(5,8)
	return codeword
end

function toTransmission(t)
	local transmissionString = ""
	for i = 1, 12 do
		for j, codeword in ipairs(t) do transmissionString = transmissionString..codeword:sub(i,i) end
		--transmissionString = transmissionString.." "
	end
	return transmissionString
end

function burstError(s) --ieviesh burstError random pozīcijai un garumam
	local firstErr = math.random(s:len() - (s:len()/6))
	--local lastErr = math.random(s:len() - firstErr) + firstErr
	local lastErr = math.random(s:len()/6) + firstErr --burstError nav garāks par 2n, kur n ir pārsūtītie koda vārdi
	print ("Transmisijas pirmā un pēdējā pārveidotā bita indekss:")
	print (firstErr, lastErr)
	return s:sub(1,firstErr-1)..flipBits(s:sub(firstErr, lastErr))..s:sub(lastErr+1)
end

function flipBits(s) -- s'ā apgriež visus bitus
	local s2 = ""
	for i = 1, s:len() do
		if s:sub(i,i) == "0" then s2 = s2.."1" else s2 = s2.."0" end
	end
	return s2
end

function transmissionToCodewords(s)
	local t = {}
	local cwCount = s:len() / 12
	for i = 1, cwCount do table.insert(t,"") end
	for i = 1, s:len() do
		local n = i % cwCount
		if n == 0 then n = cwCount end
		t[n] = t[n]..s:sub(i,i)
	end
	return t
end

function checkErrors(s) --s is Hamming codeword
	local errorLocation = 0
	local ones = {0,0,0,0}
	if s:sub(1,1) == '1' then ones[1] = ones[1] + 1 end
	if s:sub(2,2) == '1' then ones[2] = ones[2] + 1 end
	if s:sub(3,3) == '1' then ones[1], ones[2] = ones[1] + 1, ones[2] + 1 end
	if s:sub(4,4) == '1' then ones[3] = ones[3] + 1 end
	if s:sub(5,5) == '1' then ones[1], ones[3] = ones[1] + 1, ones[3] + 1 end
	if s:sub(6,6) == '1' then ones[2], ones[3] = ones[2] + 1, ones[3] + 1 end
	if s:sub(7,7) == '1' then ones[1], ones[2], ones[3] = ones[1] + 1, ones[2] + 1, ones[3] + 1 end
	if s:sub(8,8) == '1' then ones[4] = ones[4] + 1 end
	if s:sub(9,9) == '1' then ones[1], ones[4] = ones[1] + 1, ones[4] + 1 end
	if s:sub(10,10) == '1' then ones[2], ones[4] = ones[2] + 1, ones[4] + 1 end
	if s:sub(11,11) == '1' then ones[1], ones[2], ones[4] = ones[1] + 1, ones[2] + 1, ones[4] + 1 end
	if s:sub(12,12) == '1' then ones[3], ones[4] = ones[3] + 1, ones[4] + 1 end
	if ones[1] % 2 == 1 then errorLocation = errorLocation + 1 end
	if ones[2] % 2 == 1 then errorLocation = errorLocation + 2 end
	if ones[3] % 2 == 1 then errorLocation = errorLocation + 4 end
	if ones[4] % 2 == 1 then errorLocation = errorLocation + 8 end
	if errorLocation ~= 0 then
		if s:sub(errorLocation, errorLocation) == '0' then return s:sub(1,errorLocation-1)..'1'..s:sub(errorLocation+1) end
		if s:sub(errorLocation, errorLocation) == '1' then return s:sub(1,errorLocation-1)..'0'..s:sub(errorLocation+1) end
	end
	return s
end

function discardChecksums(s)
	return s:sub(3,3)..s:sub(5,7)..s:sub(9,12)
end

function byteFormToLetter(s)
	local c = 0;
	if s:sub(1,1) == '1' then c = c + 128 end
	if s:sub(2,2) == '1' then c = c + 64 end
	if s:sub(3,3) == '1' then c = c + 32 end
	if s:sub(4,4) == '1' then c = c + 16 end
	if s:sub(5,5) == '1' then c = c + 8 end
	if s:sub(6,6) == '1' then c = c + 4 end
	if s:sub(7,7) == '1' then c = c + 2 end
	if s:sub(8,8) == '1' then c = c + 1 end
	return string.char(c)
end

function combineString(t)
	local s = ""
	for i, letter in ipairs(t) do s = s..letter end
	return s
end

--printTable(transmissionToCodewords("aaabbbcccdddeeefffggghhhiiijjjkkklll"),0)

math.randomseed(os.time())
print ("Sūtāmā ziņa:")
print (sentText)
print ("\f")
local t = splitString(sentText)
for i,c in ipairs(t) do t[i] = toByteForm(c) end
print ("\f")
print ("Katra rindiņa ir ziņojuma baits:")
printTable(t,0)
print ("\f")
print ("Baiti pārveidoti uz Hamming kodu:")
for i,c in ipairs(t) do t[i] = toCodeword(c) end
printTable(t,0)
print ("\f") 
print ("Bitu pārraide (pārraušanas kļūdu apstrādei):")
local s = toTransmission(t)
print (s)
print ("\f")
s = burstError(s)
print ("\f")
print ("Bitu secība pēc nejauša garuma pārslēgšanās kļūdas ieviešanas [0; 2n], kur n ir sākotnējais ziņojuma garums baitos:")
print (s)
local t2 = transmissionToCodewords(s)
print ("\f") 
print ("Bitu secība sadalīta Hamminga kodu:")
printTable(t2,0)
for i, codeword in ipairs(t2) do t2[i] = checkErrors(codeword) end
print ("\f")
print ("Kods pēc labojumiem:")
printTable(t2,0)
for i, codeword in ipairs(t2) do t2[i] = discardChecksums(codeword) end
print ("\f")
print ("Ziņojuma baiti pēc visu pārbaudes bitu norakstīšanas:")
printTable(t2,0)
for i, byteForm in ipairs(t2) do t2[i] = byteFormToLetter(byteForm) end
print ("\f")
print ("Saņemtais ziņojums:")
print (combineString(t2))

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.