【问题标题】:how to read labview double array in python from tdms file that has been saved as byte array (flatten to string then string to byte array)如何从已保存为字节数组的tdms文件中读取python中的labview双数组(展平为字符串,然后字符串为字节数组)
【发布时间】:2021-11-19 01:54:11
【问题描述】:

在通过 flatten to string vi 将数组转换为字符串时,LabVIEW 默认将数组大小作为第一个元素添加。 如何正确读取数组维度和值(表示双浮点数的字节数组中的大端与小端格式问题)?

【问题讨论】:

    标签: python arrays floating-point endianness labview


    【解决方案1】:

    第一步是获取数组维度

        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])
    

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

    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 是一个双浮点数组

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 1970-01-01
      • 2022-01-23
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 2017-04-14
      • 1970-01-01
      相关资源
      最近更新 更多