code:python_reedsolomon
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
code:python_reedsolomon [2008/02/06 16:06] – new code 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> | ||
# | # | ||
Line 81: | 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 the Galois Field GF(2**mm) | ||
- | generate_gf() | + | generate_gf() |
- | print " | + | |
- | print " | + | |
- | for i in range(0, | + | |
- | print i, | + | |
- | print " | + | |
#compute the generator polynomial for this RS code | #compute the generator polynomial for this RS code | ||
- | gen_poly() | + | gen_poly() |
- | #for known data, stick a few numbers into a zero codeword. Data is in | ||
- | # polynomial form. | ||
- | |||
- | # for example, say we transmit the following message (nothing special!) | ||
- | s=raw_input(" | ||
- | d=0 | ||
- | for i in s: | ||
- | data[d]=ord(i) | ||
- | d=d+1 | ||
- | for i in range(len(s), | ||
- | data[i]=0 | ||
+ | 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 | # encode data[] to produce parity in bb[]. Data input and parity output | ||
# is in polynomial form | # 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 | ||
- | encode_rs() | + | 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] | ||
- | # 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(len(recd)-1, | + | |
- | if i>40 or i<17: | + | ===== Usage example ===== |
- | s+=chr(recd[i]) | + | |
- | else: | + | <code python> |
- | s+=" " | + | import reed_solomon |
+ | reed_solomon.setup_rs() | ||
+ | s=reed_solomon.encode_string("hello world my name is Laurence and I am your supreme ruler !!!111!!111!") | ||
print s | print s | ||
- | from reedsolomon import Codec | + | t=s[0: |
- | c=Codec(nn,kk,mm) | + | d = reed_solomon.decode_string(t,[]) |
- | print c.decode(s,[86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101]) | + | 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.1202313967.txt.gz · Last modified: 2008/07/19 23:31 (external edit)