Scrambler#

class sionna.phy.fec.scrambling.Scrambler(seed: int | None = None, keep_batch_constant: bool = False, binary: bool = True, sequence: torch.Tensor | None = None, keep_state: bool = True, *, precision: str | None = None, device: str | None = None, **kwargs)[source]#

Bases: sionna.phy.block.Block

Randomly flips the state/sign of a sequence of bits or LLRs, respectively.

Parameters:
  • seed (int | None) – Defines the initial state of the pseudo random generator to generate the scrambling sequence. If None, a random integer will be generated. Only used when keep_state is True.

  • keep_batch_constant (bool) – If True, all samples in the batch are scrambled with the same scrambling sequence. Otherwise, per sample a random sequence is generated.

  • sequence (torch.Tensor | None) – If provided, the seed will be ignored and the explicit scrambling sequence is used. Must be an array of 0s and 1s. Shape must be broadcastable to x.

  • binary (bool) – Indicates whether bit-sequence should be flipped (i.e., binary operations are performed) or the signs should be flipped (i.e., soft-value/LLR domain-based).

  • keep_state (bool) – Indicates whether the scrambling sequence should be kept constant.

  • precision (str | None) – Precision used for internal calculations and outputs. If None, precision is used.

  • device (str | None) – Device for computation (e.g., ‘cpu’, ‘cuda:0’). If None, device is used.

Inputs:
  • x – torch.Tensor. Tensor of arbitrary shape.

  • seedNone | int. An integer defining the state of the random number generator. If explicitly given, the global internal seed is replaced by this seed. Can be used to realize random scrambler/descrambler pairs (call with same random seed).

  • binaryNone | bool. Overrules the init parameter binary if explicitly given. Indicates whether bit-sequence should be flipped (i.e., binary operations are performed) or the signs should be flipped (i.e., soft-value/LLR domain-based).

Outputs:

x_out – torch.Tensor. Tensor of same shape as x.

Notes

For inverse scrambling, the same scrambler can be re-used (as the values are flipped again, i.e., result in the original state). However, keep_state must be set to True as a new sequence would be generated otherwise.

The scrambler block is stateless, i.e., the seed is either random during each call or must be explicitly provided during init/call. If the seed is provided in the init function, this fixed seed is used for all calls. However, an explicit seed can be provided during the call function to realize true random states.

Scrambling is typically used to ensure equal likely 0 and 1 for sources with unequal bit probabilities. As we have a perfect source in the simulations, this is not required. However, for all-zero codeword simulations and higher-order modulation, so-called “channel-adaptation” [Pfister03] is required.

Examples

import torch
from sionna.phy.fec.scrambling import Scrambler

scrambler = Scrambler(seed=42, keep_state=True)
bits = torch.randint(0, 2, (10, 100), dtype=torch.float32)
scrambled = scrambler(bits)
unscrambled = scrambler(scrambled)  # Re-use for descrambling
assert torch.allclose(bits, unscrambled)

Attributes

property seed: int#

Seed used to generate random sequence.

property keep_state: bool#

Indicates if new random sequences are used per call.

property sequence: torch.Tensor | None#

Explicit scrambling sequence if provided.