% umtsUplinkReferenceChannels UMTS Uplink measurement channels definition
%   DEF = umtsUplinkReferenceChannels(RC) returns DEF, a structure
%   containing the configuration parameters required to generate an uplink
%   measurement channel waveform using the <a href="matlab:help('umtsUplinkWaveformGenerator')">umtsUplinkWaveformGenerator</a>.
%   This function generates the configuration structures for the uplink
%   Reference Measurement Channels (RMC) or Fixed Reference Channels (FRC).
%   These are defined in TS25.101 Annex A.2 and TS25.141 Annex A.10
%   respectively.
%   
%   RC identifies the measurement channel, it must be one of the
%   following: 'RMC12.2kbps', 'RMC64kbps', 'RMC144kbps', 'RMC384kbps',
%   'FRC1', 'FRC2', 'FRC3', 'FRC4', 'FRC5','FRC6', 'FRC7', 'FRC8'.
% 
%   The fields of the output structure DEF are described in 
%   <a href="matlab:doc('umtsUplinkWaveformGenerator')">umtsUplinkWaveformGenerator</a>.
% 
%   Note that all the definitions are configured with RRC filtering and
%   oversampling set to 4.
%
%   Example: 
%   The example shows the generation of FRC1 configuration and how this is
%   used by umtsUplinkWaveformGenerator.
%
%   config = umtsUplinkReferenceChannels('FRC1');
%   waveform = umtsUplinkWaveformGenerator(config);
%
%   See also umtsUplinkWaveformGenerator, umtsDownlinkReferenceChannels.

%    Copyright 2014-2016 The MathWorks, Inc.

function config = umtsUplinkReferenceChannels(rc)
    validateUMTSParameter('ULRMC',rc);
    config = getULBaseConfig();
    if any(strcmpi(rc,{'RMC12.2kbps', 'RMC64kbps', 'RMC144kbps', 'RMC384kbps'}))   
        config = getULRMCConfig(rc,config);
    else
        % Define FRC configurations
        switch upper(rc)
            case 'FRC1'
                % Build E-DPDCH definition
                config.HSUPA.CodeCombination = [4 4];
                config.HSUPA.EDPDCHPower = config.DPCCH.Power + 12.04;
                config.HSUPA.EDPCCHPower = config.DPCCH.Power + 6.02;
                config.HSUPA.EDCH.BlockSize = 2706;
                config.HSUPA.EDCH.TTI = 2;
                config.HSUPA.EDCH.Modulation = 'BPSK';
            case 'FRC2'
                % Build E-DPDCH definition
                config.HSUPA.CodeCombination = [2 2];
                config.HSUPA.EDPDCHPower = config.DPCCH.Power + 13.00;
                config.HSUPA.EDPCCHPower = config.DPCCH.Power + 6.02;
                config.HSUPA.EDCH.BlockSize = 5412;
                config.HSUPA.EDCH.TTI = 2;
                config.HSUPA.EDCH.Modulation = 'BPSK';
            case 'FRC3'
                config.DPDCH.Enable = 'Off';
                % Build E-DPDCH definition
                config.HSUPA.CodeCombination = [2 2 4 4];
                config.HSUPA.EDPDCHPower = config.DPCCH.Power + 8.94;
                config.HSUPA.EDPCCHPower = config.DPCCH.Power + 2.05;
                config.HSUPA.EDCH.BlockSize = 8100;
                config.HSUPA.EDCH.TTI = 2;
                config.HSUPA.EDCH.Modulation = 'BPSK';
             case 'FRC4'
                % Build E-DPDCH definition
                config.HSUPA.CodeCombination = 4;
                config.HSUPA.EDPDCHPower = config.DPCCH.Power + 12.04;
                config.HSUPA.EDPCCHPower = config.DPCCH.Power;
                config.HSUPA.EDCH.BlockSize = 5076;
                config.HSUPA.EDCH.TTI = 10;
                config.HSUPA.EDCH.Modulation = 'BPSK';
            case 'FRC5'
                % Build E-DPDCH definition
                config.HSUPA.CodeCombination = [4 4];
                config.HSUPA.EDPDCHPower = config.DPCCH.Power + 12.04;
                config.HSUPA.EDPCCHPower = config.DPCCH.Power;
                config.HSUPA.EDCH.BlockSize = 9780;
                config.HSUPA.EDCH.TTI = 10;
                config.HSUPA.EDCH.Modulation = 'BPSK';
            case 'FRC6'
                % Build E-DPDCH definition
                config.HSUPA.CodeCombination = [2 2];
                config.HSUPA.EDPDCHPower = config.DPCCH.Power + 13;
                config.HSUPA.EDPCCHPower = config.DPCCH.Power -1.94;
                config.HSUPA.EDCH.BlockSize = 19278;
                config.HSUPA.EDCH.TTI = 10;
                config.HSUPA.EDCH.Modulation = 'BPSK';
            case 'FRC7'
                % Build E-DPDCH definition
                config.HSUPA.CodeCombination = 16;
                config.HSUPA.EDPDCHPower = config.DPCCH.Power + 8.94;
                config.HSUPA.EDPCCHPower = config.DPCCH.Power + 4.08;
                config.HSUPA.EDCH.BlockSize = 690;
                config.HSUPA.EDCH.TTI = 10;
                config.HSUPA.EDCH.Modulation = 'BPSK';
            case 'FRC8'
                config.DPDCH.Enable = 'Off';
                % Build E-DPDCH definition
                config.HSUPA.CodeCombination = [2 2 4 4];
                config.HSUPA.EDPDCHPower = config.DPCCH.Power + 6.98;
                config.HSUPA.EDPCCHPower = config.DPCCH.Power - 5.46;
                config.HSUPA.EDCH.BlockSize = 16218;
                config.HSUPA.EDCH.TTI = 2;
                config.HSUPA.EDCH.Modulation = '4PAM';  
            otherwise
                error('umts:error','Unsupported UL FRC specified')
        end

    end
end

% Define the base configuration from which all RMCs and FRCs are defined,
% so that the order of parameters is always the same
function config = getULBaseConfig()
    % cell wide settings
    config.TotFrames = 1;
    config.ScramblingCode = 1;
    config.FilterType = 'RRC';
    config.OversamplingRatio = 4;
    config.NormalizedPower = 'Off'; 
    
    % Now define DPDCH
    config.DPDCH.Enable = 'On';
    config.DPDCH.SlotFormat = 2;
    config.DPDCH.CodeCombination = 64;
    config.DPDCH.Power = 0;
    config.DPDCH.DataSource = 'CCTrCH';
    % Define CCTrCH
    % Build DTCH definition
    TrCH(1).Name = 'DTCH';
    TrCH(1).CRC = '16';
    TrCH(1).CodingType = 'conv3';
    TrCH(1).RMA = 256;
    TrCH(1).TTI = 20;
    TrCH(1).DataSource = 'PN9-ITU';
    TrCH(1).ActiveDynamicPart = 1;
    TrCH(1).DynamicPart(1) = struct('BlockSize',244,'BlockSetSize',244);   % 1x244 length block
    % Build DCCH definition
    TrCH(2).Name = 'DCCH';
    TrCH(2).CRC = '12';
    TrCH(2).CodingType = 'conv3';
    TrCH(2).RMA = 256;
    TrCH(2).TTI = 40;
    TrCH(2).DataSource = 'PN9-ITU';
    TrCH(2).ActiveDynamicPart = 1;
    TrCH(2).DynamicPart(1) = struct('BlockSize',100,'BlockSetSize',100);   % 1x100 length block
    config.DPDCH.CCTrCH.Name = 'DCH';
    config.DPDCH.CCTrCH.TrCH(1) = TrCH(1);
    config.DPDCH.CCTrCH.TrCH(2) = TrCH(2);
    
    % Define DPCCH
    config.DPCCH.Enable = 'On';
    config.DPCCH.SlotFormat = 0;
    config.DPCCH.Power = -5.46;
    config.DPCCH.TPCData = 1;
    config.DPCCH.TFCI = 0;
    config.DPCCH.FBIData = 0;
    
    % Define HSUPA
    % Build E-DPDCH definition
    config.HSUPA.Enable = 'On';
    config.HSUPA.CodeCombination = [4 4];
    config.HSUPA.EDPDCHPower = config.DPCCH.Power + 12.04;
    config.HSUPA.EDPCCHPower = config.DPCCH.Power + 6.02;
    config.HSUPA.RSNSequence = 0; 
    config.HSUPA.ETFCI = 0;
    config.HSUPA.HappyBit = 0;
    config.HSUPA.DataSource = 'EDCH';
    config.HSUPA.EDCH.BlockSize = 2706;
    config.HSUPA.EDCH.TTI = 2;
    config.HSUPA.EDCH.Modulation = 'BPSK';
    config.HSUPA.EDCH.DataSource = 'PN9-ITU';
    
    % Define HS-DPCCH
    config.HSDPCCH.Enable = 'Off';
    config.HSDPCCH.Power = 0;
    config.HSDPCCH.CQI = 0;
    config.HSDPCCH.HARQACK = 1;
    config.HSDPCCH.UEMIMO = 0;
    
end

function config = getULRMCConfig(rmc,config)

% Get the TrCHs from base configurations to modify
TrCH(1) = config.DPDCH.CCTrCH.TrCH(1);
TrCH(2) = config.DPDCH.CCTrCH.TrCH(2);
switch upper(rmc)
    case 'RMC12.2KBPS'
        % Build DTCH definition
        TrCH(1).Name = 'DTCH';
        TrCH(1).CRC = '16';
        TrCH(1).CodingType = 'conv3';
        TrCH(1).RMA = 256;
        TrCH(1).TTI = 20;
        TrCH(1).DynamicPart(1) = struct('BlockSize',244,'BlockSetSize',244);   % 1x244 length block
        % Build DCCH definition
        TrCH(2).Name = 'DCCH';
        TrCH(2).CRC = '12';
        TrCH(2).CodingType = 'conv3';
        TrCH(2).RMA = 256;
        TrCH(2).TTI = 40;
        TrCH(2).DynamicPart(1) = struct('BlockSize',100,'BlockSetSize',100);   % 1x100 length block
        % slot formats
        config.DPDCH.SlotFormat = 2;
        config.DPDCH.CodeCombination = 64;
        config.DPCCH.Power = -5.46;
        
    case 'RMC64KBPS'
        % Build DTCH definition
        TrCH(1).Name = 'DTCH';
        TrCH(1).CRC = '16';
        TrCH(1).CodingType = 'turbo';
        TrCH(1).RMA = 256;
        TrCH(1).TTI = 20;
        TrCH(1).DynamicPart(1) = struct('BlockSize',1280,'BlockSetSize',1280); % 1x1280 length block
        % Build DCCH definition
        TrCH(2).Name = 'DCCH';
        TrCH(2).CRC = '12';
        TrCH(2).CodingType = 'conv3';
        TrCH(2).RMA = 256;
        TrCH(2).TTI = 40;
        TrCH(2).DynamicPart(1) = struct('BlockSize',100,'BlockSetSize',100);   % 1x100 length block
        % slot formats
        config.DPDCH.SlotFormat = 4;
        config.DPDCH.CodeCombination = 16;
        config.DPCCH.Power = -9.54;
        
    case 'RMC144KBPS'
        % Build DTCH definition
        TrCH(1).Name = 'DTCH';
        TrCH(1).CRC = '16';
        TrCH(1).CodingType = 'turbo';
        TrCH(1).RMA = 256;
        TrCH(1).TTI = 20;
        TrCH(1).DynamicPart(1) = struct('BlockSize',2880,'BlockSetSize',2880);  % 1x2880 length block
        % Build DCCH definition
        TrCH(2).Name = 'DCCH';
        TrCH(2).CRC = '12';
        TrCH(2).CodingType = 'conv3';
        TrCH(2).RMA = 256;
        TrCH(2).TTI = 40;
        TrCH(2).DynamicPart(1) = struct('BlockSize',100,'BlockSetSize',100);    % 1x100 length block
        % slot formats
        config.DPDCH.SlotFormat = 5;
        config.DPDCH.CodeCombination = 8;
        config.DPCCH.Power = -11.48;
        
    case 'RMC384KBPS'
        % Build DTCH definition
        TrCH(1).Name = 'DTCH';
        TrCH(1).CRC = '16';
        TrCH(1).CodingType = 'turbo';
        TrCH(1).RMA = 256;
        TrCH(1).TTI = 10;
        TrCH(1).DynamicPart(1) = struct('BlockSize',3840,'BlockSetSize',3840);  % 1x3840 length block
        % Build DCCH definition
        TrCH(2).Name = 'DCCH';
        TrCH(2).CRC = '12';
        TrCH(2).CodingType = 'conv3';
        TrCH(2).RMA = 256;
        TrCH(2).TTI = 40;
        TrCH(2).DynamicPart(1) = struct('BlockSize',100,'BlockSetSize',100);    % 1x100 length block
        % slot formats
        config.DPDCH.SlotFormat = 6;
        config.DPDCH.CodeCombination = 4;
        config.DPCCH.Power = -11.48;
       
    otherwise
        error('umts:error','Unsupported UL RMC specified')
end
    % Finalize DPDCH using the above RMC definitions
    config.DPDCH.CCTrCH.TrCH(1) = TrCH(1);
    config.DPDCH.CCTrCH.TrCH(2) = TrCH(2);
    
    % Disable the HSUPA channels for RMCs
    config.HSUPA.Enable = 'Off';
end


