utils

pyripherals.utils contains several methods used in peripheral classes that may be useful.

Module containing general functions useful to interfaces.

Abe Stroschein, ajstroschein@stthomas.edu

Lucas Koerner, koer2434@stthomas.edu

pyripherals.utils.calc_impedance(v_in, v_out, resistance)[source]

Calculate the impedance of an unknown component.

It is assumed the voltage source is connect in series with both the resistor and the unknown component.

Parameters:
  • v_in (list(int or float)) – The sinusoidal voltage source in the circuit.

  • v_out (list(int or float)) – The output voltage across the unknown component.

  • resistance (int or float) – The resistance of the resistor in the circuit in Ohms.

Returns:

numpy.ndarray

Return type:

the array of impedances calculated.

pyripherals.utils.count_bytes(num)[source]

Count the number of bytes in a number.

pyripherals.utils.create_yaml(overwrite=False)[source]

Create a default config.yaml file.

pyripherals.utils.custom_signed_to_int(data, num_bits)[source]

Return the Python int form of a two’s complement integer in the given number of bits.

For negative numbers, we invert all bits (in num_bits) and add 1 (only keeping num_bits) to apply two’s complement and get the positive version of the number. Then we multiply the number by -1 so it has the same value as before, but now as a 32-bit Python int.

Parameters:
  • data (int or list(int) or np.ndarray(int)) – The binary data to apply two’s complement to.

  • num_bits (int) – The number of bits the number is represented in. This is important for knowing how many bits to keep in the end.

Returns:

int_data – The two’s complement conversion of the data with num_bits.

Return type:

int or np.ndarray(int)

Examples

>>>custom_signed_to_int(65531, 16) -5 >>>custom_signed_to_int([65531, 5], 16) array([-5, 5]) >>>custom_signed_to_int(np.array([65531, 5]), 16) array([-5, 5])

pyripherals.utils.from_voltage(voltage, num_bits, voltage_range, with_negatives=False)[source]

Convert the float/int voltage into binary data.

We use the bit-width and voltage range to determine the voltage per bit. Then we divide the voltage by the bit-voltage and round to an integer to find the binary data representation.

Parameters:
  • voltage (np.integer or np.floating or np.ndarray of those) – The voltage data to convert.

  • num_bits (int) – The number of bits to convert the voltage data to. Maximum 64.

  • voltage_range (int) – The total voltage range (peak-to-peak) used for the voltage.

  • with_negatives (bool) – True to convert the voltage with full negative at 0x0, zero at half scale, and full positive at full scale, False otherwise.

Returns:

np.ndarray of int or int

Return type:

binary version of voltage data. Scalar int returned if voltage was a scalar.

pyripherals.utils.get_memory_usage()[source]

Get a sorted list of the objects and their sizes.

pyripherals.utils.int_to_custom_signed(data, num_bits)[source]

Return the given integer in two’s complement form in the given number of bits.

Assuming the data fits in the given number of bits, we simply cut off any leading 1’s from the number being negative. Viewed as a num_bits-bit integer, this does not change its value.

Parameters:
  • data (int or list(int) or np.ndarray(int)) – The binary data to apply two’s complement to.

  • num_bits (int) – The number of bits the number is represented in. This is important for knowing how many bits to keep in the end.

Returns:

twos_data – The two’s complement conversion of the data with num_bits.

Return type:

int or np.ndarray(int)

Examples

>>>int_to_custom_signed(-5, 16) 65531 >>>int_to_custom_signed([-5, 5], 16) array([65531, 5]) >>>int_to_custom_signed(np.array([-5, 5]), 16) array([65531, 5])

pyripherals.utils.int_to_list(integer, byteorder='little', num_bytes=None)[source]

Convert an integer into a list of integers 1 byte long.

Parameters:
  • integer (int) – The integer to convert.

  • byteorder (str) – Either ‘little’ for little Endian (LSB first) or ‘big’ for big Endian (MSB first).

  • num_bytes (int) – The number of bytes to convert the number into. None means the function will return the minimum number of bytes necessary to represent the number.

pyripherals.utils.plt_uniques(data, ax=None, block=True)[source]

Plot only the first and last of each group of unique data points to save time and memory.

Parameters:
  • data (np.ndarray) – Data to plot. Must be at least 1 dimensional

  • ax (matplotlib.axes._subplots.AxesSubplot) – The axes to plot the data on.

  • block (bool) – Whether to block when showing the plot. Used in plt.show(block=block).

pyripherals.utils.read_h5(data_dir, file_name, chan_list=[0])[source]

Read in h5 data and return the time and adc_data for the channels in input list

Parameters:
  • (string) (file_name) –

  • (string)

  • ints) (chan_list (list of) –

Returns:

  • (numpy.ndarray) time

  • (dicionary of numpy.ndarrays) adc data

pyripherals.utils.reverse_bits(number, bit_width=8)[source]

Return an integer with the reversed bits of the input number.

pyripherals.utils.str_bitfile_version(bitfile_version: int) str[source]

Return the string format of the bitfile version.

Parameters:

bitfile_version (int) – The bitfile version number in integer form.

Returns:

str

Return type:

The string format, decimal separated form of the bitfile version number.

Examples

>>> str_bitfile_version(21301)
'02.13.01'
pyripherals.utils.to_voltage(data, num_bits, voltage_range, use_twos_comp=False)[source]

Convert the binary read data into a float voltage.

We use the bit-width of the data and the voltage range of the channel to determine the voltage per bit. Then we multiply the binary data by that voltage for the total voltage.

Parameters:
  • data (int or list(int) or np.ndarray(np.integer)) – The binary voltage data.

  • voltage_range (int) – The total voltage range (peak-to-peak) used for the data.

  • use_twos_comp (bool) – True if the given data is in two’s complement form, False otherwise.

Returns:

float or np.ndarray of float

Return type:

voltage(s) represented by the given binary data.