%% ===========================================================================
% Copyright (C) 1998,99 by Telia Research AB, Lulea, Sweden; All rights reserved. 
% 
% Description   : Main simulation program, example of local defintions
%
% File          : ExMain.m
% Project       : FSAN duplex model
% Author(s)     : Tomas Nordstrom (Tomas.Nordstrom@FTW.at)
%               : Daniel Bengtsson (Daniel.J.Bengtsson@Telia.se) 
%
% CVS:       $Id: ExMain.m,v 1.2 2000/04/06 13:12:54 uvan Exp $
%% ===========================================================================

%% ===========================================================================
% Change History
%      1998-11-13 (ToNo) Created
%      1998-12-21 (ToNo) Updated to get in sync with uiMain
%      1998-01-07 (ToNo) Removed modem under test
%      1998-01-14 (ToNo) Updated to get in sync with uiMain
%      1999-02-05 (DaB)  Updated to fit new param structure
%      1999-02-25 (ToNo) Generlized list (tfplist) handling
%      1999-02-26 (ToNo) Split out definitions into userDefinitionsExample1
%      1999-03-25 (ToNo) Restructured the source (moved into subdir xdslcomm)
%      2000-04-05 (UvAn) New return value from itu_tfplanHAM
%% ===========================================================================
global ex;
global res;
global gui;

% Set up default experiment structures
ex.param    = setupParam;                       % Parameters

[ex.tfplist, ex.param.HAMBandName] = ...
	itu_tfplanHAM([]);                % Need a HAM band definition
ex.tfplist  = fsan_tfplansMISC(ex.tfplist);      % Get plans for alien noise

ex.lclist   = setupLClist;                      % Line code definition (theoretical)
ex.lclist   = fsan_lcdefs(ex.lclist);           % Line code definitions (ADSL)
ex.clist    = etsi_cables([]);                  % Get ETSI (and ANSI) cables;

userDefinitionsExample1;

% Show traffic and topology structure
figure(1);
plotTTstructure(ex.tt);

% Show PSD masks for VDSL modem
tfplan = getList(ex.tfplist,gui.vdslDuplex);
figure(2);
gui.fax.min=1e3; gui.fax.max=12e6;
plotTFplan(tfplan,'Lin',gui.fax);

drawnow; % Show it now

% Evaluate this experiment
result = evalExperiment;

% Show the result from the experiment (Note: Mixed ADSL and VDSL results)
format compact
[bitrate_LT, bitrate_NT]=calcFSANresult(ex,result); 

sprintf('LT Rates')
bitrate_LT
sprintf('NT Rates')
bitrate_NT
sprintf('Ratio')
bitrate_NT./bitrate_LT

% Plot the results from experiment 
for current=1:length(result),
    figure;
    tmp_str=sprintf('FSAN xDSL Simulation Result, Modem %d (%s-%s)',...
        current, ex.tt.topology{result(current).Modem.LT_Node,3}, ...
        ex.tt.topology{result(current).Modem.NT_Node,3});
    set(gcf,'NumberTitle','off','Name',tmp_str);

    % Plot the LT side
    subplot(211)
    plotResult(ex,result,current,'LT');

    % Plot the NT side
    subplot(212)
    plotResult(ex,result,current,'NT');
end
% or do uiResults(ex,result) to get the gui interface for the results