-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.jl
157 lines (136 loc) · 5.33 KB
/
examples.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using FountainCodes
using Random, StatsBase, Distributions
using LinearAlgebra, SparseArrays
function r10_example(nsrc=10, nenc=20)
code = R10(nsrc)
println("Example for $code w. $nsrc source symbols and $nenc coded symbols")
src = rand(GF256, nsrc)
println("source symbols:")
println(src)
println()
# compute the intermediate symbols
A = constraint_matrix(code, 0:nsrc-1)
pre = zeros(GF256, size(A, 2))
pre[end-nsrc+1:end] .= src
int = decode(A, pre)
println("intermediate symbols:")
println(int)
println()
# randomly selected nenc unique encodes symbol identifiers, corresponding to the nenc encoded
# symbols received by the decoder (the upper limit is chosen arbitrarily)
Xs = sample(0:1000, nenc, replace=false)
# compute the encoded symbols
G = generator_matrix(code, Xs)
enc = G'*int
println("encoded symbols:")
println(enc)
println()
# decode the intermediate symbols from the encoded symbols
A = constraint_matrix(code, Xs)
pre = zeros(GF256, size(A, 2))
pre[end-nenc+1:end] .= enc
dec_int = decode(A, pre)
println("decoded intermediate symbols:")
println(dec_int)
println()
# generate the source symbols from the decoded intermediate symbols
G = generator_matrix(code, 0:nsrc-1)
dec_src = G'*dec_int
println("decoded source symbols:")
println(dec_src)
end
function rq_example(nsrc=10, nenc=20)
code = RQ(nsrc)
nsrc = code.Kp # w. zero-padding up to the next supported number of source symbols
println("Example for $code w. $nsrc source symbols and $nenc coded symbols")
src = rand(GF256, nsrc)
println("source symbols:")
println(src)
println()
# compute the intermediate symbols
A = constraint_matrix(code, 0:nsrc-1)
pre = zeros(GF256, size(A, 2))
pre[end-nsrc+1:end] .= src
int = decode(A, pre)
println("intermediate symbols:")
println(int)
println()
# randomly selected nenc unique encodes symbol identifiers, corresponding to the nenc encoded
# symbols received by the decoder (the upper limit is chosen arbitrarily)
Xs = sample(0:1000, nenc, replace=false)
# compute the encoded symbols
G = generator_matrix(code, Xs)
enc = G'*int
println("encoded symbols:")
println(enc)
println()
# decode the intermediate symbols from the encoded symbols
A = constraint_matrix(code, Xs)
pre = zeros(GF256, size(A, 2))
pre[end-nenc+1:end] .= enc
dec_int = decode(A, pre)
println("decoded intermediate symbols:")
println(dec_int)
println()
# generate the source symbols from the decoded intermediate symbols
G = generator_matrix(code, 0:nsrc-1)
dec_src = G'*dec_int
println("decoded source symbols:")
println(dec_src)
end
function lt_example(nsrc=10, nenc=20)
dd = Soliton(nsrc, nsrc, 1e-6)
code = LT{GF256}(nsrc, dd) # w. generator matrix coefficients of type GF256
println("Example for $code w. $nsrc source symbols and $nenc coded symbols")
src = rand(GF256, nsrc)
println("source symbols:")
println(src)
println()
# randomly selected nenc unique encodes symbol identifiers, corresponding to the nenc encoded
# symbols received by the decoder (the upper limit is chosen arbitrarily)
Xs = sample(0:1000, nenc, replace=false)
# compute the encoded symbols
G = generator_matrix(code, Xs)
enc = G'*src
println("encoded symbols:")
println(enc)
println()
# decode the source symbols from the encoded symbols
dec = decode(G, enc)
println("decoded intermediate symbols:")
println(dec)
end
using DelimitedFiles
function ldpc_example(nerasures=20)
# read the parity check matrix
# (transposed, since the decoder expects columns to correspond to constraints and rows to symbols)
# despite it being a binary matrix, we're using symbols of type GF256 since there are no
# optimizations specific to the binary field
H = SparseMatrixCSC{GF256,Int}(readdlm("./examples/H_612_1224.txt")')
# H = SparseMatrixCSC{GF256,Int}(readdlm("./examples/H_2400_4800.txt")')
nenc, nconstraints = size(H)
# since we do not consider encoding, we use the all-zeros vector, which is a valid codeword in
# any linear code
enc = zeros(GF256, nenc)
# randomly select nerasures symbols to erase
Xs = collect(1:nenc)
shuffle!(Xs)
Xs_rec = sort!(Xs[1:nenc-nerasures])
Xs_era = sort!(Xs[end-nerasures+1:end])
H_rec = H[Xs_rec, :] # constraints corresponding to received symbols
H_era = H[Xs_era, :] # constraints corresponding to erased symbols
enc_rec = enc[Xs_rec]
enc_era = enc[Xs_era]
println("erased encoded symbols:")
println(enc_era)
println()
# note that H'*enc = H_rec'*enc_rec + H_era'*enc_era = 0, i.e., we can solve for enc_era from
# the system of equations H_era'*enc_era = - H_rec'*enc_rec = H_rec'*enc_rec,
# where the second equality is due to the fact that -GF256(x) = GF256(x)
# finally, assuming that the code is systematic, the source symbols can be recovered from the
# decoded enc vector by selecting the subset of its indices corresponding to the systematic entries
rhs = H_rec'*enc_rec
enc_era_dec = decode(H_era, rhs)
println("decoded erased symbols:")
println(enc_era_dec)
end