Right shift an array of binary values. Parameters: ----------- binary: An ndarray of binary values. k: The number of bits to shift. Default 1. axis: The axis along which to shift. Default -1. Returns: -------- Returns an ndarray with zero prepende
(binary, k=1, axis=-1)
| 10 | |
| 11 | |
| 12 | def right_shift(binary, k=1, axis=-1): |
| 13 | """Right shift an array of binary values. |
| 14 | |
| 15 | Parameters: |
| 16 | ----------- |
| 17 | binary: An ndarray of binary values. |
| 18 | |
| 19 | k: The number of bits to shift. Default 1. |
| 20 | |
| 21 | axis: The axis along which to shift. Default -1. |
| 22 | |
| 23 | Returns: |
| 24 | -------- |
| 25 | Returns an ndarray with zero prepended and the ends truncated, along |
| 26 | whatever axis was specified.""" |
| 27 | |
| 28 | # If we're shifting the whole thing, just return zeros. |
| 29 | if binary.shape[axis] <= k: |
| 30 | return torch.zeros_like(binary) |
| 31 | |
| 32 | # Determine the padding pattern. |
| 33 | # padding = [(0,0)] * len(binary.shape) |
| 34 | # padding[axis] = (k,0) |
| 35 | |
| 36 | # Determine the slicing pattern to eliminate just the last one. |
| 37 | slicing = [slice(None)] * len(binary.shape) |
| 38 | slicing[axis] = slice(None, -k) |
| 39 | shifted = torch.nn.functional.pad( |
| 40 | binary[tuple(slicing)], (k, 0), mode="constant", value=0 |
| 41 | ) |
| 42 | |
| 43 | return shifted |
| 44 | |
| 45 | |
| 46 | def binary2gray(binary, axis=-1): |
no outgoing calls
no test coverage detected