## 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.

### Introduction

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
0000exp(0)
0011exp(jπ/4)
0103exp(j3π/4)
0112exp(jπ/2) = exp(j2π/4)
1007exp(j7π/4)
1016exp(j3π/2) = exp(j6π/4)
1104exp() = exp(j4π/4)
1115exp(j5π/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(0)000
exp(/4)001
exp(/2) = exp(j2π/4)011
exp(j3π/4)010
exp() = exp(j4π/4)110
exp(j5π/4)111
exp(j3π/2) = exp(j6π/4)101
exp(j7π/4)100

### 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, ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$, 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 ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ point.

Initialization

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;
maxNumErrs=100;
maxNumBits=1e8;

prevState = rng;
rng(529558);

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', ...
'PhaseOffset',0,'BitInput',true);
pskdemod = comm.PSKDemodulator('ModulationOrder',M,'SymbolMapping','Gray', ...
'PhaseOffset',0,'BitOutput',true,'OutputDataType','uint8', ...
'DecisionMethod','Hard decision');
pskmodb = comm.PSKModulator('ModulationOrder',M,'SymbolMapping','Binary', ...
'PhaseOffset',0,'BitInput',true);
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 ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ 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)', ...
'BitsPerSymbol',log2(M),'SignalPower',1);

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 ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values in the range 0 dB to 12 dB in steps of 2 dB.

For each ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ 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
reset(symerror);
reset(biterror);
reset(biterrorb);
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
end
% Save history of SER and BER values
SERVec(p) = SER(1);
BERVec(p) = BER(1);
BERVecb(p) = BERb(1);
end

Restore the default stream.

rng(prevState)

Results Analysis

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

${\mathit{P}}_{\mathrm{E}}\left(\mathit{M}\right)=\mathrm{erfc}\left(\sqrt{\frac{{\mathit{E}}_{\mathit{S}}}{{\mathit{N}}_{0}}}\mathrm{sin}\left(\frac{\pi }{\mathit{M}}\right)\right)$

where erfc is the complementary error function, ${\mathit{E}}_{\mathit{S}}/{\mathit{N}}_{0}$ 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, ${\mathit{P}}_{\mathrm{E}}$, 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, ${\mathit{P}}_{\mathit{b}}$, can be shown to be bounded by

$\frac{{\mathit{P}}_{\mathrm{E}}\left(\mathit{M}\right)}{{\mathrm{log}}_{2}\mathit{M}}\le {\mathit{P}}_{\mathit{b}}\le \frac{\mathit{M}/2}{\mathit{M}-1}{\mathit{P}}_{\mathrm{E}}\left(\mathit{M}\right)$

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');

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