nitypes.complex =============== .. py:module:: nitypes.complex .. autoapi-nested-parse:: Complex number data types for NI Python APIs. ================ Complex Integers ================ Some NI driver APIs (such as NI-FGEN, NI-SCOPE, NI-RFSA, and NI-RFSG) use complex numbers to represent I/Q data. Python and NumPy have native support for complex floating-point numbers, but not complex integers, so the :mod:`nitypes.complex` submodule provides a NumPy representation of complex integers. :any:`ComplexInt32DType` is a NumPy structured data type object representing a complex integer with 16-bit ``real`` and ``imag`` fields. This structured data type has the same memory layout as the ``NIComplexI16`` C struct used by NI driver APIs. For more information about NumPy structured data types, see the :ref:`NumPy documentation on structured arrays `. .. note:: In ``NIComplexI16``, the number 16 refers to the number of bits in each field. In :any:`ComplexInt32DType`, the number 32 refers to the total number of bits, following the precedent set by NumPy's other complex types. For example, :any:`numpy.complex128` contains 64-bit ``real`` and ``imag`` fields. Constructing arrays of complex integers --------------------------------------- You can construct an array of complex integers from a sequence of tuples using :func:`numpy.array`: >>> import numpy as np >>> np.array([(1, 2), (3, 4)], dtype=ComplexInt32DType) array([(1, 2), (3, 4)], dtype=[('real', '>> np.zeros(3, dtype=ComplexInt32DType) array([(0, 0), (0, 0), (0, 0)], dtype=[('real', '>> x = np.array([(1, 2), (3, 4), (5, 6)], dtype=ComplexInt32DType) >>> x[0] np.void((1, 2), dtype=[('real', '>> x[1] np.void((3, 4), dtype=[('real', '>> x[0][0] np.int16(1) >>> x[0][1] np.int16(2) You can also index by the field names ``real`` and ``imag``: >>> x[0]['real'] np.int16(1) >>> x[0]['imag'] np.int16(2) Or you can index the entire array by the field names ``real`` and ``imag``: >>> x['real'] array([1, 3, 5], dtype=int16) >>> x['imag'] array([2, 4, 6], dtype=int16) Arrays of complex integers support slicing and negative indices like any other array: >>> x[0:2] array([(1, 2), (3, 4)], dtype=[('real', '>> x[1:] array([(3, 4), (5, 6)], dtype=[('real', '>> x[-1] np.void((5, 6), dtype=[('real', '>> x[0].item() (1, 2) >>> [y.item() for y in x] [(1, 2), (3, 4), (5, 6)] To convert NumPy arrays between between different complex number data types, use the :func:`convert_complex` function: >>> convert_complex(np.complex128, x) array([1.+2.j, 3.+4.j, 5.+6.j]) >>> convert_complex(ComplexInt32DType, np.array([1.23+4.56j])) array([(1, 4)], dtype=[('real', '>> convert_complex(np.complex128, x[0]) np.complex128(1+2j) >>> convert_complex(ComplexInt32DType, np.complex128(3+4j)) np.void((3, 4), dtype=[('real', ' numpy.ndarray[_Shape, numpy.dtype[_ScalarType]] convert_complex(requested_dtype: numpy.typing.DTypeLike, value: numpy.ndarray[_Shape, Any]) -> numpy.ndarray[_Shape, Any] convert_complex(requested_dtype: type[_ScalarType] | numpy.dtype[_ScalarType], value: numpy.generic[Any]) -> numpy.ndarray[tuple[], numpy.dtype[_ScalarType]] convert_complex(requested_dtype: numpy.typing.DTypeLike, value: numpy.generic[Any]) -> numpy.ndarray[tuple[], Any] Convert a NumPy array or scalar of complex numbers to the specified dtype. :param requested_dtype: The NumPy data type to convert to. Supported data types: :any:`numpy.complex64`, :any:`numpy.complex128`, :any:`ComplexInt32DType`. :param value: The NumPy array or scalar to convert. :returns: The value converted to the specified dtype. .. py:data:: ComplexInt32Base :type: typing_extensions.TypeAlias Type alias for the base type of :any:`ComplexInt32DType`, which is :any:`numpy.void`. .. py:data:: ComplexInt32DType NumPy structured data type for a complex integer with 16-bit ``real`` and ``imag`` fields.