UKHAS Wiki

UK High Altitude Society

User Tools

Site Tools


code:python_reedsolomon

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
code:python_reedsolomon [2008/02/06 16:06] – new code laurencebcode: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://hathawaymix.org/Software/ReedSolomon|hathawaymix.org]]
 +
 <code python> <code python>
 #!/usr/bin/python #!/usr/bin/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 "Look-up tables for GF(2**%2d)\n",mm +
-print "  i   alpha_to[i]  index_of[i]\n" +
-for i in range(0,nn): +
- print i,alpha_to[i],index_of[i] +
-print "\n\n" +
 #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("enter string ") 
-d=0 
-for i in s: 
- data[d]=ord(i) 
- d=d+1 
-for i in range(len(s),kk): 
- 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),kk):
 + 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,nn-kk):
 + recd[i] = bb[i] 
 + for i in range(0,kk):
 + recd[i+nn-kk] = data[i] 
 +# print recd
 +# print len(recd)
 + s=''
 + for i in range(2*tt,len(recd)):   #pick all the human readable data
 + s+=chr(recd[i])
 + for i in range(2*tt):               
 + s+=chr(recd[i])
 +# print s
 + return s
  
-encode_rs()+def decode_string(s,corruption):    #corruption is a list of know corrupted chr positions 
 + from reedsolomon import Codec 
 + c=Codec(nn,kk,mm) 
 + f=''  
 + for i in range(2*tt):   #pick all the parity and put @ front 
 + f+=s[len(s)+i-2*tt] 
 + for i in range(2*tt,len(s)):  #copy over the data 
 + f+=s[i-2*tt] 
 + t='' 
 + for i in range(len(f)):   #reverses the whole string 
 + t+=f[len(f)-len(t)-1] 
 + #print len(t)  
 + #print t 
 + t=c.decode(t,corruption) 
 + 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,nn-kk): 
- recd[i] = bb[i]  
-for i in range(0,kk): 
- recd[i+nn-kk] = data[i]  
-print recd 
-print len(recd) 
  
-s='' +</code> 
-for i in range(len(recd)-1,-1,-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:9]+"df"+s[11:len(s)] 
-c=Codec(nn,kk,mm+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 "Errors at: ",d[1 
 </code> </code>
 +
 +===== produces: =====
 +
 +<code>
 +$ python testcode.py
 +hello world my name is Laurence and I am your supreme ruler !!!111!!111! dJ{,wOWOIUv?M
 +         ]'tkN{edkM98
 +hello world my name is Laurence and I am your supreme ruler !!!111!!111!
 +Errors at:  [84, 85]
 +</code>
 +
code/python_reedsolomon.1202313967.txt.gz · Last modified: 2008/07/19 23:31 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki