gray2bin

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

Syntax

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

Description

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.

Examples

expand all

Binary to Gray Symbol Mapping

This example shows how to use the bin2gray and gray2bin functions to map integer inputs from a natural binary order symbol mapping to a Gray coded signal constellation and vice versa, assuming 16-QAM modulation. In addition, a visual representation of the difference between Gray and binary coded symbol mappings is shown.

Create a complete vector of 16-QAM integers.

x = (0:15)';

Convert the input vector from a natural binary order to a Gray encoded vector using bin2gray.

y = bin2gray(x,'qam',16);

Convert the Gray encoded symbols, y, back to a binary ordering using gray2bin.

z = gray2bin(y,'qam',16);

Verify that the original data, x, and the final output vector, z are identical.

isequal(x,z)
ans =

     1

To create a constellation plot showing the different symbol mappings, construct a 16-QAM modulator System object and use its associated constellation function to find the complex symbol values.

hMod = comm.RectangularQAMModulator;
symbols = constellation(hMod);

Plot the constellation symbols and label them using the Gray, y, and binary, z, output vectors. The binary representation of the Gray coded symbols is shown in black while the binary representation of the naturally ordered symbols is shown in red. Set the axes so that all points are displayed.

scatterplot(symbols,1,0,'b*');

for k = 1:16
    text(real(symbols(k))-0.3,imag(symbols(k))+0.3,...
        dec2base(y(k),2,4));

    text(real(symbols(k))-0.3,imag(symbols(k))-0.3,...
        dec2base(z(k),2,4),'Color',[1 0 0]);
end

axis([-4 4 -4 4])

Observe that only a single bit differs between adjacent constellation points when using Gray coding.

See Also

Was this topic helpful?