| Communications Toolbox | ![]() |
A common task in analyzing a communication system is to vary a parameter, possibly a parameter other than Eb/N0, and find out how the system responds. This section addresses the following problem:
Problem Modify the modulation example in Modulating a Random Signal so that it computes the BER for alphabet sizes (M) of 4, 8, 16, and 32 and for integer values of EbNo between 0 and 7. For each value of M, plot the BER as a function of EbNo using a logarithmic scale for the vertical axis. |
The earlier section (Modulating a Random Signal) presented a model of the system that computes the BER for specific values of M and EbNo. Therefore, the only remaining task is to vary M and EbNo and collect multiple error rates. For simplicity, this solution uses the same number of bits for each value of M and EbNo, unlike the example in Using BERTool to Run Simulations.
This solution modifies the code from Modulating a Random Signal by introducing and exploiting a nested loop structure. To view the original code in an editor window, enter the following command in the MATLAB Command Window.
edit commdoc_mod
To view a completed M-file for this example, enter edit commdoc_mcurves in the MATLAB Command Window.
1. Define the Set of Values for the Parameter. At the beginning of the script, introduce variables that list all the values of M and EbNo that the problem requires. Also, preallocate space for error statistics corresponding to each combination of M and EbNo.
%% Ranges of Variables Mvec = [4 8 16 32]; % Values of M to consider EbNovec = [0:7]; % Values of EbNo to consider %% Preallocate space for results. number_of_errors = zeros(length(Mvec),length(EbNovec)); bit_error_rate = zeros(length(Mvec),length(EbNovec));
2. Introduce a Loop Structure. After Mvec and EbNovec are defined and space is preallocated for statistics, all the subsequent commands can go inside a loop, as illustrated below.
%% Simulation loops
for idxM = 1:length(Mvec)
for idxEbNo = 1:length(EbNovec)
% OTHER COMMANDS
end % End of loop over EbNo values
end % End of loop over M values
3. Inside the Loop, Parameterize as Appropriate. The M-code from specifies fixed values of M and EbNo, while this problem requires using a different value for each iteration of the loop. Therefore, change the definitions of M (within the Setup section) and EbNo (within the Channel section) as follows.
M = Mvec(idxM); % Size of signal constellation
EbNo = EbNovec(idxEbNo); % In dB
Also, the original M-code returns scalar values for the BER and number of errors, while it makes sense in this case to save the whole array of error statistics instead of overwriting the variables in each iteration. Therefore, replace the BER Computation section with the following.
%% BER Computation % Compare x and z to obtain the number of errors and % the bit error rate. [number_of_errors(idxM,idxEbNo),bit_error_rate(idxM,idxEbNo)] = ... biterr(x,z);
Note An earlier step preallocated space for the matrices number_of_errors and bit_error_rate. While not strictly necessary, this is a better MATLAB programming habit than expanding the matrices' size in each iteration. To learn more, see Preallocating Arrays in the MATLAB documentation set. |
4. Suppress Earlier Plots. Running multiple iterations would result in a large number of plots, which this example suppresses for simplicity. Remove the lines of code that use these functions:stem, title, xlabel, ylabel, figure, scatterplot, hold, legend, axis.
5. Create BER Plot. The semilogy function in MATLAB creates a plot with a logarithmic scale in the vertical axis. The following commands, placed just before the end of the loop over M values, create the desired BER plot curve by curve during the simulation.
%% Plot a Curve. markerchoice = '.xo*'; plotsym = [markerchoice(idxM) '-']; % Plotting style for this curve semilogy(EbNovec,bit_error_rate(idxM,:),plotsym); % Plot one curve. drawnow; % Update the plot instead of waiting until the end. hold on; % Make sure next iteration does not remove this curve.
You might also want to customize the plot at the end by adding this code after the end of both loops.
%% Complete the plot.
title('Performance of M-QAM for Varying M');
xlabel('EbNo (dB)'); ylabel('BER');
legend('M = 4','M = 8','M = 16','M = 32',...
'Location','SouthWest');
6. Run the Entire Script. The script creates a plot like the one below.

| Using BERTool to Run Simulations | Learning More | ![]() |
© 1994-2005 The MathWorks, Inc.