Example: Preparing a Simulation Function for Use with BERTool

This section adapts the function template given in Template for a Simulation Function to use simulation code from the documentation example in Example: Curve Fitting for an Error Rate Plot.

Preparing the Function

To prepare the function for use with BERTool, follow these steps:

  1. Copy the template from Template for a Simulation Function into a new M-file in the MATLAB Editor. Save it in a directory on your MATLAB path, using the filename bertool_simfcn.

  2. From the original example, the following lines are setup tasks. They are modified from the original example to rely on the input arguments that BERTool provides to the function, instead of defining variables such as EbNovec and numerrmin directly.

    % Set up initial parameters.
    siglen = 1000; % Number of bits in each trial
    M = 2; % DBPSK is binary.
    snr = EbNo; % Because of binary modulation
    ntrials = 0; % Number of passes through the loop

    Place these lines of code in the template section marked Set up parameters.

  3. From the original example, the following lines are the core simulation tasks, after all setup work has been performed.

    msg = randint(siglen, 1, M); % Generate message sequence.
    txsig = dpskmod(msg,M); % Modulate.
    rxsig = awgn(txsig, snr, 'measured'); % Add noise.
    decodmsg = dpskdemod(rxsig,M); % Demodulate.
    newerrs = biterr(msg,decodmsg); % Errors in this trial
    ntrials = ntrials + 1; % Update trial index.

    Place the code for these core simulation tasks in the template section marked Proceed with simulation.

  4. Also in the template section marked Proceed with simulation (after the code from the previous step), include the following new lines of code that update the values of totErr and numBits.

    % Update the total number of errors.
    totErr = totErr + newerrs;
    
    % Update the total number of bits processed.
    numBits = ntrials * siglen;

The bertool_simfcn function is now compatible with BERTool. Note that unlike the original example, the function here does not initialize EbNovec, define EbNo as a scalar, or use numerrmin as the target number of errors; this is because BERTool provides input arguments for similar quantities. The bertool_simfcn function also excludes code related to plotting, curve fitting, and confidence intervals in the original example because BERTool enables you to do similar tasks interactively without writing code.

Using the Prepared Function

To use bertool_simfcn in conjunction with BERTool, continue the example by following these steps:

  1. Open BERTool and go to the Monte Carlo panel.

  2. Set parameters on the Monte Carlo panel as shown below.

  3. Click Run.

    BERTool spends some time computing results and then plots them. Note that they do not appear to fall along a smooth curve because the simulation required only five errors for each value in EbNo.

  4. To fit a curve to the series of points in the BER Figure window, check the box next to Fit in the data viewer.

    BERTool plots the curve, as below.


© 1994-2005 The MathWorks, Inc.