function fqstruct=planConvertion(frequencies,directions,guardband);
%% ===========================================================================
%planConvertion - Convert a frequency plan into centre frequencies and bw
%
% Parameter:    frequencies	Array of switching frequencies
% Parameter:    directions	Direction at these frequencies
% Parameter:    guardband	Size (0-1) of guardbands
% Returns:      fqstruct	Frequency and bw struct
%			.up.fc  	Center frequencies for upstream
%			.up.bw  	Bandwidth for each upstream band
%			.down.fc  	Center frequencies for downstream
%			.down.bw  	Bandwidth for each downstream band
%
% Example(s):
%   fqstruct=planConvertion([.138e6 2.6e6 5.4e6 10.25e6  20e6],'dudu')
%   Defines a four band plan
%
%   fqstruct=planConvertion([.138e6 2.6e6 5.4e6 10.25e6 15e6 20e6],'dudnu')
%   Defines a four band plan with a gap between 10.25 and 15 MHz
%
%   fqstruct=planConvertion([.138e6 2.6e6 5.4e6 10.25e6 15e6 20e6],'dudud',0.1)
%   Defines a five band plan with guard bands of 10%
%
%% ===========================================================================

%% ===========================================================================
% Copyright (C) 1999 by Telia Research AB, Lulea, Sweden; All rights reserved. 
% Project       : FSAN duplex model
% Author(s)     : Tomas Nordstrom (Tomas.Nordstrom@FTW.at)
%               : Daniel Bengtsson (Daniel.J.Bengtsson@Telia.se)
%
% CVS:       $Id: planConvertion.m,v 1.6 2000/03/10 13:45:59 tono Exp $
%% ===========================================================================
% Change History
%      1999-10-10 (ToNo) Created
%      1999-11-02 (ToNo) Modified the way guard bands is defined
%      2000-03-09 (ToNo) Fixed a mixup in ui/di index
%% ===========================================================================

if nargin<2,
    error('Not enough input arguments.');
end

if nargin<3
   guardband = 0;
end;

if length(frequencies) ~= length(directions)+1,
    error('planConvertion: length missmatch');
end;

ui=1;
di=1;
endi=length(frequencies);
i=1;
while i <= length(directions),
    stopi=i+1;
    while (stopi<endi) & (directions(i) == directions(stopi)),
        stopi=stopi+1;
    end;

    % define guard bands based on switch freqencies
    gbl = frequencies(i)*guardband/2;                        % guard band low
    gbh = frequencies(stopi)*guardband/2;                    % guard band high
    bw  = (frequencies(stopi) - frequencies(i) - gbl - gbh); % bandwidth of band
    cfq = (frequencies(i) + gbh + bw/2);                     % center frequency
    switch directions(i),
        case 'u'
            fqstruct.up.fc(ui)=cfq;
            fqstruct.up.bw(ui)=bw;
            ui=ui+1;
        case 'd'
            fqstruct.down.fc(di)=cfq;
            fqstruct.down.bw(di)=bw;
            di=di+1;
        case 'n'
            % do nothing
        otherwise
            error(['planConvertion: unknown direction char "' ...
                    directions(i) '"']);
    end;
    
    i=stopi;
end;