qformatpy.overflow

qformatpy.overflow(iarray, signed: bool = True, w: int = 16, overflow_action: str = 'Wrap')[source]

Handle overflow in an integer array based on the specified overflow action.

Parameters:
iarraynumpy.ndarray

Input array containing integer values.

signedbool

Indicates whether the numbers in the array are signed (True) or unsigned (False).

wint

Number of bits used to represent each value in the array.

overflow_actionstr
Action to be taken in case of overflow. Supported actions:
  • ‘Error’: Raise an OverflowError if overflow occurs.

  • ‘Wrap’: Wraparound overflow, values wrap around the representable range.

  • ‘Saturate’: Saturate overflow, values are clamped to the maximum or minimum representable value.

Default is ‘Wrap’.

Returns:
numpy.ndarray

Array with overflow-handled values.

Raises:
OverflowError

If overflow_action is ‘Error’ and overflow occurs.

ValueError

If an invalid overflow_action is provided.

Examples

The example below shows an 8 bit integrator overflowing with the overflow function set to ‘Wrap’:

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from qformatpy import overflow
>>> n_smp = 512
>>> y = np.zeros(n_smp)
>>> for i in range(n_smp - 1):
>>>     y[i+1] = overflow(y[i] + 1, signed=True, w=8, overflow_action='Wrap')
>>> plt.plot(y)
>>> plt.grid()
>>> plt.show()
_images/overflow-1.png