Skip to content

Instantly share code, notes, and snippets.

@mbaz
Created September 18, 2021 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbaz/0d8259e2a4aa7c19902a476e19b27ee7 to your computer and use it in GitHub Desktop.
Save mbaz/0d8259e2a4aa7c19902a476e19b27ee7 to your computer and use it in GitHub Desktop.
Simple demo of FER improvement using Hamming (7,4) over a BSC
module HamFER
export run
using StaticArrays
const Gt = SA[1 0 0 0 ;
0 1 0 0 ;
0 0 1 0 ;
0 0 0 1 ;
1 1 1 0 ;
0 1 1 1 ;
1 0 1 1]
const epat = SA[1 0 0 0 ;
0 1 0 0 ;
0 0 1 0 ;
0 0 0 1 ;
0 0 0 0 ;
0 0 0 0 ;
0 0 0 0]
function encode(nibble::SVector{4,Int})::SVector{7,Int}
return Gt*nibble .% 2
end
function iscodeword(cw::SVector{7,Int})::Bool
if encode(cw[SVector(1,2,3,4)]) == cw
return true
end
return false
end
function decode(rx::SVector{7,Int})::SVector{4,Int}
idx_d = SVector(1,2,3,4)
# If rx is a codeword, return it
if iscodeword(rx)
return rx[idx_d]
end
# Introduce 1-bit errors until a codeword is obtained
for err in eachcol(epat)
x = (rx .+ err) .% 2
if iscodeword(x)
return x[idx_d]
end
end
# More than one error: bail out and return the data
return rx[idx_d]
end
function BSC(b::Int, p::Float64)::Int
if rand() < p
return b+1 % 2
else
return b
end
end
function run(; pb = 1e-3, experiments = 1000)
idx_d = SVector(1,2,3,4)
bsc(x) = BSC(x, pb)
err_uncoded = 0 # errored frames without coding
err_coded = 0 # errored frames with coding
fer_uncoded = 0.0
fer_coded = 0.0
B = [0,1]
# coded
for i = 1:experiments
for j = 1:16
data = SVector{4}(rand(B, (4,)))
cw = encode(data)
rx = bsc.(cw)
cw_est = decode(rx)
if cw_est[idx_d] != data
err_coded += 1
break
end
end
end
# uncoded
for i = 1:experiments
data = SVector{64}(rand(B, (64,)))
rx = bsc.(data)
if data != rx
err_uncoded += 1
end
end
fer_coded = err_coded/experiments
fer_uncoded = err_uncoded/experiments
return (fer_uncoded, fer_coded,)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment