Blame view

binary_par.py 2.32 KB
52e659d53   Brice Colombier   Initial commit
1
2
3
4
  # Author: Brice Colombier
  #         Laboratoire Hubert Curien
  #         42000 Saint-Etienne - France
  # Contact: b.colombier@univ-st-etienne.fr
fca6943a7   Brice Colombier   Clean imports
5
  # Project: CASCADE
52e659d53   Brice Colombier   Initial commit
6
7
  # File: binary_par.py
  # Date : 2016-10-12
fca6943a7   Brice Colombier   Clean imports
8
9
  from parity import parity
  from get_parities_from_indices import get_parities_from_indices
52e659d53   Brice Colombier   Initial commit
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  
  
  def binary_par(reference_response, blocks_to_correct, response_on_board):
  
      """Implementation of the BINARY algorithm found in the CASCADE protocol.
  
      From a reference response and a list of blocks to correct, it obtains the
      parities of the blocks on board. According to these parities, it narrows
      down to either the first or the second half of each block.
  
      """
      
      block_size = int(len(blocks_to_correct[0]))
      if block_size < 2:
          raise ValueError("Illegal block size: too short", blocks_to_correct)
      # Compute the parity of the first half of the response blocks
      blocks_to_correct_first_half = [x[:int(len(x)/2)] for x in blocks_to_correct]
      blocks_return = []
      ref_rep = []
fca6943a7   Brice Colombier   Clean imports
29
      parities = get_parities_from_indices(blocks_to_correct_first_half, response_on_board)
52e659d53   Brice Colombier   Initial commit
30
31
      if len(blocks_to_correct[0]) > 2:
          for i, j in enumerate(parities):
fca6943a7   Brice Colombier   Clean imports
32
              if parity(reference_response[i][:block_size/2], j):
52e659d53   Brice Colombier   Initial commit
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
                  # Error is in the first half of the block
                  blocks_return.append(blocks_to_correct[i][:block_size/2])
                  ref_rep.append(reference_response[i][:block_size/2])
              else:
                  # Error is in the second half of the block
                  blocks_return.append(blocks_to_correct[i][block_size/2:])
                  ref_rep.append(reference_response[i][block_size/2:])
          return ref_rep, blocks_return
      elif len(blocks_to_correct[0]) == 2:
          for i, j in enumerate(parities):
              if reference_response[i][0] == j:
                  blocks_return.append(blocks_to_correct[i][1])
              else:
                  blocks_return.append(blocks_to_correct[i][0])
          return ref_rep, blocks_return
  
  if __name__ == "__main__":
      list_in = [[186, 254, 7, 55, 186, 254, 7, 55],
                 [186, 254, 7, 55, 186, 254, 7, 55],
                 [186, 254, 7, 55, 186, 254, 7, 55],
                 [186, 254, 7, 55, 186, 254, 7, 55]]
      res = binary_par(list_in, "bus_script_in1.txt", "bus_script_out1.txt")
      print(res)
      print([x[0] for x in res])