code:python_reedsolomon
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
code:python_reedsolomon [2008/02/03 18:37] – created laurenceb | code:python_reedsolomon [2008/07/19 23:33] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== Main code (uses a c library for decoding, as the tx only has to encode, and we're assuming running binaries on the ground is easy) ===== | ||
+ | |||
+ | The decoder can be found at [[http:// | ||
+ | |||
<code python> | <code python> | ||
# | # | ||
- | global mm=4 # RS code over GF(2**4) - change to suit | ||
- | global nn=15 # nn=2**mm -1 | ||
- | global tt=3 # number of errors that can be corrected | ||
- | global kk=9 # kk = nn-2*tt | ||
- | global pp [1, 1, 0, 0, 1] # specify irreducible polynomial coeffts | + | mm=7 # RS code over GF(2**4) - change to suit |
- | global | + | tt=16 # number of errors that can be corrected |
- | global | + | nn=2**mm -1 #length of codeword |
- | global | + | kk = nn-2*tt |
- | global | + | |
- | global | + | primative_polynomials=[[1, |
- | global | + | pp=primative_polynomials[mm-3] # specify irreducible polynomial coeffts |
+ | alpha_to=[0]*(nn+1) | ||
+ | index_of=[0]*(nn+1) | ||
+ | gg=[0]*(nn-kk+1) | ||
+ | recd=[0]*(nn) | ||
+ | data=[0]*(kk) | ||
+ | bb=[0]*(nn-kk) | ||
- | generate_gf(): | + | def generate_gf(): |
# generate GF(2**mm) from the irreducible polynomial p(X) in pp[0]..pp[mm] | # generate GF(2**mm) from the irreducible polynomial p(X) in pp[0]..pp[mm] | ||
# lookup tables: | # lookup tables: | ||
Line 39: | Line 45: | ||
index_of[0] = -1 ; | index_of[0] = -1 ; | ||
- | gen_poly(): | + | def gen_poly(): |
# Obtain the generator polynomial of the tt-error correcting, length | # Obtain the generator polynomial of the tt-error correcting, length | ||
# nn=(2**mm -1) Reed Solomon code from the product of (X+alpha**i), | # nn=(2**mm -1) Reed Solomon code from the product of (X+alpha**i), | ||
gg[0] = 2 # primitive element alpha = 2 for GF(2**mm) | gg[0] = 2 # primitive element alpha = 2 for GF(2**mm) | ||
gg[1] = 1 # g(x) = (X+alpha) initially | gg[1] = 1 # g(x) = (X+alpha) initially | ||
- | for i in range(2, | + | for i in range(2, |
gg[i] = 1 | gg[i] = 1 | ||
for j in range(i-1, | for j in range(i-1, | ||
Line 51: | Line 57: | ||
else: | else: | ||
gg[j] = gg[j-1] | gg[j] = gg[j-1] | ||
- | gg[0] = alpha_to[(index_of[gg[0]]+i)%nn] ; # gg[0] can never be zero | + | gg[0] = alpha_to[(index_of[gg[0]]+i)%nn] ; # gg[0] can never be zero |
# convert gg[] to index form for quicker encoding */ | # convert gg[] to index form for quicker encoding */ | ||
for i in range(0, | for i in range(0, | ||
Line 57: | Line 63: | ||
- | encode_rs(): | + | def encode_rs(): |
# take the string of symbols in data[i], i=0..(k-1) and encode systematically | # take the string of symbols in data[i], i=0..(k-1) and encode systematically | ||
# to produce 2*tt parity symbols in bb[0]..bb[2*tt-1] | # to produce 2*tt parity symbols in bb[0]..bb[2*tt-1] | ||
Line 79: | Line 85: | ||
bb[j] = bb[j-1] | bb[j] = bb[j-1] | ||
bb[0] = 0 | bb[0] = 0 | ||
+ | def setup_rs(): | ||
+ | # generate the Galois Field GF(2**mm) | ||
+ | generate_gf() | ||
+ | #compute the generator polynomial for this RS code | ||
+ | gen_poly() | ||
+ | |||
+ | |||
+ | def encode_string(s): | ||
+ | d=0 | ||
+ | for i in s: | ||
+ | data[d]=ord(i) | ||
+ | d=d+1 | ||
+ | for i in range(len(s), | ||
+ | data[i]=0 | ||
+ | # encode data[] to produce parity in bb[]. Data input and parity output | ||
+ | # is in polynomial form | ||
+ | encode_rs() | ||
+ | # put the transmitted codeword, made up of data plus parity, in recd[] */ | ||
+ | for i in range(0, | ||
+ | recd[i] = bb[i] | ||
+ | for i in range(0, | ||
+ | recd[i+nn-kk] = data[i] | ||
+ | # print recd | ||
+ | # print len(recd) | ||
+ | s='' | ||
+ | for i in range(2*tt, | ||
+ | s+=chr(recd[i]) | ||
+ | for i in range(2*tt): | ||
+ | s+=chr(recd[i]) | ||
+ | # print s | ||
+ | return s | ||
+ | |||
+ | def decode_string(s, | ||
+ | from reedsolomon import Codec | ||
+ | c=Codec(nn, | ||
+ | f='' | ||
+ | for i in range(2*tt): | ||
+ | f+=s[len(s)+i-2*tt] | ||
+ | for i in range(2*tt, | ||
+ | f+=s[i-2*tt] | ||
+ | t='' | ||
+ | for i in range(len(f)): | ||
+ | t+=f[len(f)-len(t)-1] | ||
+ | #print len(t) | ||
+ | #print t | ||
+ | t=c.decode(t, | ||
+ | del s | ||
+ | s='' | ||
+ | b='' | ||
+ | for i in t[0]: | ||
+ | b+=t[0][len(t[0])-len(b)-1] | ||
+ | for i in range(len(b)): | ||
+ | s+=b[i] | ||
+ | return s,t[1] | ||
+ | |||
</ | </ | ||
+ | |||
+ | ===== Usage example ===== | ||
+ | |||
+ | <code python> | ||
+ | import reed_solomon | ||
+ | reed_solomon.setup_rs() | ||
+ | s=reed_solomon.encode_string(" | ||
+ | print s | ||
+ | t=s[0: | ||
+ | d = reed_solomon.decode_string(t, | ||
+ | print d[0] | ||
+ | print " | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== produces: ===== | ||
+ | |||
+ | < | ||
+ | $ python testcode.py | ||
+ | hello world my name is Laurence and I am your supreme ruler !!!111!!111! dJ{, | ||
+ | | ||
+ | hello world my name is Laurence and I am your supreme ruler !!!111!!111! | ||
+ | Errors at: [84, 85] | ||
+ | </ | ||
+ |
code/python_reedsolomon.1202063852.txt.gz · Last modified: 2008/07/19 23:31 (external edit)