本文整理汇总了Python中mapper.Mapper.encode方法的典型用法代码示例。如果您正苦于以下问题:Python Mapper.encode方法的具体用法?Python Mapper.encode怎么用?Python Mapper.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mapper.Mapper
的用法示例。
在下文中一共展示了Mapper.encode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RSCoder
# 需要导入模块: from mapper import Mapper [as 别名]
# 或者: from mapper.Mapper import encode [as 别名]
class RSCoder(object):
def __init__(self, b, n, k, mapper=None):
"""Creates a new Reed-Solomon Encoder/Decoder object configured with
the given b, n and k values.
b is the base to use, must be prime
n is the length of a codeword, must be less than b
k is the length of the message, must be less than n
mapper is an class with encode and decode methods used to translate
between strings and arrays of integers
The code will have error correcting power s where 2s = n - k
The typical RSCoder is RSCoder(256, 255, 223)
"""
if n < 0 or k < 0:
raise ValueError("n and k must be positive")
if not n < b:
raise ValueError("n must be less than b")
if not k < n:
raise ValueError("Codeword length n must be greater than message length k")
if mapper is None:
if b <= len(mapper_default_alphabet):
self.mapper = Mapper(mapper_default_alphabet, mapper_default_equivs)
else:
raise ValueError("Base b too large for default mapper")
else:
self.mapper = mapper
# α (a) is the generator of the field being used. This must be picked
# appropriately if the field is changed. For integers mod p a generator
# is a number such that for every n in ints mod p, There exists and l
# Such that α^l=n mod p
# For p=59 α=2 works (this can be verified easily through brute force
self.PFint = PFint(b)
self.a = self.PFint(findgen(b))
self.b = b
self.n = n
self.k = k
# Generate the generator polynomial for RS codes
# g(x) = (x-α^1)(x-α^2)...(x-α^(n-k))
g = Polynomial((self.PFint(1),))
for l in xrange(1,n-k+1):
p = Polynomial((self.PFint(1), -self.PFint(self.a)**l))
g = g * p
self.g = g
# h(x) = (x-α^(n-k+1))...(x-α^n)
h = Polynomial((self.PFint(1),))
for l in xrange(n-k+1,n+1):
p = Polynomial((self.PFint(1), self.PFint(self.a)**l))
h = h * p
self.h = h
# g*h is used in verification, and is always x^n-1
# TODO: This is hardcoded for (255,223)
# But it doesn't matter since my verify method doesn't use it
#self.gtimesh = Polynomial(x_max=self.PFint(1), x_zero=self.PFint(1))
def encode(self, message, poly=False, nostrip=False):
"""Encode a given string with reed-solomon encoding. Returns a byte
string with the k message bytes and n-k parity bytes at the end.
If a message is < k bytes long, it is assumed to be padded at the front
with null bytes.
The sequence returned is always n bytes long.
If poly is not False, returns the encoded Polynomial object instead of
the polynomial translated back to a string (useful for debugging)
"""
n = self.n
k = self.k
message = self.mapper.pad(message, k)
if len(message)>k:
raise ValueError("Message length is max %d. Message was %d" % (k,
len(message)))
# Encode message as a polynomial:
m = Polynomial(self.PFint(x) for x in self.mapper.decode(message))
# Shift polynomial up by n-k by multiplying by x^(n-k)
mprime = m * Polynomial((self.PFint(1),) + (self.PFint(0),)*(n-k))
# mprime = q*g + b for some q
# so let's find b:
b = mprime % self.g
# Subtract out b, so now c = q*g
c = mprime - b
# Since c is a multiple of g, it has (at least) n-k roots: α^1 through
# α^(n-k)
#.........这里部分代码省略.........