Convert an array of binary values into Gray codes. This uses the classic X ^ (X >> 1) trick to compute the Gray code. Parameters: ----------- binary: An ndarray of binary values. axis: The axis along which to compute the gray code. Default=-1. Returns: --
(binary, axis=-1)
| 44 | |
| 45 | |
| 46 | def binary2gray(binary, axis=-1): |
| 47 | """Convert an array of binary values into Gray codes. |
| 48 | |
| 49 | This uses the classic X ^ (X >> 1) trick to compute the Gray code. |
| 50 | |
| 51 | Parameters: |
| 52 | ----------- |
| 53 | binary: An ndarray of binary values. |
| 54 | |
| 55 | axis: The axis along which to compute the gray code. Default=-1. |
| 56 | |
| 57 | Returns: |
| 58 | -------- |
| 59 | Returns an ndarray of Gray codes. |
| 60 | """ |
| 61 | shifted = right_shift(binary, axis=axis) |
| 62 | |
| 63 | # Do the X ^ (X >> 1) trick. |
| 64 | gray = torch.logical_xor(binary, shifted) |
| 65 | |
| 66 | return gray |
| 67 | |
| 68 | |
| 69 | def gray2binary(gray, axis=-1): |