Utilities
Utilities & helper functions for things like fixed-point/binary conversion, dB/Mag conversion, etc.
NOTE: while there are Python packages that can help with arbitrary FXP math like this, most DSP functions here can use numpy integer types to model common C/HDL fixed-point values. This also removes an external dependency from this package.
avg_pwr_dB(x)
Returns the average power (in dB) of an input array of linear values (e.x. voltage)
Source code in rfproto/utils.py
149 150 151 152 |
|
binlist2int(list_)
Converts a bit array to its integer representation.
Source code in rfproto/utils.py
50 51 52 |
|
dB_to_mag(x)
Return the linear magnitude value given the decibel (dB) value x
Source code in rfproto/utils.py
144 145 146 |
|
dB_to_power(x)
Return the linear power value given the decibel (dB) value x
Source code in rfproto/utils.py
139 140 141 |
|
dbfs_fft(x, max_mag=1.0)
Compute FFT
Source code in rfproto/utils.py
131 132 133 134 135 136 |
|
dbl_to_fxp(x, num_frac_bits)
Converts a floating-point value x
into a fixed-point integer given
some number of fractional bits num_frac_bits
. Note that a signed FXP value
requires 1-bit for sign representation. For example, a 16b signed short
int
representing a value with no integer part has num_frac_bits = 15
(or in
Q0.15
in Q format
Source code in rfproto/utils.py
24 25 26 27 28 29 30 |
|
deinterleave_iq(iq, swap_iq=False)
De-interleave input I/Q array (e.x. [I0,Q0,I1,Q1,..]
) to complex output array
Source code in rfproto/utils.py
161 162 163 164 165 166 |
|
fxp_round_halfup(x, N)
Round fixed point value x
by \(N\) bits using half-up rounding, like:
Source code in rfproto/utils.py
38 39 40 41 42 43 |
|
fxp_to_dbl(x, num_frac_bits)
Converts a fixed-point integer x
into a floating-point value given
some number of fractional bits num_frac_bits
. Note that a signed FXP value
requires 1-bit for sign representation. For example, a 16b signed short
int
representing a value with no integer part has num_frac_bits = 15
(or in
Q0.15
in Q format
Source code in rfproto/utils.py
15 16 17 18 19 20 21 |
|
fxp_truncate(x, N)
Truncate fixed point value x
by \(N\) bits
Source code in rfproto/utils.py
33 34 35 |
|
gray_code(n)
Generate \(N\)-bit gray code sequence
Source code in rfproto/utils.py
203 204 205 206 207 208 |
|
int2binlist(int_, width=None)
Converts an integer to its bit array representation.
Source code in rfproto/utils.py
61 62 63 |
|
int_list_to_bin_str(x, width=0)
Returns a list of strings based on the binary representation of a list
of input integers x
Source code in rfproto/utils.py
84 85 86 87 88 89 |
|
int_to_bin_str(x, width=0)
Convert integer to binary string representation w/o '0b' prefix. If prefix
is desired, simply use: bin(x)
Source code in rfproto/utils.py
66 67 68 69 70 71 72 |
|
int_to_fixed_width_bin_str(x, bit_width)
Convert integer to 2's complement binary representation, forcing a specified bitwidth
Source code in rfproto/utils.py
75 76 77 78 79 80 81 |
|
interleave_iq(i, q, dtype=np.float32)
Interleave separate I/Q arrays into one complex output array
Source code in rfproto/utils.py
155 156 157 158 |
|
mag_to_dB(x, ref=1.0)
Return the decibel (dB) value of the given linear magnitude value x
(e.g.
energy or signal amplitude) and an optional reference value ref
Source code in rfproto/utils.py
113 114 115 116 117 |
|
mag_to_dBFS(x, max_mag)
Converts magnitude value(s) x
(number of counts for a given integer
sample type to a dBFS (dB full scale) value, given maximum magnitude max_mag
.
Note that max_mag
is scaled by \(\sqrt{2}\) when input samples are complex to
account of max magnitude \(= \sqrt{I^{2} + Q^{2}}\), to normalize to 0 dBFS
Source code in rfproto/utils.py
120 121 122 123 124 125 126 127 128 |
|
open_iq_file(file, dtype=np.int8, swap_iq=False)
Open interleaved binary file and create complex I/Q output array. Note if
wanting to open a floating point complex file, simply use the NumPy native method:
np.fromfile(file, dtype=np.complex64)
for instance for single precision complex C-type
This method is mainly for interleaved complex integer files, which could be opened via
dtype=np.dtype([('re', np.int16), ('im', np.int16)])
, but this still needs conversion to
floating-point complex type for further math usage.
Source code in rfproto/utils.py
169 170 171 172 173 174 175 176 177 178 |
|
power_to_dB(x, ref=1.0)
Return the decibel (dB) value of the given linear power value x
and an
optional reference value ref
. This assumes power is already square of an
energy value (e.g. amplitude or magnitude)
Source code in rfproto/utils.py
105 106 107 108 109 110 |
|
write_ints_to_file(file_name, x, bit_width)
Write list of integers in fixed-width, 2's complement binary format to file
Source code in rfproto/utils.py
92 93 94 95 96 97 |
|
write_iq_to_file(file, iq, dtype=np.int8, scale_factor=1.0, round_output=True)
Write complex I/Q array to binary file in interleaved format
Source code in rfproto/utils.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|