Problem to recover data from bit stream

2 views (last 30 days)
Toan
Toan on 11 Jul 2014
Commented: Toan on 11 Jul 2014
I have a txt file that has data:
123456789 abcef
Now I want to read it and recover it from bit stream. How to do it by matlab. I try to do it but to recovery that data from bit stream, it does not work This is my code
fid=fopen('test.txt','r');
sStream=fread(fid,'*char')' % to stream
%%convert to bit stream 0-1
sPacket=dec2bin(sStream,8);
%%store in vector Example s=[10000 1000 11101110 ...]
sPacket = str2num(reshape(sPacket',[],8))';
%%Let recovery it to 123456789 abcef
%%Problem in here
dPacket = {}
for idxsPacket = 1:size(sPacket,2)
dPacket{idxsPacket} = char(bin2dec(num2str(sPacket(idxsPacket)))) ;
end
It does has error but the result is not correct. Could you help me fix it? This is result
'' ' 'î' 'à' ' ' '!' 'u' 'Ø' '' 'C' '«' '²' ' ' '$' '÷' 'b' ' ' 'L' 'ª' 'à'

Answers (1)

Geoff Hayes
Geoff Hayes on 11 Jul 2014
The use of reshape is causing the problem. Is there a reason why this is being used?
After you have read in the data (from file), the characters are all converted to their equivalent binary representation in sPacket as
sPacket =
00110001
00110010
00110011
00110100
00110101
00110110
00110111
00111000
00111001
00100000
01100001
01100010
01100011
01100101
01100110
The transpose of sPacket is
sPacket' =
000000000000000
000000000011111
111111111111111
111111111000000
000000011000000
000111100000011
011001100001101
101010101010110
which is an 8x15 matrix, and so has 8*15=120 elements. Invoking reshape produces
reshape(sPacket',[],8)
00011011
00010001
10000000
11000000
01100011
00110000
00011101
11101110
01011011
00110001
10010000
11001000
01100111
00110011
11011100
a 15x8 matrix where 15 is chosen because of the use of [] (and since there are 120 elements in sPacket, 120/8 = 15). But note how these 15 binary strings are very different from the 15 strings in sPacket.
sPacket2 is then the string to number conversion of the above, and the code then works backwards, inverting all previous operations to get the original characters
for idxsPacket = 1:length(sPacket2)
dPacket{idxsPacket} = char(bin2dec(num2str(sPacket2(idxsPacket)))) ;
end
In the above, there is no attempt to invert the reshape operation, and so the characters that are produced are not the correct ones.
The easy fix is to just remove the reshape operation, replacing
sPacket2 = str2num(reshape(sPacket',[],8))';
with
sPacket2 = str2num(sPacket)';
Try the above and see what happens!
  1 Comment
Toan
Toan on 11 Jul 2014
Thank you sir, I edit by my code. It run successfully
fid=fopen('test.txt','r');
sStream=fread(fid,'*char')' % to stream
sPacket=dec2bin(sStream,8)-'0';
dPacket = {};
size(sPacket)
fwid=fopen('decoded_file.txt','w');
for idxsPacket = 1:size(sPacket);
dPacket{idxsPacket} = char(bin2dec(num2str(sPacket(idxsPacket,:)))) ;
fwrite(fwid, dPacket{idxsPacket});
end
dPacket
fclose(fid);
fclose(fwid);

Sign in to comment.

Categories

Find more on Signal Generation, Manipulation, and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!