function probErr = vitproberrdemo(BER, dist, numErr)
%VITPROBERRDEMO calculates the theoretical probability of error VITSIMDEMO
%
%   VITPROBERRDEMO(BER, DIST, NUMERR) calculated the probability of error for
%   convolutional codes with hard decision decoding.  BER is a vector containing
%   raw channel bit error rates.  DIST is a vector of distances for error paths
%   in the trellis.  NUMERR is a vector containing the number of errors that
%   occurs in each of the paths in the trellis. The performance calculated is the
%   Union Bound on the decoder performance.

%   Copyright 1996-2000 The MathWorks, Inc.
%   $Revision: 1.1 $ $Date: 2000/08/30 21:03:52 $


probErr = 0;
for idx = [1:length(dist)]
    probErr = probErr + bernex(BER, dist(idx), dist(idx)/2, dist(idx)) .* numErr(idx);
end



% Helper function to calculate bernoulli's trials

function sumProb = bernex(p, n, low_k, high_k)

if(nargin < 4)
    high_k = low_k;
end

if((low_k < 0) | (low_k > n))
    error('low_k must be between 0 and N')
end
if((high_k < 0) | (high_k > n))
    error('high_k must be between 0 and N')
end

sumProb = 0;
for idx = [low_k+1: high_k+1]

    k = idx-1;
    prod = nchoosek(n, k) .* (p .^ k) .* ((1-p) .^ (n-k));
    sumProb = sumProb + prod;
end

