Documentation Center

  • Trial Software
  • Product Updates


Convert Gray-encoded positive integers to corresponding Gray-decoded integers


y = gray2bin(x,modulation,M)
[y,map] = gray2bin(x,modulation,M)


y = gray2bin(x,modulation,M) generates a Gray-decoded output vector or matrix y with the same dimensions as its input parameter x. x can be a scalar, vector, or matrix. modulation is the modulation type and must be a string equal to 'qam', 'pam', 'fsk', 'dpsk', or 'psk'. M is the modulation order that can be an integer power of 2.

[y,map] = gray2bin(x,modulation,M) generates a Gray-decoded output y with its respective Gray-encoded constellation map, map.

You can use map output to label a Gray-encoded constellation. The map output gives the Gray encoded labels for the corresponding modulation. See the example below.

    Note:   If you are converting binary coded data to Gray-coded data and modulating the result immediately afterwards, you should use the appropriate modulation object or function with the'Gray' option, instead of BIN2GRAY.


 % To Gray decode a vector x with a 16-QAM Gray encoded
 % constellation and return its map, use:
 x=randi([0 15],1,100);
 [y,map] = gray2bin(x,'qam',16);
 % Obtain the symbols for 16-QAM
 hMod = modem.qammod('M', 16);
 symbols = hMod.Constellation;
 % Plot the constellation
 hold on;
 % Label the constellation points according
 % to the Gray mapping
 for jj=1:16
  'XLim',[-4 4],'YLim',...
   [-4 4],'Box','on','YGrid','on', 'XGrid','on');

The example code generates the following plot, which shows the 16 QAM constellation with Gray-encoded labeling.

See Also

Was this topic helpful?