Template for a Simulation Function

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.

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;

Understanding the Template

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,

Using the Template

Here is a procedure for using the template with your own simulation code:

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

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

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

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

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


© 1994-2005 The MathWorks, Inc.