def show_example(): # Configurable parameters field = fieldmath.BinaryField(0x11D) generator = 0x02 msglen = 8 ecclen = 5 rs = reedsolomon.ReedSolomon(field, generator, msglen, ecclen) # Generate random message message = [random.randrange(field.size) for _ in range(msglen)] print("Original message: {}".format(message)) # Encode message to produce codeword codeword = rs.encode(message) print("Encoded codeword: {}".format(codeword)) # Perturb some values in the codeword probability = float(ecclen // 2) / (msglen + ecclen) perturbed = 0 for i in range(len(codeword)): if random.random() < probability: codeword[i] ^= random.randrange(1, field.size) perturbed += 1 print("Number of values perturbed: {}".format(perturbed)) print("Perturbed codeword: {}".format(codeword)) # Try to decode the codeword decoded = rs.decode(codeword) print("Decoded message: {}".format(decoded if (decoded is not None) else "Failure")) print("")
def reed_encode(data, ecclen): msglen = len(data) field = fieldmath.BinaryField(0x11D) generator = 0x02 rs = reed.ReedSolomon(field, generator, msglen, ecclen) encoded = rs.encode(data) return encoded
def test_correctness(): # Field parameters field = fieldmath.BinaryField(0x11D) generator = 0x02 # Run forever unless an exception is thrown or unexpected behavior is encountered testduration = 10.0 # In seconds while True: # Choose random Reed-Solomon parameters msglen = random.randrange(1, field.size) ecclen = random.randrange(1, field.size) codewordlen = msglen + ecclen if codewordlen > field.size - 1: continue numerrors = random.randrange(codewordlen + 1) rs = reedsolomon.ReedSolomon(field, generator, msglen, ecclen) # Do as many trials as possible in a fixed amount of time numsuccess = 0 numwrong = 0 numfailure = 0 starttime = time.time() while time.time() - starttime < testduration: # Generate random message message = [random.randrange(field.size) for _ in range(msglen)] # Encode message to codeword codeword = rs.encode(message) # Perturb values in the codeword indexes = [i for i in range(codewordlen)] for i in range(numerrors): # Partial Durstenfeld shuffle j = random.randrange(i, codewordlen) indexes[i], indexes[j] = indexes[j], indexes[i] # Actual perturbation codeword[indexes[i]] ^= random.randrange(1, field.size) # Try to decode the codeword, and evaluate result decoded = rs.decode(codeword) if decoded == message: numsuccess += 1 elif numerrors <= ecclen // 2: raise AssertionError("Decoding should have succeeded") elif decoded is not None: numwrong += 1 else: numfailure += 1 # Print parameters and statistics for this round print( f"msgLen={msglen}, eccLen={ecclen}, codewordLen={codewordlen}, numErrors={numerrors}; numTrials={numsuccess + numwrong + numfailure}, numSuccess={numsuccess}, numWrong={numwrong}, numFailure={numfailure}" )
def test_invert_randomly_binary(self): TRIALS = 100 f = fieldmath.BinaryField(0x481) for _ in range(TRIALS): size = int(math.sqrt(random.random()) * 9) + 2 size = max(min(size, 10), 1) mat = fieldmath.Matrix(size, size, f) for i in range(mat.row_count()): for j in range(mat.column_count()): mat.set(i, j, random.randrange(f.size)) self._test_invert(f, mat)
def GPUBI(A): #生成矩阵的标准化 f = fieldmath.BinaryField(modprim) #prim numRows = len(A) #行 numCols = len(A[0]) #列 mat = fieldmath.Matrix(numRows, numCols, f) for x in range(numRows): for y in range(numCols): mat.set(x, y, A[x][y]) mat.reduced_row_echelon_form() GPUB = np.zeros((k, n + w), dtype=np.int) xx = np.array(mat.values) return xx