| Communications Toolbox | ![]() |
Below is a template that you can use when adapting your code to work with BERTool. You can open it in an editor by entering edit bertooltemplate in the MATLAB Command Window. The description in Understanding the Template explains the template's key sections, while Using the Template indicates how to use the template with your own simulation code. Alternatively, you can develop your simulation function without using the template, but be sure that it satisfies the requirements described in Requirements for Functions.
Note The template is not yet ready for use with BERTool. You must insert your own simulation code in the places marked INSERT YOUR CODE HERE. For a complete example based on this template, see Example: Preparing a Simulation Function for Use with BERTool. |
function [ber, numBits] = bertooltemplate(EbNo, maxNumErrs, maxNumBits)
% Import Java class for BERTool.
import com.mathworks.toolbox.comm.BERTool;
% Initialize variables related to exit criteria.
totErr = 0; % Number of errors observed
numBits = 0; % Number of bits processed
% --- Set up parameters. ---
% --- INSERT YOUR CODE HERE.
% Simulate until number of errors exceeds maxNumErrs
% or number of bits processed exceeds maxNumBits.
while((totErr < maxNumErrs) && (numBits < maxNumBits))
% Check if the user clicked the Stop button of BERTool.
if (BERTool.getSimulationStop)
break;
end
% --- Proceed with simulation.
% --- Be sure to update totErr and numBits.
% --- INSERT YOUR CODE HERE.
end % End of loop
% Compute the BER.
ber = totErr/numBits;
From studying the code in the function template above, you can observe how the function either satisfies the requirements listed in Requirements for Functions or indicates where your own insertions of code should do so. In particular,
The function has appropriate input and output arguments.
The function includes a placeholder for code that simulates a system for the given Eb/N0 value.
The function uses a loop structure to stop simulating when the number of errors exceeds maxNumErrs or the number of bits exceeds maxNumBits, whichever occurs first.
Note Although the while statement of the loop describes the exit criteria, your own code inserted into the section marked Proceed with simulation must compute the number of errors and the number of bits. If you do not perform these computations in your own code, then clicking Stop is the only way to terminate the loop. |
In each iteration of the loop, the function detects when the user clicks the Stop button in BERTool.
Here is a procedure for using the template with your own simulation code:
Determine the setup tasks that you must perform. For example, you might want to initialize variables containing the modulation alphabet size, filter coefficients, a convolutional coding trellis, or the states of a convolutional interleaver. Place the code for these setup tasks in the template section marked Set up parameters.
Determine the core simulation tasks, assuming that all setup work has already been performed. For example, these tasks might include error-control coding, modulation/demodulation, and channel modeling. Place the code for these core simulation tasks in the template section marked Proceed with simulation.
Also in the template section marked Proceed with simulation, include code that updates the values of totErr and numBits. The quantity totErr represents the number of errors observed so far. The quantity numBits represents the number of bits processed so far. The computations to update these variables depend on how your core simulation tasks work.
Note Updating the numbers of errors and bits is important for ensuring that the loop terminates. However, if you accidentally create an infinite loop early in your development work using the function template, then you can use the Stop button in BERTool to abort the simulation. |
Omit any setup code that initializes EbNo, maxNumErrs, or maxNumBits, because BERTool passes these quantities to the function as input arguments, after evaluating the data entered in the GUI.
Adjust your code or the template's code as necessary to use consistent variable names and meanings. For example, if your original code uses a variable called ebn0 and the template's function declaration (first line) uses the variable name EbNo, then you must change one of the names so that they match. As another example, if your original code uses SNR instead of Eb/N0, then you must convert quantities appropriately.
| Requirements for Functions | Example: Preparing a Simulation Function for Use with BERTool | ![]() |
© 1994-2005 The MathWorks, Inc.