| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Communications Toolbox |
| Contents | Index |
| Learn more about Communications Toolbox |
| On this page… |
|---|
The examples so far have performed tasks associated with various components of a communication system. In some cases, you might need to create a more sophisticated simulation that uses one or more of these techniques:
Looping over a set of values of a specific parameter, such as Eb/N0, the alphabet size, or the oversampling rate, so you can see the parameter's effect on the system
Processing data in multiple smaller sets rather than in one large set, to reduce the memory requirement
Dynamically determining how much data to process to get reliable results, instead of trying to guess at the beginning
This section discusses these issues and provides examples of constructs that you can use in your simulations of communication systems.
Communications Toolbox software includes a graphical user interface called BERTool. Using the BERTool GUI, you can solve problems like the following:
Problem Modify the modulation example in Modulating a Random Signal so that it computes the BER for integer values of EbNo between 0 and 7. Plot the BER as a function of EbNo using a logarithmic scale for the vertical axis. |
BERTool solves the problem by managing a series of simulations with different values of Eb/N0, collecting the results, and creating a plot. You provide the core of the simulation, which in this case is a minor modification of the example in Modulating a Random Signal.
This section introduces BERTool as well as some simulation-related issues, in these topics:
However, this section is not a comprehensive description of BERTool; for more information about BERTool, see BERTool: A Bit Error Rate Analysis GUI.
This solution uses code from commdoc_gray.m as well as code from a template file that is tailored for use with BERTool. To view the original code in an editor window, enter these commands in the MATLAB Command Window.
edit commdoc_gray edit bertooltemplate
To view a completed M-file for this example, enter edit commdoc_bertool in the MATLAB Command Window.
1. Save Template in Your Own Directory. Navigate to a directory where you want to save your own files. Save the BERTool template (bertooltemplate) under the filename my_commdoc_bertool to avoid overwriting the original template.
Also, change the first line of my_commdoc_bertool, which is the function declaration, to use the new filename.
function [ber, numBits] = my_commdoc_bertool(EbNo, maxNumErrs, maxNumBits)
2. Copy Setup Code Into Template. In the my_commdoc_bertool file, replace
% --- Set up parameters. --- % --- INSERT YOUR CODE HERE.
with the following setup code adapted from the example in commdoc_gray.m.
% Setup % Define parameters. M = 16; % Size of signal constellation k = log2(M); % Number of bits per symbol n = 1000; % Number of bits to process nsamp = 1; % Oversampling rate
To save time in the simulation, the code above changes the value of n from its original value. At small values of EbNo, it is not necessary to process tens of thousands of symbols to compute an accurate BER; at large values of EbNo, the loop structure in the template file (described later) causes the simulation to include at least 100 errors even if it must iterate several times through the loop to accumulate that many errors.
3. Copy Simulation Code Into Template. In the my_commdoc_bertool file, replace
% --- Proceed with simulation. % --- Be sure to update totErr and numBits. % --- INSERT YOUR CODE HERE.
with the rest of the code (that is, the code following the Setup section) from the example in commdoc_gray.m.
Also, type a semicolon at the end of the last line of the pasted code (the biterr command) to suppress screen output when BERTool runs the simulation.
4. Update numBits and totErr. After the pasted code from the last step and before the end statement from the template, insert the following code.
%% Update totErr and numBits. totErr = totErr + number_of_errors; numBits = numBits + n;
These commands enable the function to keep track of the number of bits processed and the number of errors detected.
5. Suppress Earlier Plots. Running multiple iterations would result in a large number of plots, which this example suppresses for simplicity. In the my_commdoc_bertool file, remove the lines of code that use these functions: stem, title, xlabel, ylabel, figure, scatterplot, hold, legend, axis.
6. Omit Direct Assignment of EbNo. When BERTool invokes a simulation function, it specifies a value of EbNo. The my_commdoc_bertool function must not directly assign EbNo. Therefore, remove or comment out the line that you pasted into my_commdoc_bertool (within the Channel section) that assigns EbNo directly.
% EbNo = 10; % In dB % COMMENT OUT FOR BERTOOL
7. Save Simulation Function. The simulation function, my_commdoc_bertool, is complete. Save the file so that BERTool can use it.
8. Open BERTool and Enter Parameters. To open BERTool, enter
bertool
in the MATLAB Command Window. Then click the Monte Carlo tab and enter parameters as shown below.

These parameters tell BERTool to run your simulation function, my_commdoc_bertool, for each value of EbNo in the vector 2:10 (that is, the vector [2 3 4 5 6 7 8 9 10]). Each time the simulation runs, it continues processing data until it detects 100 bit errors or processes a total of 1e8 bits, whichever occurs first.
9. Use BERTool to Simulate and Plot. Click the Run button on BERTool. BERTool begins the series of simulations and eventually reports the results to you in a plot like the one below.

To compare these BER results with theoretical results, leave BERTool open and use the procedure below.
To check whether the results from the solution above are correct, use BERTool again. This time, use its Theoretical panel to plot theoretical BER results in the same window as the simulation results from before. Follow this procedure:
In the BERTool GUI, click the Theoretical tab and enter parameters as shown below.

The parameters tell BERTool to compute theoretical BER results for 16-QAM over an AWGN channel, for Eb/N0 values in the vector 2:10.
Click the Plot button. The resulting plot shows a solid curve for the theoretical BER results and plotting markers for the earlier simulation results.

Notice that the plotting markers are close to the theoretical curve. It is relevant that the simulation code used a Gray-coded signal constellation, unlike the first modulation example of this chapter (in Modulating a Random Signal). The theoretical performance results assume a Gray-coded signal constellation.
To continue exploring BERTool, you can select the Fit check box to fit a curve to the simulation data, or set Confidence Level to a numerical value to include confidence intervals in the plot. See also BERTool: A Bit Error Rate Analysis GUI for more about BERTool.
Looking more closely at the simulation function in this example, you might make a few observations about its structure, and particularly about the loop marked with the comments
% Simulate until number of errors exceeds maxNumErrs % or number of bits processed exceeds maxNumBits.
The loop structure means that the simulation processes some data, accumulates bit errors, and then decides whether to repeat the process with another set of data. The advantage of this approach is that you do not have to guess in advance how much data you need to process to obtain an accurate BER estimate. This is very useful when your series of simulations spans a large Eb/N0 range because simulations at higher values of Eb/N0 require more data processing to maintain the same level of accuracy in the BER estimate. Another advantage of this approach is that you avoid memory problems caused by excessively large data sets.
However, a potential complication from dividing large data sets into a series of smaller data sets that you process in a loop is that you might need to take steps to ensure the continuity of computations from one iteration to the next. For example, continuity is important when the simulation includes convolutional decoding, convolutional interleaving/deinterleaving, continuous phase modulation, fading channels, and equalization. To learn more about how to maintain continuity, see the examples in
The vitdec reference page
The viterbisim demonstration function (designed to be used with BERTool)
The muxdeintrlv reference page
The mskdemod reference page
If you divide your data set into a series of very small data sets, then the large number of function calls might make the simulation slow. You can use the Profiler tool in MATLAB to help you make your code faster.
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 fromcommdoc_gray.m 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 practice 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 shown in the following figure.

![]() | Studying Components of a Communication System | Running Simulations Using the Error Rate Test Console | ![]() |

Learn how to apply early verification to your development process through these technical resources.
How much time do you spend on testing to ensure implementation meets system-level requirements?
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |