Example: Curve Fitting for an Error Rate Plot

This example simulates a simple DBPSK (differential binary phase shift keying) communication system and plots error rate data for a series of Eb/N0 values. It uses the berfit function to fit a curve to the somewhat rough set of empirical error rates. Because the example is long, this discussion presents it in multiple steps:

Setting Up Parameters for the Simulation

The first step in the example is to set up parameters that will be used during the simulation. Parameters include the range of Eb/N0 values to consider and the minimum number of errors that must occur before the simulation computes an error rate for that Eb/N0 value.

% Set up initial parameters.
siglen = 1000; % Number of bits in each trial
M = 2; % DBPSK is binary.
EbNomin = 0; EbNomax = 10; % EbNo range, in dB
numerrmin = 5; % Compute BER only after 5 errors occur.
EbNovec = EbNomin:1:EbNomax; % Vector of EbNo values
numEbNos = length(EbNovec); % Number of EbNo values
% Preallocate space for certain data.
ber = zeros(1,numEbNos); % BER values
intv = cell(1,numEbNos); % Cell array of confidence intervals

Simulating the System Using a Loop

The next step in the example is to use a for loop to vary the Eb/N0 value (denoted by EbNo in the code) and simulate the communication system for each value. The inner while loop ensures that the simulation continues to use a given EbNo value until at least the predefined minimum number of errors has occurred. When the system is very noisy, this requires only one pass through the while loop, but in other cases, this requires multiple passes.

The communication system simulation uses these toolbox functions:

As the example progresses through the for loop, it collects data for later use in curve fitting and plotting:

% Loop over the vector of EbNo values.
for jj = 1:numEbNos
   EbNo = EbNovec(jj);
   snr = EbNo; % Because of binary modulation
   ntrials = 0; % Number of passes through the while loop below
   numerr = 0; % Number of errors for this EbNo value
   % Simulate until numerrmin errors occur.
   while (numerr < numerrmin)
      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
      numerr = numerr + newerrs; % Total errors for this EbNo value
      ntrials = ntrials + 1; % Update trial index.
   end
   % Error rate and 98% confidence interval for this EbNo value
   [ber(jj), intv1] = berconfint(numerr,(ntrials * siglen),.98);
   intv{jj} = intv1; % Store in cell array for later use.
   disp(['EbNo = ' num2str(EbNo) ' dB, ' num2str(numerr) ...
         ' errors, BER = ' num2str(ber(jj))])
end

This part of the example displays output in the Command Window as it progresses through the for loop. Your exact output might be different, because the example uses random numbers.

EbNo = 0 dB, 182 errors, BER = 0.182
EbNo = 1 dB, 156 errors, BER = 0.156
EbNo = 2 dB, 104 errors, BER = 0.104
EbNo = 3 dB, 66 errors, BER = 0.066
EbNo = 4 dB, 42 errors, BER = 0.042
EbNo = 5 dB, 27 errors, BER = 0.027
EbNo = 6 dB, 13 errors, BER = 0.0065
EbNo = 7 dB, 7 errors, BER = 0.007
EbNo = 8 dB, 5 errors, BER = 0.00125
EbNo = 9 dB, 5 errors, BER = 0.000625
EbNo = 10 dB, 5 errors, BER = 0.00041667

Plotting the Empirical Results and the Fitted Curve

The final part of this example fits a curve to the BER data collected from the simulation loop. It also plots error bars using the output from the berconfint function.

% Use BERFIT to plot the best fitted curve,
% interpolating to get a smooth plot.
fitEbNo = EbNomin:0.25:EbNomax; % Interpolation values
berfit(EbNovec,ber,fitEbNo);

% Also plot confidence intervals.
hold on;
for jj=1:numEbNos
   semilogy([EbNovec(jj) EbNovec(jj)],intv{jj},'g-+');
end
hold off;


© 1994-2005 The MathWorks, Inc.