Robochameleon  v1.0
symb2bits.m
1 function [bits,bits_] = symb2bits(symb,M,bitorder)
2 
3 if ~isvector(symb) || ~isa(symb,'uint16')
4  error('Input must be a vector (scalar) of type uint16.');
5 end
6 
7 [~,symb_range] = findrange(symb);
8 if nargin<2 || isempty(M) % M is not specified
9  log2M_ = nextpow2(symb_range.max);
10 else
11  log2M_ = log2M(M);
12 end
13 if symb_range.min<1 || symb_range.max>M
14  error('Value out of range. Symbols must be in range 1:%d.',M);
15 end
16 
17 if nargin<3
18  bitorder = 'lsb-first'; % if bitorder not specified, assume LSB is first
19 end
20 lut = logical(arrayfun(@(x)x-48,dec2bin(0:M-1)))'; % LUT for converting decimal to binary LSB first
21 if strcmpi(bitorder,'lsb-first')
22  lut = flipud(lut);
23 elseif ~strcmpi(bitorder,'msb-first')
24  % If order is not 'msb-first' at this point, bitorder is neither of two
25  % available options => throw an error.
26  error('Bit order can be set to ''lsb-first'' (default) or ''msb-first''.');
27 end
28 
29 bits = false(log2M_,numel(symb));
30 for i=max(2,symb_range.min):symb_range.max
31  idx = symb==i; % Find indices for all symbols i
32  bits(:,idx) = repmat(lut(:,i),1,nnz(idx)); % Take value from the LUT
33 end
34 bits_ = bits(:);
function findrange(in x)
Finds min-max range of a vector/matrix.