How to convert bitstream to packets and bitxor function

5 次查看(过去 30 天)
I have to convert a bit stream file such as name `BL.264` source file . Now I want to parse the file into packets with packet size is 50 bytes. After that, I want to implement bit XOR between first packet and second packet. To do it, I will use `fread` and `bitxor` functions. However, I have some issues:
  1. If size of file is not divisible by packet sizes, we must add some padding at the end of final packet, *Right?* Let see my implementation, is it correct?
  2. The bitxor supports xor bitwise that have limit length. In my case, packet size is 50 bytes, it is impossible to bitxor two packet. How to resolve that problem.
Let see my code. If have any problem, let me know
function packet=bitstream2packet(file,psize)
%%file='BL.264';
%%psize=50;
fid=fopen(file,'r');
bytelist=fread(fid,'*uint8')';
bitexpanded = dec2bin(bytelist(:), 8) - '0';
bitstream = reshape( bitexpanded.', [],1);
%%Add padding
remain=rem(size(bitstream,1),psize);
bitstream(end+psize-remain)=0;
packet = reshape( bitstream.', [],50);
xorPacket=bitxor(packet(1,:),packet(2,:))
fclose(fid);
end

采纳的回答

Guillaume
Guillaume 2014-11-14
You're storing your bitstream as a logical array. In that case, it's a logical operation that you want to perform, not a bitwise xor. Therefore use xor instead of bitxor.
Compare
n1 = 8; n2=32
n1logicalbit = dec2bin(n1, 8) - '0'; %gives [0 0 0 0 1 0 0 0]
n2logicalbit = dec2bin(n2, 8) - '0'; %gives [0 0 1 0 0 0 0 0]
logicalxor = xor(n1logicalbit, n2logicalbit); %gives [0 0 1 0 1 0 0 0]
binaryxor = bitxor(n1, n2); %gives 40
isequal(logicalxor, dec2bin(binaryxor, 8)-'0') %return true
Otherwise your code is fine. There's a few unnecessary transpositions, you could simply do:
bytelist = fread(fid, '*uint8'); %no transpose
bitexpanded = dec2bin(bytelist, 8) -'0'; %no colon which in your case just transposed
I would also move the fclose to just after you've done the fread. This way if your code stops further down because of an error or the user interrupting it, the file won't be left open.
  7 个评论
Toan
Toan 2014-11-17
Thank Guillaume. I am very impressive with your scheme. However, my packet size is 50bytes=400bits (8 x 50) , instead of 50 bits as your code. I try to modify it by
bit400 = fread(fid, Inf, 'ubit400=>uint64');
It has error
Error using fread
Invalid precision.
How to use your scheme in this case. BTW, I need to convert the bit400 to binary representation, so I will use
bitexpanded = dec2bin(bit400(:), 8) - '0';
Is it correct? The reason why I must convert to binary representation is that my algorithm is developed for bit level. Thank you
Guillaume
Guillaume 2014-11-17
Now, I'm very confused. your original code split the file in packets of 50 bits, not bytes.
If you're operating on bytes, then you don't need to read the file in bits unit, just read it as a 50*Inf array of bytes:
byte50 = fread(fid, [50 Inf], '*uint8')'; %again it will automatically pad the last packet
Even if you're operating on bits afterward, you probably don't need to convert to binary. Certainly not for xoring packets. The bit* functions should be enough.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by