Main Content

Gray-Coded Binary Ordering

Gray coding is a technique that multilevel modulation schemes often use to minimize the bit error rate. It consists of ordering modulation symbols so that the binary representations of adjacent symbols differ by only one bit. This section shows a communications system with Gray-coded 8-ary phase shift keying (8-PSK) modulation to compare the error rate performance of Gray and natural binary coded bit ordering.


The example in this section modulates gray and natural binary coded data frames using the 8-PSK method. The data frames passes through an AWGN channel, and are demodulated using an 8-PSK demodulator. Error rate calculator System objects measure the symbol and bit error rates.

In this communications system, PSK Modulator System objects:

  • Accept binary-valued inputs that represent integers between 0 and M – 1. M is the modulation order and is equal to 8 for 8-PSK modulation.

  • Map binary representations to constellation points using Gray-coded and natural binary-coded ordering.

  • Produce unit-magnitude complex phasor outputs, with evenly spaced phases between 0 and 2π(M – 1)/M.

This table indicates the relationship between Gray-coded binary representations in the input and phasors in the output. The second column of the table is an intermediate representation that the System object uses in its computations.

Modulator InputGray-Coded OrderingModulator Output
0112exp(jπ/2) = exp(j2π/4)
1016exp(j3π/2) = exp(j6π/4)
1104exp() = exp(j4π/4)

This table sorts the first two columns from the previous table, according to the output values. This sorting makes it clearer that there is only a 1 bit difference between neighboring symbols. In the following figure, notice that the numbers in the second column of the table appear in counterclockwise order.

Modulator OutputModulator Input
exp(/2) = exp(j2π/4)011
exp() = exp(j4π/4)110
exp(j3π/2) = exp(j6π/4)101

Compare Error Rate for Gray- and Binary-Coded Ordering

Compare Gray coding with natural binary coding by using appropriately configured PSK modulator and PSK demodulator System objects. This simulation iterates over a range of bit energy to noise power spectral density, Eb/N0, values and runs until either the specified maximum number of bit errors (maxNumErrs) or the maximum number of bits (maxNumBits) is reached for Gray coding for each Eb/N0 point.


Initialize the system variables and create System objects for modulation, demodulation, AWGN channel, and error rate operations. Since the comm.AWGNChannel System object™ and the randi function use the default random stream, set the random number generator seed to ensure repeatable results. The state of the random number generator is stored before setting the random stream seed, and restored at the end of the example.

M = 8; % Modulation order for 8-PSK
SamplesPerFrame = 10000;

prevState = rng;

Create PSK modulator and demodulator System objects to map the binary input data to 8-PSK Gray- and binary-coded constellations.

pskmod = comm.PSKModulator('ModulationOrder',M,'SymbolMapping','Gray', ...
pskdemod = comm.PSKDemodulator('ModulationOrder',M,'SymbolMapping','Gray', ...
    'PhaseOffset',0,'BitOutput',true,'OutputDataType','uint8', ...
    'DecisionMethod','Hard decision');
pskmodb = comm.PSKModulator('ModulationOrder',M,'SymbolMapping','Binary', ...
pskdemodb = comm.PSKDemodulator('ModulationOrder',M,'SymbolMapping','Binary', ...
    'PhaseOffset',0,'BitOutput',true,'OutputDataType','uint8', ...
    'DecisionMethod','Hard decision');

Create an AWGN channel System object to add noise to the modulated signal. The noise method is configured to Eb/N0 for the processing loop. The PSK modulator generates symbols with 1 W of power, so the signal power property of the AWGN channel object is set to 1 W also.

awgnchan = comm.AWGNChannel('NoiseMethod','Signal to noise ratio (Eb/No)', ...

Create a symbol error rate and bit error rate calculator System objects to compare the demodulated integer and bit data with the original source data. This comparison yields symbol error and bit error statistics. The output of the error rate calculator System object is a three-element vector containing the calculated error rate, the number of errors observed, and the amount of data processed. The simulation uses the three-element vector to determine when to stop the simulation.

symerror = comm.ErrorRate;
biterror = comm.ErrorRate;
biterrorb = comm.ErrorRate;

Frame Processing Loop

Configure a frame processing loop where data is coded, modulated, and demodulated using 8-PSK modulation. The loop simulates the communications system for Eb/N0 values in the range 0 dB to 12 dB in steps of 2 dB.

For each Eb/N0 value, the simulation stops when either the maximum number of errors (maxNumErrs) or the maximum number of bits (maxNumBits) processed by the bit error rate calculator System object is reached for the Gray coded bits.

EbNoVec = 0:2:12; % Eb/No values to simulate
SERVec = zeros(size(EbNoVec)); % Initialize SER history
BERVec = zeros(size(EbNoVec)); % Initialize BER history for Gray ordered
BERVecb = zeros(size(EbNoVec)); % Initialize BER history for binary ordered
for p = 1:length(EbNoVec)
  % Reset System objects
  awgnchan.EbNo = EbNoVec(p);
  % Reset SER / BER for the current Eb/No value
  SER = zeros(3,1);
  BER = zeros(3,1);
  while (BER(2)<maxNumErrs) && (BER(3)<maxNumBits)
    % Generate random data
    txSym = randi([0 M-1],SamplesPerFrame,1,'uint8');  
    txBits = reshape(de2bi(txSym,log2(M),'left-msb')',[],1); % Convert symbols to bits
    tx = pskmod(txBits);
    txb = pskmodb(txBits);
    rx = awgnchan(tx);
    rxb = awgnchan(txb);
    rxBits = pskdemod(rx);
    rxBitsb = pskdemodb(rxb);
    rxSym = bi2de(reshape(rxBits,log2(M),[])','left-msb');
    SER = symerror(txSym,rxSym); % Symbol error rate for Gray-coded data 
    BER = biterror(txBits,rxBits); % Bit error rate for Gray-coded data
    BERb = biterrorb(txBits,rxBitsb); % Bit error rate for natural binary-coded data
  % Save history of SER and BER values
  SERVec(p) = SER(1);
  BERVec(p) = BER(1);
  BERVecb(p) = BERb(1);

Restore the default stream.


Results Analysis

Analyze the data from the example and compare theoretical performance with simulation performance. The theoretical symbol error probability of MPSK is


where erfc is the complementary error function, ES/N0 is the ratio of energy in a symbol to noise power spectral density, and M is the number of symbols.

To determine the bit error probability, convert the symbol error probability, PE, to its bit error equivalent. There is no general formula for the symbol to bit error conversion. Nevertheless, upper and lower limits are easy to establish. The actual bit error probability, Pb, can be shown to be bounded by


The lower limit corresponds to the case where the symbols have undergone Gray coding. The upper limit corresponds to the case of pure binary coding.

Calculate theoretical error probabilities by using the use the berawgn function. Plot the simulated symbol error rate for Gray coding, bit error rate for Gray and natural binary coding, and the theoretical symbol error and bit error probabilities for Gray coding.

[theorBER,theorSER] = berawgn(EbNoVec,'psk',M,'nondiff');

semilogy(EbNoVec,SERVec,'o',EbNoVec,BERVecb,'x',EbNoVec,BERVec,'*', ...
legend('Symbol error rate','Bit error rate (Binary)','Bit error rate (Gray)', ...
          'Theoretical Symbol error rate','Theoretical Bit error rate', ...
xlabel('Eb/No (dB)'); ylabel('Error Probability');
title('Symbol and Bit Error Probability');
grid on;