%% ===========================================================================
% Copyright (C) 2000 by Forschungszentrum Telekommunikation Wien, Austria;
%                                                     All rights reserved. 
%
% Description   : Reach calculation for a SDSL modem in a given scenario 
%                 (only one cable type between CO and a single NT point) 
%                 
% Project       : B1, FTW
%
% Author(s)     : Tomas Nordstrom (Tomas.Nordstrom@FTW.at)
%               : Gernot Schmid (gernot.schmid@arcs.ac.at)
%
%% ===========================================================================

%% ===========================================================================
% Change History
%      2000-11-08 (GS)   Created
%% ===========================================================================
clear all;
global ex;
global res;

% Test if we have problem reading numbers in international OS versions
if 3 == 3.14,
    error(['Matlab thinks 3 is equal to 3.14, ' ...
           'please use an US (or posix C) version of the system. ' ...
           'Or add LC_NUMERIC=us into $MATLAB/bin/.matlab5rc.sh' ]);
end

ex.param    = setupParam;  % Basic default simulation Parameter

ex.tfplist  = itu_tfplanHAM([]);                % Need a HAM band definition
ex.tfplist  = fsan_tfplansMISC(ex.tfplist);     % Get plans for alien noise
ex.tfplist  = fsan_noise(ex.tfplist);
ex.tfplist  = etsi_tfplansVDSL(ex.tfplist);     % Get plan VDSL-FDD
ex.tfplist  = etsi_tfplansSDSL(ex.tfplist);

ex.lclist   = setupLClist;                      % Line code definition (Theo.)
ex.lclist   = fsan_lcdefs(ex.lclist);           % Line code definitions (ADSL)
ex.lclist   = etsi_lcdefsSDSL(ex.lclist);       % Line code definitions (SDSL)

ex.clist    = etsi_cables([]);                  % Get ETSI (and ANSI) cables;

% Determines how the generic name ADSL is mapped to a specific linecode
xDSL.name='ADSL';     
xDSL.used='ADSL';
ex.param.xDSLlist=insertList(ex.param.xDSLlist,xDSL);

gui.ttlist  = fsan_loops([]);                   % Def. of some FSAN Topologies
gui.ttlist  = UserLoopsSDSL(gui.ttlist);  % Def. of some Example Loops

ex.param.FSANNoiseModel='Calculate';            % Use predefined noise models or 'Calculate'

ex.param.modemlist=str2mat('SDSL-sym-1024');    % Considered modem

scenario='SDSL-Scenario 0';                     % Scenario to be investigated

% Margin to be met (default: 6.0 dB + 1.6 dB for implementation loss) 
target_margin=7.6; 

start_len=5000;               % Start length for Loop length iteration in meter

%===================== End of Set up Parameter section =======================


ex.tt       = getList(gui.ttlist,scenario);     % Get the scenario

% Check if the scenario is really a 'single-cable-loop'
[n,m]=size(ex.tt.topology);
if n~=2
    message=sprintf('Only one cable between CO and one NT point are allowed');
    error(message);
end

% Create needed linecodes and tfplans for all SDSL services in the scenario
ETSI_SDSL_traffic2lctf;

% Prepare the search
h = waitbar(0,'Reach Computation in Progress, Please wait...');
len=start_len;    
ex.tt.topology{2,1}=len;
result = evalExperiment;
[bitrate_LT, bitrate_NT, margin_LT, margin_NT] = calcFSANresult(ex,result);
margin=min(margin_LT,margin_NT);
delta=margin-target_margin;

% Search for the reach
quitloop=0;
cnt=1;
waitbar(10*cnt/100,h);
while quitloop==0   
    cnt=cnt+1;
    if abs(delta)<0.05    % Acceptable margin-accuracy of +- 0.05 dB 
        quitloop=1;
    else
        if (abs(delta)>0.05) & (quitloop==0)
            segs = size(ex.tt.topology);
            Att  = getAtt(ex,1,segs(1),ex.tt.topology,ex.param.frequency.f,ex.param.Zterm);
            IL   = -Att(find(ex.param.frequency.f==150e3));
            ILpLL=IL/len;
            len=len+delta/ILpLL;  
            ex.tt.topology{2,1}=len;
            result = evalExperiment;
            [bitrate_LT, bitrate_NT, margin_LT, margin_NT] = calcFSANresult(ex,result);
            margin=min([margin_NT margin_LT]);
            delta=margin-target_margin;
        end  
    end  
    waitbar(10*cnt/100,h);
    
end %while  
close(h); 

% Show the reach in the topology structure

ex.tt.topology{2,4}=cellstr(['<--  max. length: ' ...
                    (num2str(round(len))) ' m  -->']);
figure; 
plotTTstructure(ex.tt);
drawnow; % Show it now
text(1,170,['Reach for ' ex.param.modemlist  ...
            ' on cable ' ex.tt.topology{2,2}...
            ' for ' num2str(target_margin) 'dB target margin']);
% Show the result in the command window
fprintf('\n\nMaximum Reach for %14s from CO to NT\n', ex.param.modemlist);
fprintf('Maximum Reach = : %g Meter (target margin:%2.2fdB)\n\n',len,target_margin);