Robochameleon  v1.0
constmap.m
1 function [map,demap] = constmap(consttype,M,maptype,usermap)
2 % CONSTMAP Generates constellation mappings.
3 %
4 % MAP = CONSTMAP(CONSTTYPE,M,MAPTYPE[,USERMAP])
5 %
6 % CONSTTYPE - constellation type (check types supported by constref)
7 % M - constellation order
8 % MAPTYPE - constellation map type; available:
9 % 'gray' Gray mapping
10 % 'user' User-defined mapping (used to generate demapping map)
11 % USERMAP - user-provided map with M elements
12 %
13 % Base-1 index is used for maps to simplify MATLAB code.
14 % Example of use is in constutils_example.m
15 %
16 % See also: constref
17 %
18 % This function requires grays function from 'Gray Code Manipulation'
19 % package available at http://www.mathworks.com/matlabcentral/fileexchange/15570-gray-code-manipulation
20 %
21 % Robert Borkowski, rbor@fotonik.dtu.dk
22 % Technical University of Denmark
23 % v1.0, 15 August 2014
24 
25 switch lower(maptype)
26  case {'linear', 'lin', 'binary', 'bin'}
27  map = uint16(1:M)';
28  case 'gray'
29  seq = uint16(0:M-1)';
30  map = bitxor(seq,bitshift(seq,-1))+1; % Generate Gray code
31  if strcmpi(consttype,'qam')
32  log2M = log2(M);
33  if iswhole(log2M)
34  for i=log2M:2*log2M:M
35  map(i+(1:log2M)) = map(i+(log2M:-1:1));
36  end
37  else
38  error('Gray mapping for constellations with M~=2^k is undefined. Please work on 8-QAM.');
39  end
40  end
41 
42  case 'custom'
43  if nargin<4
44  error('For type ''custom'', a custom map has to be provided.');
45  elseif numel(usermap)~=M
46  error;
47  else
48  map = uint16(usermap(:));
49  end
50 end
51 
52 [~,demap] = sort(map);
53 demap = uint16(demap);
54 demap_ = demap;
55 
56 switch lower(consttype)
57  case 'qam'
58  if M == 32
59  demap(:,end+1) = [1:1:M]; % This is not a Gray code! This change make this code compatible with TxSymbolsGen_v1.
60  else
61  log2M = log2(M);
62  for i=1:3;
63  demap(:,end+1) = reshape(rot90(reshape(demap_,2^(log2M/2),2^(log2M/2)),i),M,1);
64  end
65  end
66  case 'psk'
67  for i=1:M-1
68  demap(:,end+1) = circshift(demap_,i);
69  end
70 
71  case 'dpask'
72  if ~rem(M,sqrt(M)) && M>1 % square constellation
73  demap(:,end+1) = reshape(reshape(demap_,log2(M),log2(M)).',M,1);
74  elseif M==8 %couldn't figure out how to do this elegantly, sorry Robert
75  demap(:,end+1) = demap_([1, 5, 3, 7, 2, 6, 4, 8]);
76 % demap = [ 1 1;
77 % ? ?
78 % 2 2
79 % ? ?
80 % ? ?
81 % ? ?
82 % ? ?
83 % ? ? ];
84 
85  end
86 
87  case 'dpask-experimental'
88  demap = [1 1
89  2 2
90  3 5
91  4 6
92  5 3
93  6 4
94  7 7
95  8 8 ];
96 % demap = [demap [flipud(demap(1:4,:)); demap(5:8,:)]];
97 
98  otherwise
99  warning('No demapping permutations generated.');
100 end