diff --git a/devices/SDP_statistics.py b/devices/SDP_statistics.py index bb9106290b67de2b1b4f9944cbea5708ee5faa1e..a9d5c43df4a80a1007b2a533b11b157f05ba7e18 100644 --- a/devices/SDP_statistics.py +++ b/devices/SDP_statistics.py @@ -4,19 +4,19 @@ import numpy __all__ = ["StatisticsPacket"] -def extract_bits(value: bytes, first: int, last:int=None) -> int: - """ Return bits [first:last] from value, and return their integer value. Bit 0 = LSB. +def get_bit_value(value: bytes, first_bit: int, last_bit:int=None) -> int: + """ Return bits [first_bit:last_bit] from value, and return their integer value. Bit 0 = LSB. For example, extracting bits 2-3 from b'01100' returns 11 binary = 3 decimal: - extract_bits(b'01100', 2, 3) == 3 + get_bit_value(b'01100', 2, 3) == 3 - If last is not given, just the value of bit 'first' is returned. """ + If 'last_bit' is not given, just the value of bit 'first_bit' is returned. """ - # default last to first - if last is None: - last = first + # default last_bit to first_bit + if last_bit is None: + last_bit = first_bit - return value >> first & ((1 << (last - first + 1)) - 1) + return value >> first_bit & ((1 << (last_bit - first_bit + 1)) - 1) class StatisticsPacket(object): """ @@ -71,15 +71,15 @@ class StatisticsPacket(object): return { "_raw": bits, - "antenna_band_index": extract_bits(bits, 15), - "nyquist_zone_index": extract_bits(bits, 13, 14), - "t_adc": extract_bits(bits, 12), - "fsub_type": extract_bits(bits, 11), - "payload_error": extract_bits(bits, 10), - "beam_repositioning_flag": extract_bits(bits, 9), - "subband_calibrated_flag": extract_bits(bits, 8), - "reserved": extract_bits(bits, 5, 7), - "gn_index": extract_bits(bits, 0, 4), + "antenna_band_index": get_bit_value(bits, 15), + "nyquist_zone_index": get_bit_value(bits, 13, 14), + "t_adc": get_bit_value(bits, 12), + "fsub_type": get_bit_value(bits, 11), + "payload_error": get_bit_value(bits, 10), + "beam_repositioning_flag": get_bit_value(bits, 9), + "subband_calibrated_flag": get_bit_value(bits, 8), + "reserved": get_bit_value(bits, 5, 7), + "gn_index": get_bit_value(bits, 0, 4), } @property