%-------------------------------  Copyright ---------------------------------
%--   Copyright (C) 2005 Universite de Bretagne-Sud / LESTER
%--   Author : Christophe Cunat
%-------------------------------------------------------------------------------
%-- This program is free software; you can redistribute it and/or
%-- modify it under the terms of the GNU General Public License
%-- as published by the Free Software Foundation; either version 2
%-- of the License, or (at your option) any later version.
%
%-- This program is distributed in the hope that it will be useful,
%-- but WITHOUT ANY WARRANTY; without even the implied warranty of
%-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%-- GNU General Public License for more details.
%
%-- You should have received a copy of the GNU General Public License
%-- along with this program; if not, write to the Free Software
%-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%-------------------------------------------------------------------------------
% File : near_opt_bc
% Theorical computation of Probabilities of false alarm and nondetection
% for a binary correlator.
% Number of bits for correlation : N
%-------------------------------------------------------------------------------
clear all;
N = 64;

% Use computational function of P.D.F 
% c_a_ns : correlation_approximated_NoSignal (i.e. p = 0.5)
% c_o_ns : correlation_optimal_NoSignal      (i.e. p = 0.5)
[c_a_ns, c_o_ns] = P_distribution(0.5, N);

%%% Uncomment for displaying a simple difference example between nosignal
%%% and signal probability density functions
% [c_a_s, c_o_s] = P_distribution(0.1, N);
% figure(20);
% plot(c_o_ns, '-k');
% hold on;
% plot(c_a_ns, '--k');
% plot(c_a_s, ':k');
% plot(c_o_s, '-.k');
% xlabel('\fontname{cm} Correlation value');
% ylabel('\fontname{cm} Probability');
% set(gcf, 'color', 'white');
% set(gca, 'color', 'white');
% set(gcf, 'InvertHardCopy', 'on');
% set(gcf, 'Renderer', 'painter');
% textobj = findobj('type', 'text');
% hold off;
    

% Loop for correlation computation according to p 
k = 0;
variation = 0.01:0.005:0.5;
for p=variation
    k = k + 1;
    [c_a_s, c_o_s] = P_distribution(p, N);

    % Threshold computation
    % z is a temporary parameters useless in the following.
    [z, tp_a(k)]  = max(min(c_a_s, c_a_ns)) ;
    [z, tp_o(k)]  = max(min(c_o_s, c_o_ns));
    if (c_a_s(tp_a(k)) > c_a_ns(tp_a(k)))
        tp_a(k) = tp_a(k) - 1;
    end;
    if (c_o_s(tp_o(k)) > c_o_ns(tp_o(k)))
        tp_o(k) = tp_o(k) - 1;
    end;
    
    
    % Summation: Probability of nondetection de non détection
    prob_nd_a(k)     = sum(c_a_s(1: tp_a(k)));
    prob_nd_o(k)     = sum(c_o_s(1: tp_o(k)));
    % Summation: Probabilite of false alarm
    prob_fa_a(k)     = sum(c_a_ns(tp_a(k)+1 : length(c_a_ns)));
    prob_fa_o(k)     = sum(c_o_ns(tp_o(k)+1 : length(c_o_ns)));
end 

%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Displaying the results.
% Pd vs. Pfa
figure(8);
% P(true detection) = 1 - Pnd
plot(variation, 1 - prob_nd_o, '-', 'linewidth', 2, 'color', [.5 .5 .5]);
xlabel('Error probability (p)');
ylabel('Probability of false alarm and true detection');
hold on;
grid on;
plot(variation, prob_fa_o, '-', 'linewidth', 2, 'color', [.5 .5 .5]);

plot(variation, prob_fa_a, '-k');
plot(variation, 1 - prob_nd_a, '-k');
set(gcf, 'color', 'white');
set(gca, 'color', 'white');
set(gcf, 'InvertHardCopy', 'on');
set(gcf, 'Renderer', 'painter');
textobj = findobj('type', 'text');
hold off;

% Pfa in semilog graph
figure(3)
semilogy(variation, prob_fa_o, '-', 'linewidth', 2, 'color', [.5 .5 .5]);
hold on;
xlabel('error probability (p)');
ylabel('Pfa');
semilogy(variation, prob_fa_a, '-k');
grid on;
axis([0 0.5 10^-6 1]);
hold off;
set(gcf, 'color', 'white');
set(gca, 'color', 'white');
set(gcf, 'InvertHardCopy', 'on');
set(gcf, 'Renderer', 'painter');

