0

I am working on error control in WMSNs. I want to transmit a video through binary symmetric channel with error probability p. So I have frames (images) in each gop which are shown by a matrix.

Each matrix element have decimal value which might be positive or negative. As explained here I need to convert this whole matrix to a binary stream. I used

reshape(dec2bin(typecast(b,'uint8'),8).',1,[])

for converting elements to binary streams but I cannot get back the exact number using

typecast(uint8(bin2dec(reshape(m,8,[]).')),'double').

On the other side, I think for getting the right bit error rate, I have to convert the whole matrix to just one bit stream which I'm not sure how to do that. And convert them to a matrix of measured values of a image again.

Hanna
  • 539
  • 2
  • 9
  • 24
  • I think you need `m = reshape(dec2bin(typecast(b(:),'uint8'),8).',1,[])` and then convert back with `reshape(typecast(uint8(bin2dec(reshape(m,8,[]).')),'double'),size(b))` (if you paste from this comment, beware of possible unwanted zero-width characters right before the last comma) – Luis Mendo Feb 24 '17 at 10:45
  • @LuisMendo Thanks for your answer. But there's error `The input character is not valid in MATLAB statements or expressions.` – Hanna Feb 24 '17 at 12:51
  • As I said in my previous comment, some unwanted zero-width characters creep in somehow. I've posted it as an answer, which avoids that – Luis Mendo Feb 24 '17 at 14:19

1 Answers1

0

I think you need

m = reshape(dec2bin(typecast(b(:),'uint8'),8).',1,[]);

Note that this reads the matrix in Matlab's standard, column-major order (down, then across).

Then you can convert back with

b_recovered = reshape(typecast(uint8(bin2dec(reshape(m,8,[]).')),'double'),size(b));

Since typecast converts data types without changing the underlying data, this process entails no loss of accuracy. For example,

>> b = randn(2,3)
b =
  -0.241247174335006   0.540703471823211   0.526269662140438
   0.908207564087271  -0.507829312416083  -1.067884765919437

>> m = reshape(dec2bin(typecast(b(:),'uint8'),8).',1,[])
m =
101100101011100000000010111110100010111111100001110011101011111101001010100000100011011101001111000010010001000011101101001111110010100100001111000010100101111001110001010011011110000100111111001011101101111100011000010000100010001101000000111000001011111100010010101001010111100001111001001100111101011111100000001111110101110001010100000110000101011000001110000101101111000110111111

>> b_recovered = reshape(typecast(uint8(bin2dec(reshape(m,8,[]).')),'double'),size(b))
b_recovered =
  -0.241247174335006   0.540703471823211   0.526269662140438
   0.908207564087271  -0.507829312416083  -1.067884765919437

>> b==b_recovered
ans =
  2×3 logical array
   1   1   1
   1   1   1
>> 
Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Can I have more accuracy in recovered decimal numbers? – Hanna Feb 24 '17 at 16:59
  • @Hanna There is no loss of accuracy. Please see edited answer – Luis Mendo Feb 24 '17 at 17:24
  • I need to know where MSBs are after conversion. I tested it with many numbers but I cannot uderstand how it's working. For example 1 is becomes `0000000000000000000000000000000000000000000000001111000010111111` – Hanna Feb 27 '17 at 10:47
  • @Hanna I'm not sure either. Maybe [this](https://en.wikipedia.org/wiki/Double-precision_floating-point_format) can help – Luis Mendo Feb 27 '17 at 10:51