def create_binary_wf_data(wf, sync_mkr=0, samp_mkr=0, vertical_resolution=12):
"""Given numpy arrays of waveform and marker data convert to binary format.
Assumes waveform data is np.float in range -1 to 1 and marker data can be cast to bool
Binary format is waveform in MSB and and markers in LSB
waveform sync_mkr samp_mkr
15 downto 4/2 1 0
"""
#cast the waveform to integers
if not((vertical_resolution == 12) or (vertical_resolution == 14)):
raise ValueError("vertical resolution must be 12 or 14 bits")
#convert waveform to integers
scale_factor = 2**(vertical_resolution-1)
bin_data = np.int16((scale_factor-1)*np.array(wf))
#clip if necessary
if np.max(bin_data) > scale_factor-1 or np.min(bin_data) < -scale_factor:
warnings.warn("Clipping waveform. Max value: {:d} Min value: {:d}. Scale factor: {:d}.".format(np.max(bin_data), np.min(bin_data),scale_factor))
bin_data = np.clip(bin_data, -scale_factor, scale_factor-1)
# bin_data = bin_data.byteswap()
#shift up to the MSB
bin_data = np.left_shift(bin_data, 4 if vertical_resolution == 12 else 2)
#add in the marker bits
bin_data = np.bitwise_or(bin_data, np.bitwise_or(np.left_shift(np.bitwise_and(sync_mkr, 0x1), 1), np.bitwise_and(samp_mkr, 0x1)))
return bin_data
评论列表
文章目录