Skip to content

striqt.waveform.units

Unit strings for power, amplitude, and dB quantities, and their SI prefixes.

SIPrefix dataclass

SIPrefix(exponent: int, symbol: str)

a decimal exponent that is a multiple of 3 and its SI symbol.

exponent instance-attribute

exponent: int

the decimal exponent, a multiple of 3 in the range of PREFIXES

symbol instance-attribute

symbol: str

the prefix symbol, '' at exponent 0

Units dataclass

Units(base: str, per: Union[str, None] = None)

a units string split at its first /.

base instance-attribute

base: str

the unit left of the first /: 'dBm', 'mW', '√mW', 'Hz', 'S'

per class-attribute instance-attribute

per: Union[str, None] = None

the denominator right of the first /, or None without one

format_quantity

format_quantity(
    value: float,
    units: Union[str, None],
    *,
    prefix: Union[SIPrefix, str, Literal['auto']] = 'auto',
    places: Union[int, None] = None,
    sep: str = ' ',
) -> str

return value in engineering notation with a prefixed units suffix.

Parameters:

  • value (float) –

    the value in units; a numpy or cupy scalar is converted with float

  • units (Union[str, None]) –

    the units string of value, '' for a bare prefix, or None to return the scaled mantissa alone

  • prefix (Union[SIPrefix, str, Literal['auto']], default: 'auto' ) –

    'auto' selects si_prefix of value, a prefix symbol such as 'M' or 'u' forces that prefix, and an SIPrefix is applied as given

  • places (Union[int, None], default: None ) –

    the decimal places of the mantissa, or None for the shortest representation ('g')

  • sep (str, default: ' ' ) –

    the text between the mantissa and the suffix

Returns:

  • str –

    the mantissa, then sep and the prefix symbol and units when units is not

  • str –

    None; 'nan', '∞', or '-∞' for a value that is not finite, with the

  • str –

    unprefixed units suffix

Raises:

  • ValueError –

    if prefix is a symbol that PREFIXES does not define

has_power_form

has_power_form(units: str) -> bool

return whether units carries a DB_POWER, POWER, or AMPLITUDE field.

is_prefixable

is_prefixable(units: Union[str, None]) -> bool

return whether an SI prefix may scale the base unit of units.

A base that begins dB, the bases 'mW', 'FS', 'a.u.', and 'unitless' (with or without a leading √), and None are not prefixable. The empty string is a bare number that takes a prefix.

parse_units

parse_units(units: str) -> Units

return units split into the base unit and the denominator.

Parameters:

  • units (str) –

    a units string such as 'dBm/Hz' or 'S/s'

Returns:

  • Units –

    the Units with base the text before the first / and per the text

  • Units –

    after it, or None without a /

power_family

power_family(units: str) -> Union[PowerUnits, None]

return the power unit that the dB, linear, or amplitude units string refers to.

'dBm', 'mW', and '√mW/Hz' give 'mW'; 'dBFS' gives 'FS'; 'dB(a.u.)' gives 'a.u.'. A ratio ('dB') or a unit of another kind gives None.

resolve_power_units

resolve_power_units(
    units: str, power_units: PowerUnits
) -> str

return units with its power form fields filled by power_units.

DB_POWER becomes the dB unit of power_units ('dBm' for 'mW'), POWER becomes power_units, and AMPLITUDE the square root form. A denominator such as '{dB}/Hz' is kept. A string without a form field is returned unchanged.

si_prefix

si_prefix(
    values: Union[float, Iterable[float]],
    units: Union[str, None] = None,
    *,
    places: Union[int, None] = None,
) -> SIPrefix

return one SI prefix for every value of values in units.

The prefix is that of the largest finite nonzero magnitude, with a mantissa that rounds to 1000 under places carried to the next prefix. With two or more distinct magnitudes, the prefix is lowered to that of the step between the closest pair, by at most one prefix. Thus 3.7 and 3.75 GHz read as 3700 and 3750 MHz, and every mantissa stays below 10⁶. Units that are not is_prefixable, and values with no finite nonzero magnitude, give the exponent 0.

Parameters:

  • values (Union[float, Iterable[float]]) –

    one value, or the values to share the prefix; zero, NaN, and infinite values are ignored

  • units (Union[str, None], default: None ) –

    the units string of the values, or None for a bare number

  • places (Union[int, None], default: None ) –

    the decimal places the mantissas are shown with, or None for the shortest representation

Returns:

  • SIPrefix –

    the SIPrefix with an exponent in the range of PREFIXES

to_dB

to_dB(units: str) -> str

return the dB form of a linear power or amplitude units string.

'mW' and '√mW' give 'dBm', 'W/Hz' gives 'dBW/Hz', and 'unitless' gives 'dB'. A string without a known linear base is returned unchanged.

to_linear

to_linear(units: str) -> str

return the linear power form of a dB or amplitude units string.

'dBm' and '√mW' give 'mW', 'dBW/Hz' gives 'W/Hz', and 'dB' gives 'unitless'. A string without a known base is returned unchanged.

to_wave

to_wave(units: str) -> str

return the amplitude (square root of power) form of a units string.

'dBm' and 'mW' give '√mW', and 'dB' gives '√unitless'. A string without a known base is returned unchanged.

with_power_units

with_power_units(
    units: str, power_units: PowerUnits
) -> str

return units with its power unit replaced by power_units in the same form.

The dB, linear, or amplitude form and the denominator are kept: 'dBm/Hz' with 'FS' gives 'dBFS/Hz', and '√mW' with 'a.u.' gives '√a.u.'. A string that power_family does not recognize is returned unchanged.