MATLAB从二进制文件中逐位读取

时间:2016-02-01 10:16:14

标签: matlab wav binary-data bits

我能够使用以下代码成功读取.wav文件。

[y,Fs,nbits,opts] = wavread('MyFile.wav','native')

所以我现在从文件中知道存储在y中的数据,sample rate (Fs)nbits 16'native'通知我数据属于uint16类型。

现在我想逐位读取数据值。我已经知道的每个数据值由16位组成。

是否可以逐位读取一个数据值。因此,对于uint16数据值,我想阅读bit 0bits 1-15。我希望读取0位给我一个值,位1-15给我一个值。这可能吗?

我知道fread但我只知道在逐字节读取时如何使用它。

1 个答案:

答案 0 :(得分:0)

当你设法回答自己的问题时,总是很好。所以这就是我发现我能做到的:

[y,Fs,nbits,opts] = wavread('MyFile.wav','native')

Sample = y(1:10,1); %Takes 10 data values
                    %I know I have uint16 data type

%I then take a bit at a time from the data value

Bit_16 = bitget(Sample(:,1),16);
Bit_15 = bitget(Sample(:,1),15);
Bit_14 = bitget(Sample(:,1),14);
and so on . . . 

%I could then produce a matrix from the individual bit values

Matr = [Bit_16,Bit_15,Bit_14,...,];

%Each row of the matrix is now the desired binary number
%Next steps, convert to string and then to a decimal number

Str = num2str(Matr);
15-bit value = bin2dec(Str); 

然后你可以只做1位

相关问题