如何从保存为字节数组的tdms文件中读取python中的labview双数组(展开为字符串,然后是字符串到字节数组)

2024-04-19 21:30:15 发布

您现在位置:Python中文网/ 问答频道 /正文

当通过展平到字符串vi将数组转换为字符串时,默认情况下,LabVIEW将数组大小作为第一个元素预先挂起。 如何正确读取数组维度和值(表示双浮点的字节数组中的大端和小端格式问题)


1条回答
网友
1楼 · 发布于 2024-04-19 21:30:15

第一步是获取数组维度

    def size_lv_flatten_to_string (data,dimension=0):
        """
        returns size of array strored by lv in preprend of flatten to string from array - single dim =[0..3], 
        2d array has two of these and so on. Single dimesnion is 0 not 1
        labview prepends with array size in tdms files by default
        """
        len1=channel.data[dimension*4:dimension*4+4]  # 0 is 0:4, 4:8
        len1 = len1[::-1] #reverse 
        len_bytes = np.array (len1, dtype=np.uint8) #convert to numpy array
        data_as_i32 = len_bytes.view(dtype=np.int32)  
        return (data_as_i32[0])

下一步是将字节数组转换为双浮点。注意:在这个例子中,数据是2d数组,所以它有两个维度

x_array_size= size_lv_flatten_to_string (channel.data,dimension=0) 
y_array_size=size_lv_flatten_to_string (channel.data,dimension=1)

d2d = np.reshape(channel.data[8::], (-1, y_array_size)) #actual data starts at offset 8. First 8 bytes are length
d2d_as_dfloat = d2d.view(dtype='>f8') #use >f8 or <f8 to interpret the bytes as float64 with correct byte order

d2d_as_dfloat是一个双浮点数组

相关问题 更多 >