In this example shows how to use the API of pyerasure
to encode and decode with
using the RandomUniform coefficient generator.
1#!/usr/bin/env python
2# encoding: utf-8
3
4# License for Commercial Usage
5# Distributed under the "PYERASURE EVALUATION LICENSE 1.3"
6# Licensees holding a valid commercial license may use this project in
7# accordance with the standard license agreement terms provided with the
8# Software (see accompanying file LICENSE.rst or
9# https://www.steinwurf.com/license), unless otherwise different terms and
10# conditions are agreed in writing between Licensee and Steinwurf ApS in which
11# case the license will be regulated by that separate written agreement.
12#
13# License for Non-Commercial Usage
14# Distributed under the "PYERASURE RESEARCH LICENSE 1.2"
15# Licensees holding a valid research license may use this project in accordance
16# with the license agreement terms provided with the Software
17# See accompanying file LICENSE.rst or https://www.steinwurf.com/license
18
19import os
20import random
21
22import pyerasure
23import pyerasure.finite_field
24import pyerasure.generator
25
26
27def main():
28 """
29 Simple example showing how to encode and decode a block of memory using an
30 RLNC code.
31 """
32
33 # Pick the finite field to use for the encoding and decoding.
34 field = pyerasure.finite_field.Binary8()
35
36 # Pick the number of symbols to encode/decode.
37 symbols = 150
38
39 # Pick the size of each symbol in bytes
40 symbol_bytes = 1400
41
42 # Create an encoder and decoder. The encoder and decoder must be created
43 # identically to be compatible.
44 encoder = pyerasure.Encoder(field, symbols, symbol_bytes)
45 decoder = pyerasure.Decoder(field, symbols, symbol_bytes)
46
47 # Create generator generator. The generator must similarly be created
48 # based on the encoder/decoder.
49 generator = pyerasure.generator.RandomUniform(field, encoder.symbols)
50
51 # Allocate some data to encode. In this case we make a buffer
52 # with the same size as the encoder's block size (the max.
53 # amount a single encoder can encode)
54 # Just for fun - fill data_in with random data
55 data_in = bytearray(os.urandom(encoder.block_bytes))
56
57 # Assign the data buffer to the encoder so that we may start
58 # to produce encoded symbols from it
59 encoder.set_symbols(data_in)
60
61 # Keep track of the systematic symbols
62 systematic_index = 0
63
64 # Lose packets with 10% probability
65 loss_probability = 10
66
67 while not decoder.is_complete():
68
69 if encoder.rank > systematic_index:
70 print("systematic symbol", end="")
71 index = systematic_index
72 systematic_index += 1
73 symbol = encoder.symbol_data(index)
74
75 # Drop packet based on loss probability
76 if random.randint(0, 100) < loss_probability:
77 print(" - lost")
78 else:
79 decoder.decode_systematic_symbol(symbol, index)
80 print(f" - decoded, rank now {decoder.rank}")
81 else:
82 print("coded symbol", end="")
83 coefficients = generator.generate()
84 symbol = encoder.encode_symbol(coefficients)
85
86 # Drop packet based on loss probability
87 if random.randint(0, 100) < loss_probability:
88 print(" - lost")
89 else:
90 decoder.decode_symbol(symbol, bytearray(coefficients))
91 print(f" - decoded, rank now {decoder.rank}")
92
93 if data_in == decoder.block_data():
94 print("Decoding was successful. Yay!")
95 else:
96 print("Data was not decoded correctly. Something went wrong")
97
98
99if __name__ == "__main__":
100 main()