Guide Converting Data - 2020-UQ-Communication-Systems/public GitHub Wiki
Let's consider the ascii character W
in this example.
input = 'W';
We know that in Hexadecimal this is represented as 0x57
and in decimal as 87
. However, when we want to convert this into bits, the bit ordering is very important. We can have least significant bit (LSB) ordering which means we first send the "least significant bit" and most significant bit ordering (MSB).
In Matlab and Python we can convert this from the character representation to a decimal using the following code:
Python:
numdata=ord(input)
MATLAB:
numdata = input + 0
In Matlab/Octave we can use the bitget
function to convert the ASCII character into bits.
bits = bitget(numdata, 1:8);
Note that we needed to convert the string into a number as above, and we want to output bits 1 to 8 in that order. Matlab/Octave always indexes arrays and bits from 1, therefore what normally would be called bit 0-7 is called 1-8.
On the other hand in Python, we can access data using a for loop and bit operations:
[(numdata & (1<<x))>>x for x in np.arange(0, 8, 1)]
Python indexes arrays from 0, so the np.arange operator creates numbers from 0-7.
Here is a diagram of the meaning of each of those bits:
If we want to send BPSK information, then this is all we need. However if we want to send QPSK, or 16-QAM data, then we need to group bits into symbols.
For QPSK, we have 2 bits per symbol, and therefore we would send: 3 1 1 1
.
For 16-QAM we would have 4 bits per symbol and therefore we would send: 7 5
.
Similarly for MSB ordering we can use the following function in MATLAB:
bits = bitget(numdata, 8:-1:1);
or in Python
[(numdata & (1<<x))>>x for x in np.arange(7, -1, -1)]
and for the character W
the following outputs will occur:
2 bits per symbol: 1 1 1 3
4 bits per symbol: 5 7