Convert an array of Gray codes back into binary values. Parameters: ----------- gray: An ndarray of gray codes. axis: The axis along which to perform Gray decoding. Default=-1. Returns: -------- Returns an ndarray of binary values.
(gray, axis=-1)
| 67 | |
| 68 | |
| 69 | def gray2binary(gray, axis=-1): |
| 70 | """Convert an array of Gray codes back into binary values. |
| 71 | |
| 72 | Parameters: |
| 73 | ----------- |
| 74 | gray: An ndarray of gray codes. |
| 75 | |
| 76 | axis: The axis along which to perform Gray decoding. Default=-1. |
| 77 | |
| 78 | Returns: |
| 79 | -------- |
| 80 | Returns an ndarray of binary values. |
| 81 | """ |
| 82 | |
| 83 | # Loop the log2(bits) number of times necessary, with shift and xor. |
| 84 | shift = 2 ** (torch.Tensor([gray.shape[axis]]).log2().ceil().int() - 1) |
| 85 | while shift > 0: |
| 86 | gray = torch.logical_xor(gray, right_shift(gray, shift)) |
| 87 | shift = torch.div(shift, 2, rounding_mode="floor") |
| 88 | return gray |
| 89 | |
| 90 | |
| 91 | def encode(locs, num_dims, num_bits): |