function plotTFplanDMT(tfplan,ftype,fax,usc,dsc);
%plotTFplan - plots a FSAN scenario time and frequency plan
%
% Parameter:    tfplan.name         	The name of this plan
% Parameter:    tfplan.upstream	    	The upstrean frequency plan
% Parameter:    tfplan.downstream   	The upstrean frequency plan
% Parameter:    tfplan.interpolation    The interpolation method used
% Parameter:    ftype			The type of frequency axis ('Log','Linear')
% Parameter:    fax.min			The min frequency to plot
% Parameter:    fax.max			The max frequency to plot
%
% Global parameters used:
%	ex.param.f		 	Frequency plan
%       ex.param.HAM                    If HAMband should be enforced
%
% Example(s):
%   plotTFplan(tfplan) allows you to plot the frequency plan
%
% Reference:
%    FSAN xDSL simulation tool manual
%% ===========================================================================

%% ===========================================================================
% Copyright (C):                                        
%       1998-1999 by Telia Research AB, Lulea, Sweden;                
%       2000-2001 by Forschungszentrum Telekommunikation Wien, Austria;
%                                                         All rights reserved.
% Project       : FSAN duplex model
% Author(s)     : Tomas Nordstrom (Tomas.Nordstrom@FTW.at)
%               : Daniel Bengtsson (Daniel.J.Bengtsson@Telia.se)
%
% CVS:       $Id: plotTFplanDMT.m,v 1.1 2001/09/04 18:34:17 tono Exp $
%% ===========================================================================
% Change History
%      1998-11-12 (ToNo) Created
%      1999-01-08 (ToNo) Added fill
%      1999-01-14 (ToNo) Fixed support for local frequency axis (fax)
%      1999-01-15 (ToNo) Added legend the hard way
%      1999-01-17 (ToNo) Fixed a plot of the whole defined frequency axis
%      1999-02-08 (DaB)  updated to fit new struct
%      1999-02-09 (DaB)  removed local freq axis
%      1999-03-01 (DaB)  Updated to fit new HAM band defintion
%      1999-08-19 (DaB)  updated for new tfplan struct
%      1999-10-01 (DaB)  Rewritten for new ex struct
%      2001-07-05 (ToNo) Now we support two-sided colored background noise
%      2001-08-05 (ToNo) Add support to plot the tones used in DMT
%% ===========================================================================

global ex;
if nargin<1,
        error('Not enough input arguments.');
end

if nargin<2,
    ftype='Linear';
end

if nargin<3,
    fax.min=min(ex.param.frequency.f);
    fax.max=max([tfplan.PSD.active.upstream tfplan.PSD.active.downstream])+1e6;
end

% Set up a local frequency axis (with a suitable f max)
f=ex.param.frequency.f;

df		= 4.3125e3;      % Carrier spacing 
cval=df:df:df*255;

% Set up the data
upPSD 	= eval(tfplan.PSD.upstream);
downPSD = eval(tfplan.PSD.downstream);

ex.param.frequency.f=cval;
upCPSD 	= eval(tfplan.PSD.upstream);
downCPSD = eval(tfplan.PSD.downstream);
% log scale
upCPSD=10*log10(upCPSD);
downCPSD=10*log10(downCPSD);

ex.param.frequency.f=f;

% remove hamband
if(tfplan.PSD.HAM.active == 1)
    hamband = getList(ex.tfplist,ex.param.HAMBandName);
    hamPSD  = eval(hamband.PSD.downstream);
    upPSD   = min(upPSD,hamPSD);
    downPSD = min(downPSD,hamPSD);
end

% log scale
upPSD=10*log10(upPSD);
downPSD=10*log10(downPSD);

maxval = max([upPSD downPSD]);
minval = min([upPSD downPSD]);


% Plot it
uc=[0.7 0.7 1];
dc=[1 0.7 0.7];
cla;
if strcmp(ftype,'Log')
    semilogx(f,upPSD,'b',f,downPSD,'r--');
    hold on;
    semilogx(cval(usc),upCPSD(usc),'b*',cval(dsc),downCPSD(dsc),'r+');
else
    fill(f,upPSD,uc,f,downPSD,dc);
    hold on;
    plot(f,upPSD,'b',f,downPSD,'r--');
    plot(cval(usc),upCPSD(usc),'b*',cval(dsc),downCPSD(dsc),'r+');
end

titlestring=sprintf('%s PSD mask',tfplan.name);
title(titlestring);
lowest=min([eval(ex.param.bgNoise.LT) eval(ex.param.bgNoise.NT)]);
axvec=[fax.min fax.max lowest -35];
axis(axvec);
xlabel('Frequency');
% ylabel('PSD (dbM)');
grid on;

%legend('upstream','downstream',4) is not working, thus, this:
% Plot legend the hard way
curax=get(gca,'Position') ;
startx = curax(1)+curax(3)-120;
starty=curax(2);
h1 = axes('Box','on', 'CameraUpVector',[0 1 0], 'CLimMode','manual', ...
    'Color',[1 1 1], 'DrawMode','fast', ...
    'FontName','times', 'FontSize',12, 'NextPlot','add', ...
    'Units','pixels', 'Position',[startx starty 120 40], ...
    'XColor',[0 0 0], 'XLimMode','manual', 'XTick',-1, ...
    'XTickLabelMode','manual', 'XTickMode','manual', ...
    'YColor',[0 0 0], 'YLimMode','manual', 'YTick',-1, ...
    'YTickLabelMode','manual', 'YTickMode','manual', 'ZColor',[0 0 0]);

h2 = text('Color',[0 0 0], 'FontName','times', 'FontSize',11, ...
    'Position',[0.4 0.66 0], 'String','upstream');
box1 = [0.0605 0.8333; 0.3026 0.8333; 0.3026 0.5000; 0.0605 0.5000; 0.0605 0.8333];
h2 = patch('FaceColor',uc, 'Faces',[1 2 3 4 5], 'Vertices',box1);

h2 = text('Color',[0 0 0], 'FontName','times', 'FontSize',11, ...
    'Position',[0.4 0.333 0], 'String','downstream');
box2 =[0.0605 0.5000; 0.3026 0.5000; 0.3026 0.1667; 0.0605 0.1667; 0.0605 0.5000];
h2 = patch('FaceColor',dc, 'Faces',[1 2 3 4 5], 'Vertices',box2);

hold off;