General Utilities
General utilities (optic.utils)
Basic class to be used as a struct of parameters |
|
|
Convert linear value to dB (decibels). |
|
Convert dB (decibels) to a linear value. |
|
Convert dBm to Watts. |
|
Converts a positive integer or an array-like of positive integers to a NumPy array of the specified size containing bits (0 and 1). |
|
Converts a positive integer to a NumPy array of the specified size containing bits (0 and 1). |
|
Computes the dot product of two 1D arrays in a Numba-compatible way. |
|
Converts an input NumPy array of bits (0 and 1) to a decimal integer. |
|
Converts a bit error rate (BER) to a Q factor in dB. |
|
Convert LLRs to bit probabilities using a numerically stable sigmoid. |
- ber2Qfactor(ber)[source]
Converts a bit error rate (BER) to a Q factor in dB.
- Parameters:
ber (float) – The bit error rate to be converted.
- Returns:
The Q factor in dB, \(20\log_{10}Q\), corresponding to the input BER.
- Return type:
Notes
For a binary signal with Gaussian noise, the bit error rate is related to the Q-factor by \(\mathrm{BER} = \frac{1}{2}\mathrm{erfc}\left(Q/\sqrt{2}\right)\), which is inverted as
\[Q = \sqrt{2}\,\mathrm{erfc}^{-1}(2\,\mathrm{BER}). \tag{1}\]The value returned is the Q-factor in dB, following the usual convention
\[Q_{dB} = 20\log_{10}Q = 10\log_{10}Q^2, \tag{2}\]so that, e.g., \(\mathrm{BER} = 10^{-9}\) corresponds to \(Q \approx 6\) and \(Q_{dB} \approx 15.6\) dB.
- bitarray2dec(x_bitarray)[source]
Converts an input NumPy array of bits (0 and 1) to a decimal integer.
- dB2lin(x)[source]
Convert dB (decibels) to a linear value.
- Parameters:
x (float) – The value in dB to be converted to a linear value.
- Returns:
The linear value.
- Return type:
Notes
A (power) ratio expressed in decibels is converted to linear units as
\[x = 10^{x_{dB}/10}. \tag{1}\]
- dBm2W(x)[source]
Convert dBm to Watts.
- Parameters:
x (float) – The power value in dBm to be converted to Watts.
- Returns:
The power value in Watts.
- Return type:
Notes
The dBm is the power level in decibels relative to 1 mW,
\[P_{W} = 10^{-3}\cdot 10^{P_{dBm}/10}, \tag{1}\]so that 0 dBm corresponds to 1 mW and 30 dBm to 1 W.
- dec2bitarray(x, bit_width)[source]
Converts a positive integer or an array-like of positive integers to a NumPy array of the specified size containing bits (0 and 1).
- decimal2bitarray(x, bit_width)[source]
Converts a positive integer to a NumPy array of the specified size containing bits (0 and 1). This version is slightly quicker but only works for one integer.
- dotNumba(a, b)[source]
Computes the dot product of two 1D arrays in a Numba-compatible way.
This function is equivalent to
np.dotfor 1D arrays but can be JIT-compiled with Numba for accelerated execution.- Parameters:
a (ndarray of shape (N,)) – First input array (complex-valued).
b (ndarray of shape (N,)) – Second input array (complex-valued).
- Returns:
result – The dot product of a and b, computed as the sum of element-wise products.
- Return type:
Notes
Both input arrays must have the same length.
This function initializes the result as a complex number to support complex-valued operations.
- lin2dB(x)[source]
Convert linear value to dB (decibels).
- Parameters:
x (float) – The linear value to be converted to dB.
- Returns:
The value converted to dB, i.e 10log10(x).
- Return type:
Notes
The value in decibels of a (power) ratio \(x\) is
\[x_{dB} = 10\log_{10}x. \tag{1}\]
- llr2bitProb(llr, prec=<class 'numpy.float32'>)[source]
Convert LLRs to bit probabilities using a numerically stable sigmoid.
- Parameters:
llrs (1D numpy array) – Log-likelihood ratios (LLRs) of bits.
- Returns:
probs – Bit probabilities P(bit = 1).
- Return type:
1D numpy array
Notes
With the log-likelihood ratio defined as \(\Lambda = \ln\left[P(b = 0)/P(b = 1)\right]\), the probability of the bit being equal to one is given by the logistic (sigmoid) function,
\[P(b = 1) = \frac{1}{1 + e^{\Lambda}}. \tag{1}\]Eq. (1) is evaluated in a numerically stable way, which avoids the overflow of the exponential for LLRs of large magnitude.
- class parameters[source]
Basic class to be used as a struct of parameters
- copy()[source]
Returns a deep copy of the parameters object.
This method creates a new instance of the parameters class with the same attributes and values.
- Returns:
A new instance of the parameters class with copied attributes.
- Return type:
- latex_table()[source]
Generates a LaTeX table of the parameters and their values.
- Returns:
A LaTeX table representation of the parameters and their values.
- Return type:
- table()[source]
Generates a Markdown table of the parameters and their values.
- Returns:
A Markdown table representation of the parameters and their values.
- Return type:
- to_engineering_notation(value)[source]
Converts a numerical value to engineering notation with appropriate prefixes.
- view()[source]
Prints the attributes and their values in either standard or scientific notation.