【问题标题】:Read binary data as three UBYTE (8-bit Unsigned Byte) in MATLAB and use bit shift operators to get two 12 bit streams在 MATLAB 中将二进制数据读取为三个 UBYTE(8 位无符号字节)并使用位移运算符得到两个 12 位流
【发布时间】:2020-12-15 13:02:54
【问题描述】:

我需要在 MATLAB 中将数据读取为 UBYTE(8 位无符号字节),然后使用位移运算符获得两个 12 位流,样本数为 3600000。我必须通过一个命令来做到这一点,即前 3 个 UBYTE 中包含前两个 12 位值,第一个字节的 8 位,第二个字节的前 4 位,然后是字节 2 的前 4 位,所有 8字节 3 中的位。然后该过程重复接下来的 3 个字节(字节 4-6 等),依此类推。

前 12 位值:字节 1 |+ (( 字节 2 & 0xf0)

第二个12位值:((byte 2 & 0xf) > 4) |+ ((byte 3 & 0xf)

如何在 MATLAB 中应用此命令?

【问题讨论】:

    标签: matlab bit-shift 8-bit


    【解决方案1】:

    如果我理解正确,那么这样的事情可能会起作用:

    % Some data for testing
    data = randi([0, 256], 1, 9, 'uint8');
    
    % Reshape data so two 12-bit values are in each column
    data = reshape(data, 3, []);
    
    m1 = uint16(hex2dec('0f'));
    m2 = uint16(hex2dec('f0'));
    
    first_values = uint16(data(1, :)) + bitshift(bitand(uint16(data(2, :)), m2), 4);
    second_values = bitshift(bitand(uint16(data(2, :)), m1), 4) ...
      + bitshift(bitand(uint16(data(3, :)), m2), -4) ...
      + bitshift(bitand(uint16(data(3, :)), m1), 8);
    

    我可能在使用掩码时弄错了,但 bitshiftbitand 命令可能正是您要找的。​​p>

    输出是 unit16 的两个数组。如果您需要输出为 12 位类型,我不确定如何在 matlab 中最好地做到这一点。

    【讨论】:

    • 实际上,我必须读取二进制文件中的 3 个流并将值与示例文件进行比较。 Stream1:3600000 个样本,8 位,起始位置:112 Stream2:3600000 个样本,12 位,起始位置:3600112 Stream3:3600000 个样本,12 位,起始位置:9000112 我通过“* uint8”轻松读取流 1。但是关于stream2和3,我通过'ubit12 => uint16'读取它们,值是0到4095。但在示例文件中,值范围在200到1000之间。当我问我朋友之间差异的原因时我在示例文件中的值,他建议如上所述我必须在 MATLAB 中应用。
    猜你喜欢
    • 2013-01-01
    • 2013-11-26
    • 2019-08-20
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多