Main Content

Symbol Mapping Examples

Compare Error Rate for Gray- and Binary-Coded Ordering

Compare Gray coding with 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.

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
sps = 10000;    % Samples per frame
maxNumErrs=100; % Stop simulation if 100 errors reached
maxNumBits=1e8; % Stop simulation if 1e8 bits transmitted

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 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)", ...
    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 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)); % SER history
BERVec = zeros(size(EbNoVec)); % BER history for Gray ordered
BERVecb = zeros(size(EbNoVec)); % 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 and 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],sps,1,"uint8");
    txBits = int2bit(txSym,log2(M),true); % Convert symbols to bits
    tx = pskmod(txBits);
    txb = pskmodb(txBits);
    rx = awgnchan(tx);
    rxb = awgnchan(txb);
    rxBits = pskdemod(rx);
    rxBitsb = pskdemodb(rxb);
    rxSym = bit2int(rxBits,log2(M),true);
    SER = symerror(txSym,rxSym); % SER for Gray-coded data 
    BER = biterror(txBits,rxBits); % BER for Gray-coded data
    BERb = biterrorb(txBits,rxBitsb); % BER for 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. Calculate theoretical error probabilities by using the berawgn function. Plot the simulated symbol error rate for Gray coding, bit error rate for Gray and 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;

Gray-Coded M-PSK Modulation Error Rate in AWGN Channel Using Simulink

This example uses the doc_gray_code to compute bit error rates (BER) and symbol error rates (SER) for M-PSK modulation. The theoretical error rate performance of M-PSK modulation in AWGN is compared to the error rate performance for Gray-coded symbol mapping and to the error rate performance of binary-coded symbol mapping.

The Random Integer Generator block serves as the source, producing a sequence of integers. The Integer to Bit Converter block converts each integer into a corresponding binary representation. The M-PSK Modulator Baseband block in the doc_gray_code model:

  • Accepts binary-valued inputs that represent integers in the range [0, (M - 1], where M is the modulation order.

  • Maps binary representations to constellation points using a Gray-coded ordering.

  • Produces unit-magnitude complex phasor outputs, with evenly spaced phases in the range [0, (2 $\pi$ (M - 1) / M)].

The AWGN Channel block adds white Gaussian noise to the modulated data. The M-PSK Demodulator Baseband block demodulates the noisy data. The Bit to Integer Converter block converts each binary representation to a corresponding integer. Then two separate Error Rate Calculation blocks calculate the error rates of the demodulated data. The block labeled SER Calculation compares the integer data to compute the symbol error rate statistics and the block labeled BER Calculation compares the bits data to compute the bit error rate statistics. The output of the Error Rate Calculation block is a three-element vector containing the calculated error rate, the number of errors observed, and the amount of data processed.

To reduce simulation run time and ensure that the statistics of the errors remain stable as the Eb/N0 ratio increases, the model is configured to run until 100 errors occur or until 1e8 bits have been transmitted.

The model initializes variables used to configure block parameters by using the PreLoadFcn callback function. For more information, see Model Callbacks (Simulink).

Produce Error Rate Curves

Compute the theoretical BER for nondifferential 8-PSK in AWGN over a range of Eb/N0 values by using the berawgn function. Simulate the doc_gray_code model with Gray-coded symbol mapping over the same range of Eb/N0 values.

Compare Gray coding with binary coding, by modifying the M-PSK Modulator Baseband and M-PSK Demodulator Baseband blocks to set the Constellation ordering parameter to Binary instead of Gray. Simulate the doc_gray_code model with binary-coded symbol mapping over the same range of Eb/N0 values.

Plot the results by using the semilogy function. The Gray-coded system achieves better error rate performance than the binary-coded system. Further, the Gray-coded error rate aligns with the theoretical error rate statistics.

Gray Encode Modulated Signal

For the PSK, DPSK, FSK, QAM, and PAM modulation types, Gray constellations are obtained by setting the symbol mapping to Gray-encoding in the corresponding modulation function, System object™, or block. The default symbol order uses Gray-encoded ordering for modulation.

For modulation functions, you can specify "gray", "bin", or a vector specifying custom symbol ordering. This example uses the qammod function with Gray-encoded symbol mapping. Check the constellation plot, the modulated symbols are Gray-encoded because all adjacent elements differ by only one bit.

k = 4;
M = 2^k;
y = int2bit([0:M-1]',k,false);
symorder = "gray";
xmap = qammod(y,M,symorder, ...
    InputType="bit", ...
    PlotConstellation=true);

Related Topics