% ===========================================================================================================
% ==== Simulator for WCDMA radio interface dimensioning by David Soldani 28 February     2004             ===
% ===========================================================================================================
% This define the TC in the following
% GB Traffic
% Signalling link         RAB = 1
% Emergency call          RAB = 2
% Conversational speech   RAB = 3
% Conversational data     RAB = 4
% Streaming               RAB = 5

% NGB Traffic
% Interactive             RAB = 6
% Background              RAB = 7

% ApplicationService = 1; Signalling (re-establishment)
% ApplicationService = 2; Emergency call
% ApplicationService = 3; CS speech call
% ApplicationService = 4; CS video call
% ApplicationService = 5; PS/CS Streaming
% ApplicationService = 6; Multimedia Messages (MMS)
% ApplicationService = 7; Push to Talk (PoC)
% ApplicationService = 8; See What I See = Real Time Video Sharing (RTVS)
% ApplicationService = 9; Dialup connection (FTP, Web browsing, Real one, etc...)
% ApplicationService = 10; WAP browsing
% ApplicationService = 11; Games (Not supported)

clear

%randn('state', 0); % randn('state',sum(100*clock));
%rand('state', 0);  % rand('state',sum(100*clock));

NofApplicationServices =10;

% --- SIGNALLING (exponential) -----------
MeanServiceTime(1) = 120;    % Signalling - Call or Session duration in average - 120 [s] from UMTS 30.03 version 3.2.0
MeanArrivalRate(1)= 4800;	% Signalling - Mean call/session arrival rate [s], in average a user makes a call every MeanArrivalRate s
ShareOfCalls(1)= 0; % [%] 
% QoS Profile
QoSprofile(1).RRP=9; % Priority value for AC
QoSprofile(1).TF1=3.4; % [kb/s] Guaranteed bit rate
QoSprofile(1).TF2=0; % [kb/s]
QoSprofile(1).TF3=0; % [kb/s]
QoSprofile(1).TF4=0; % [kb/s]
QoSprofile(1).TF5=0; % [kb/s]
QoSprofile(1).TF6=0; % [kb/s]
QoSprofile(1).TF7=0; % [kb/s]
QoSprofile(1).TF8=0; % [kb/s]
QoSprofile(1).THP=0; % THP (not defined)
QoSprofile(1).TC=1; % Signalling
QoSprofile(1).ARP=0; % Gold

% ----- EMERGENCY CALL (exponential) -----
MeanServiceTime(2) = 60;     % Emergency call - Call or Session duration in average - 120 [s] from UMTS 30.03 version 3.2.0
MeanArrivalRate(2)=60000;	% 1mErl Emergency call - Mean call/session arrival rate [s], in average a user makes a call every MeanArrivalRate s
ShareOfCalls(2) = 0; % [%]
% QoS Profile
QoSprofile(2).RRP=1; % Priority value for AC
QoSprofile(2).TF1=12.2; % [kb/s] Guaranteed bit rate 
QoSprofile(2).TF2=0; % [kb/s]
QoSprofile(2).TF3=0; % [kb/s]
QoSprofile(2).TF4=0; % [kb/s]
QoSprofile(2).TF5=0; % [kb/s]
QoSprofile(2).TF6=0; % [kb/s]
QoSprofile(2).TF7=0; % [kb/s]
QoSprofile(2).TF8=0; % [kb/s]
QoSprofile(2).THP=0; % THP (not defined)
QoSprofile(2).TC=2; % CS - Emergency call
QoSprofile(2).ARP=0; % ND

% ---- SPEECH CALL (exponential) ---------
MeanServiceTime(3) = 90;    % Conversational Speech - Call or Session duration in average - 120 [s] from UMTS 30.03 version 3.2.0
MeanArrivalRate(3)=4800;	% 25mErl Speech call - Mean call arrival rate [s], in average a user makes a call every MeanArrivalRate s
ShareOfCalls(3)= 100; % [%] for traffic mix
% QoS Profile
QoSprofile(3).RRP=2; % Priority value for AC
QoSprofile(3).TF1=12.2; % [kb/s] Guaranteed bit rate
QoSprofile(3).TF2=0; % [kb/s]
QoSprofile(3).TF3=0; % [kb/s]
QoSprofile(3).TF4=0; % [kb/s]
QoSprofile(3).TF5=0; % [kb/s]
QoSprofile(3).TF6=0; % [kb/s]
QoSprofile(3).TF7=0; % [kb/s]
QoSprofile(3).TF8=0; % [kb/s]
QoSprofile(3).THP=0; % THP (not defined)
QoSprofile(3).TC=3; % CS - Speech call
QoSprofile(3).ARP=0; % ND

% ---- VIDEO CALL (exponential) ----------
MeanServiceTime(4) = 120;    % Conversational Data - Call or Session duration in average - 120 [s] UMTS 30.03 version 3.2.0
MeanArrivalRate(4)=24000;	% 5mErl Video call - Mean call arrival rate [s], in average a user makes a call every MeanArrivalRate s
ShareOfCalls(4)= 3; % [%] for traffic mix
% QoS Profile
QoSprofile(4).RRP=3; % Priority value for AC
QoSprofile(4).TF1=64; % [kb/s] Guaranteed bit rate
QoSprofile(4).TF2=0; % [kb/s]
QoSprofile(4).TF3=0; % [kb/s]
QoSprofile(4).TF4=0; % [kb/s]
QoSprofile(4).TF5=0; % [kb/s]
QoSprofile(4).TF6=0; % [kb/s]
QoSprofile(4).TF7=0; % [kb/s]
QoSprofile(4).TF8=0; % [kb/s]
QoSprofile(4).THP=0; % THP (not defined)
QoSprofile(4).TC=4; % CS - Video call
QoSprofile(4).ARP=0; % ND

% ---- PS - STREAMING --------
MeanServiceTime(5) = 600;    % s 
MeanArrivalRate(5)=5*3600;	% 16mErl Streaming - Mean call arrival rate [s], in average a user makes a call every MeanArrivalRate s
ShareOfCalls(5) = 10; % [%] for traffic mix
% QoS Profile
QoSprofile(5).RRP=6; % Priority value for AC, PS and LC
QoSprofile(5).TF1=0; % [kb/s] Guaranteed bit rate (if mapped onto Streaming TC)
QoSprofile(5).TF2=0; % [kb/s]
QoSprofile(5).TF3=0; % [kb/s]
QoSprofile(5).TF4=64; % [kb/s]
QoSprofile(5).TF5=64; % [kb/s]
QoSprofile(5).TF6=64; % [kb/s]
QoSprofile(5).TF7=64; % [kb/s]
QoSprofile(5).TF8=64; % [kb/s]
QoSprofile(5).THP=2; % THP (not defined)
QoSprofile(5).TC=6; % Interactive TC (GB)
QoSprofile(5).ARP=2; % Gold


% ---- MMS -----------------
MeanServiceTime(6) = 10;    % 0 means that it depends on clip and bit rate
MeanArrivalRate(6)=2*3600;	% Interactive - Mean session arrival rate [s], in average a user opens a session every MeanArrivalRate s
ShareOfCalls(6)= 10; % [%] for traffic mix
% QoS Profile
QoSprofile(6).RRP=7; % Priority value for AC, PS and LC
QoSprofile(6).TF1=0; % [kb/s] 
QoSprofile(6).TF2=0; % [kb/s]
QoSprofile(6).TF3=0; % [kb/s]
QoSprofile(6).TF4=64; % [kb/s] Minimum Allowed Bit Rate
QoSprofile(6).TF5=128; % [kb/s]
QoSprofile(6).TF6=144; % [kb/s]
QoSprofile(6).TF7=256; % [kb/s]
QoSprofile(6).TF8=384; % [kb/s] Maximum Allowed Bit Rate
QoSprofile(6).THP=3; % Dummy parameter for PS (maybe to be removed)
QoSprofile(6).TC=6; % Interactive
QoSprofile(6).ARP=3; % Bronze

% ---- PoC -----------------
MeanServiceTime(7) = 60;    % 0 means that it depends on clip and bit rate
MeanArrivalRate(7)=2*3600;	% Interactive - Mean session arrival rate [s], in average a user opens a session every MeanArrivalRate s
%group size definition: Geometric distribution
mean_group_size=1;
max_group_size=25;
min_group_size=1;
ShareOfCalls(7)=20; % [%] for traffic mix
% QoS Profile
QoSprofile(7).RRP=5; % Priority value for AC, PS and LC
QoSprofile(7).TF1=0; % 5.15; % [kb/s] Guaranteed bit rate (RLC)
QoSprofile(7).TF2=0; % [kb/s]
QoSprofile(7).TF3=0; % [kb/s]
QoSprofile(7).TF4=16; % [kb/s] Min Allowed BR
QoSprofile(7).TF5=16; % [kb/s]
QoSprofile(7).TF6=16; % [kb/s]
QoSprofile(7).TF7=16; % [kb/s]
QoSprofile(7).TF8=16; % [kb/s] Max Allowed BR
QoSprofile(7).THP=1; % THP2
QoSprofile(7).TC=6; % Interactive
QoSprofile(7).ARP=1; % Gold

% ---- RTVS = SWISS (exponential) -----------------
MeanServiceTime(8) = 180;    % 0 means that it depends on clip and bit rate
MeanArrivalRate(8)=2*3600;	% Interactive - Mean session arrival rate [s], in average a user opens a session every MeanArrivalRate s
ShareOfCalls(8)= 3; % [%] for traffic mix
% QoS Profile
QoSprofile(8).RRP=4; % Priority value for AC, PS and LC
QoSprofile(8).TF1=64; % [kb/s] Guaranteed bit rate
QoSprofile(8).TF2=0; % [kb/s]
QoSprofile(8).TF3=0; % [kb/s]
QoSprofile(8).TF4=0; % [kb/s]
QoSprofile(8).TF5=0; % [kb/s]
QoSprofile(8).TF6=0; % [kb/s]
QoSprofile(8).TF7=0; % [kb/s]
QoSprofile(8).TF8=0; % [kb/s]
QoSprofile(8).THP=0; % THP2
QoSprofile(8).TC=5; % Streaming
QoSprofile(8).ARP=1; % Gold

% ------- Dialup: http, FTP, emails -------------
MeanServiceTime(9) = 1200;    % 0 means that it depends on clip and bit rate
MeanArrivalRate(9)=2*3600;	% Interactive - Mean session arrival rate [s], in average a user opens a session every MeanArrivalRate s
ShareOfCalls(9)= 1; % [%] for traffic mix
% QoS Profile
QoSprofile(9).RRP=8; % Priority value for AC, PS and LC
QoSprofile(9).TF1=0; % [kb/s] 
QoSprofile(9).TF2=0; % [kb/s]
QoSprofile(9).TF3=0; % [kb/s]
QoSprofile(9).TF4=64; % [kb/s] Minimum Allowed Bit Rate
QoSprofile(9).TF5=128; % [kb/s]
QoSprofile(9).TF6=144; % [kb/s]
QoSprofile(9).TF7=256; % [kb/s]
QoSprofile(9).TF8=384; % [kb/s] Maximum Allowed Bit Rate
QoSprofile(9).THP=3; % THP3
QoSprofile(9).TC=7; % Background
QoSprofile(9).ARP=3; % Bronze


% -------------- Wap browsing -------------------------------------------------------------------------------
MeanServiceTime(10) = 600;    % s
MeanArrivalRate(10)=4*3600;	%  Mean call/session arrival rate [s], in average a user opens a session every MeanArrivalRate s
ShareOfCalls(10)= 20;%100-sum(ShareOfCalls(1:(NofApplicationServices-1))); % [%] for traffic mix
% QoS Profile
QoSprofile(10).RRP=7; % Priority value for AC, PS and LC
QoSprofile(10).TF1=0; % [kb/s] 
QoSprofile(10).TF2=0; % [kb/s]
QoSprofile(10).TF3=0; % [kb/s]
QoSprofile(10).TF4=64; % [kb/s] Minimum Allowed Bit Rate
QoSprofile(10).TF5=128; % [kb/s]
QoSprofile(10).TF6=144; % [kb/s]
QoSprofile(10).TF7=256; % [kb/s]
QoSprofile(10).TF8=384; % [kb/s] Maximum Allowed Bit Rate
QoSprofile(10).THP=3; % THP3
QoSprofile(10).TC=6; % Interactive
QoSprofile(10).ARP=3; % Bronze
% -----------------------------------------------------------------------------------------------------------------------------------------------
mErl=MeanServiceTime.*(1./MeanArrivalRate)*1000;

MeasurementPeriod = 3600;% Observation time for statistics [s], i.e. simulation interval


NofIteration= 1000; % Number of snapshots for reliable statistics

N = 720; % N. of subscribers in the cell in the beginning

PtxTarget=70; % [%] Ptx_Target (remember that 20% of Ltot is left for common channels)
Offset=10; % [%] 1 dB=12.5 %  PtxTarget+Offset is the overload threshold

PtxNGBCapacity=0; % 2dB Range = 0 ... 60 dB, step 0.1 dB

W= 3.84*10^6; 	%	chip rate [cps]

% ---------- Activity Factors (default values) for Loading Computation -------------
AF(1) = 0;    % Signalling
AF(2) = 0.67; % Emergency call
AF(3) = 0.67; % CS speech
AF(4) = 1;    % CS video
AF(5) = 1;    % PS/CS Streaming
AF(6) = 1;    % Multimedia Messages (MMS)
AF(7) = 0.6;    % Push to Talk (PoC)
AF(8) = 1;    % See What I See = Real Time Video Sharing (RTVS)
AF(9) = 0.8;  % Dialup connection (FTP, Web browsing, Real one, etc...)
AF(10) = 0.6; % WAP browsing

% ---------- Thresholds for user satisfaction in %  -------------
Limit(1) = 90;    % Signalling
Limit(2) = 90; % Emergency call
Limit(3) = 90; % CS speech
Limit(4) = 90;    % CS video
Limit(5) = 90;    % PS/CS Streaming
Limit(6) = 70;    % Multimedia Messages (MMS)
Limit(7) = 90;    % Push to Talk (PoC)
Limit(8) = 90;    % See What I See = Real Time Video Sharing (RTVS)
Limit(9) = 50;  % Dialup connection (FTP, Web browsing, Real one, etc...)
Limit(10) = 70; % WAP browsing

% ---------- Target bit rate in kb/s  -------------
TargetBR(1) = 3.4;    % Signalling
TargetBR(2) = 12.2; % Emergency call
TargetBR(3) = 12.2; % CS speech
TargetBR(4) = 64;    % CS video
TargetBR(5) = 64;    % PS/CS Streaming
TargetBR(6) = 32;    % Multimedia Messages (MMS)
TargetBR(7) = 8;    % Push to Talk (PoC)
TargetBR(8) = 64;    % See What I See = Real Time Video Sharing (RTVS)
TargetBR(9) = 32;  % Dialup connection (FTP, Web browsing, Real one, etc...)
TargetBR(10) = 32; % WAP browsing

% ------------ Downlink required EbNo --------------------------------------
EbNo(1) = 7;   % Signalling
EbNo(2) = 7;   % Emergency call
EbNo(3) = 7;   % Conversational speech
EbNo(4) = 6;   % Conversational data
EbNo(5) = 6;   % Streaming
EbNo(6) = 5;    % Multimedia Messages (MMS)
EbNo(7) = 7;    % Push to Talk (PoC)
EbNo(8) = 6;    % See What I See = Real Time Video Sharing (RTVS)
EbNo(9) = 5.5;  % Dialup connection (FTP, Web browsing, Real one, etc...)
EbNo(10) = 5.5; % WAP browsing

iOthOwn = 0.55; % DL Other cell to own cell interference (here, macro with omni antennas)
alpha = 0.5;    % DL orthogonality, here in VehA channel
sho = 0.20;     % Soft handover overhead in DL


%ServiceThroughput=zeros(NofApplicationServices,1);


Lnrt=0;


for NUsers=N:3000
    
    
    for k=1:NofIteration
        
        % ================================================================================
        % ================== Call Generator ==============================================
        % ================================================================================
        
        
        ActivityProbability = MeanServiceTime.*(1./MeanArrivalRate).*ShareOfCalls/100; %Probability for one user to be active
        
        ShareOfCalls_new=ShareOfCalls;
        NUsers_new=NUsers;
        
        Queue=[];
        % Scan all users
        group_size = geometric(mean_group_size,min_group_size,max_group_size);
        
        ActivityProbability(7) = group_size*MeanServiceTime(7)/MeanArrivalRate(7)*ShareOfCalls(7)/100; %Probability for being active for one user.        
          
        
        i=0;
        for j=1:NUsers
            
            temp_rand=rand;
            
            if min(find(cumsum(ActivityProbability)>temp_rand))>0; 
                
                ApplicationService = min(find(cumsum(ActivityProbability)>temp_rand));
                
                i = i+1;  
                Queue(1, i) = QoSprofile(ApplicationService).TC; % QoS Class or RAB type
                Queue(2, i) = QoSprofile(ApplicationService).RRP; % Call/Session Priority, for NGB traffic has time dependend value: Queue(2, i) = Queue(32, i)-TimePriorityStep(RAB)*(T2-Queue(26, i))/To;
                Queue(3, i) = 0; % Management field (1 admitted, 2 scheduled, 0 by AC)
                Queue(4, i) = QoSprofile(ApplicationService).TF1; % Bit rate [kbps]
                Queue(5, i) = QoSprofile(ApplicationService).TF2; % Bit rate [kbps]
                Queue(6, i) = QoSprofile(ApplicationService).TF3; % Bit rate [kbps]
                Queue(7, i) = QoSprofile(ApplicationService).TF4; % Bit rate [kbps]
                Queue(8, i) = QoSprofile(ApplicationService).TF5; % Bit rate [kbps]
                Queue(9, i) = QoSprofile(ApplicationService).TF6; % Bit rate [kbps]
                Queue(10, i) = QoSprofile(ApplicationService).TF7; % Bit rate [kbps]
                Queue(11, i) = QoSprofile(ApplicationService).TF8; % Bit rate [kbps]
                Queue(12, i) = 0 ; % Collects the av. active throughput of the call/session [kbps]    
                Queue(13, i) = 0; % Allocated bitrate [kbps]
                Queue(14, i) = ApplicationService; % Carried application service
                Queue(15, i) = QoSprofile(ApplicationService).THP; % for PS
                
                ShareOfCalls_new(ApplicationService)=100*(((NUsers_new*ShareOfCalls_new(ApplicationService)/100)-1)/(NUsers_new-1));
                
                NUsers_new=NUsers_new-1;    
                ActivityProbability = MeanServiceTime.*(1./MeanArrivalRate).*ShareOfCalls_new/100;
                ActivityProbability(7) = group_size*MeanServiceTime(7)/MeanArrivalRate(7)*ShareOfCalls_new(7)/100;
                
            end
        end
        
        if (Queue~=[])
            Queue=sortrows(Queue',2)'; % The queue is sorted based on priorities
            
            
            % ==========================================================================
            % ========================  Allocation loop ===============================
            % ==========================================================================
            
            
            Lrt=0;
            % Admission Control
            i=1;
            while and(Lrt<PtxTarget, i<=size(Queue,2)) % Call admission for not rejected session/call due to queuing
                DeltaLoad = dlwcdmaload(EbNo(Queue(14,i)),1,sho,AF(Queue(14,i)),alpha,iOthOwn,W,Queue(4,i));
                if or((Lrt+DeltaLoad)<=PtxTarget/10^(PtxNGBCapacity/10), Queue(1,i)>=6) % NGB traffic is always admitted
                    if Queue(1,i)<=5 % Collect loading and bitrate for GB
                        Lrt=Lrt+DeltaLoad; % Allocated non controllable load
                        Queue(13,i)=Queue(4,i); % Collect actual bit rate
                    end
                    Queue(3,i)=1; % Admitted
                    
                end
                i=i+1;
                
            end  % End of call admission   
            
            % =========== Queue Management =============
            
            
            % Packet Scheduler
            ind1 = find(Queue(1, :)>5);
            cnt_NGB = length(ind1); % Number of DCHs to be scheduled
            
            PSqueue = Queue(:, ind1);
            
            if and(cnt_NGB>0,(PtxTarget-Lrt)>0)
                
                fg=0; % Number of bit rates
                for dd=0:7
                    for nn=1:cnt_NGB
                        fg=fg+1;
                        raw(fg)=dd; % Collect position of Bit-Rate
                        col(fg)=nn; % Collect position of RAB
                    end
                end
                
                ntot=fg;
                quit=0;
                
                while and(ntot>=cnt_NGB,quit==0)
                    CR=0;
                    ServedCRposition=0;
                    AllocatedNGBcapacity=0;
                    AllocatedNGBbitRate=0;
                    Lnrt=0;
                    for kk=ntot:-1:(ntot-cnt_NGB+1)
                        Lnrt=Lnrt+dlwcdmaload(EbNo(PSqueue(14,col(kk))),1,sho,AF(PSqueue(14,col(kk))),alpha,iOthOwn,W,PSqueue(4+raw(kk),col(kk)));
                        CR=CR+1;
                        ServedCRposition(CR)=ind1(col(kk)); % PSqueue(size(Queue,1)+1,col(kk)); % Collect position of served Capacity Request
                        AllocatedNGBcapacity(CR)=dlwcdmaload(EbNo(PSqueue(14,col(kk))),1,sho,AF(PSqueue(14,col(kk))),alpha,iOthOwn,W,PSqueue(4+raw(kk),col(kk)));
                        AllocatedNGBbitRate(CR)=PSqueue(4+raw(kk),col(kk)); % Collect allocated NGB bi-rate
                    end % 
                    if Lnrt >(PtxTarget-Lrt) % PtxNGBoffset*WeightNGBoffset is the autotuning part
                        ntot=ntot-1;
                        if ntot<cnt_NGB
                            cnt_NGB=cnt_NGB-1;
                        end   
                    else 
                        quit=1;
                    end
                end % While Cycle
                for sd=1:CR % Update loading for served NGB Capacity Requests
                    Queue(13,ServedCRposition(sd))=AllocatedNGBbitRate(sd);
                    Queue(3,ServedCRposition(sd))=2; % The CR is served by PS
                end   
            end % Packet Scheduler
            
            
            
            Loading_rt(k)=Lrt;% Lrt in each iteration
            Loading_nrt(k)=Lnrt;% Lrt in each iteration
            Throughput_rt(k)=sum(Queue(13,find(Queue(3,:)==1 & Queue(1,:)<=5)));% Lrt in each iteration
            Throughput_nrt(k)=sum(Queue(13,find(Queue(3,:)==2)));% Lnrt in each iteration
            
            for i=1:NofApplicationServices
                UserType(k,i)=length(find(Queue(14,:)==i));
            end
            
            for i=1:NofApplicationServices
                AdmittedUserType(k,i)=length(find(Queue(14,:)==i&Queue(3,:)>0));
            end
            
            for i=1:NofApplicationServices
                if length(find(Queue(14,:)==i & Queue(3,:)==0))>0
                    CallBlockRatio(k,i)=length(find(Queue(14,:)==i & Queue(3,:)==0))/max(UserType(k,i),1)*100;
                else
                    CallBlockRatio(k,i)=0;
                end    
            end
            
            
            for i=1:NofApplicationServices
                if length(find(Queue(14,:)==i))>0
                    ServiceThroughput(k,i)=sum(Queue(13,find(Queue(14,:)==i))); %sum of allocated bitrate per service
                    %mean(Queue(13,find(Queue(14,:)==i & Queue(3,:)>0)))               
                else   
                    ServiceThroughput(k,i)=0;
                end
                
            end 
            
            
            Speech_satisfied_user(k) =(1- (CallBlockRatio(k,3))/100)*100;            
            Video_satisfied_user(k) =(1- (CallBlockRatio(k,4))/100)*100;            
            SWIS_satisfied_user(k) =(1- (CallBlockRatio(k,8))/100)*100;            
            PoC_satisfied_user(k) =(1- length(find(Queue(13,:)<TargetBR(7) & Queue(14,:)==7))/max(UserType(k,7),1) - CallBlockRatio(k,7)/100)*100;            
            Streaming_satisfied_user(k) =(1- length(find(Queue(13,:)<TargetBR(5) & Queue(14,:)==5))/max(UserType(k,5),1) - CallBlockRatio(k,5)/100)*100;            
            MMS_satisfied_user(k) =(1- length(find(Queue(13,:)<TargetBR(6) & Queue(14,:)==6))/max(UserType(k,6),1) - CallBlockRatio(k,6)/100)*100;            
            Dialup_satisfied_user(k) =(1- length(find(Queue(13,:)<TargetBR(9) & Queue(14,:)==9))/max(UserType(k,9),1) - CallBlockRatio(k,9)/100)*100;         
            WAP_satisfied_user(k) =(1- length(find(Queue(13,:)<TargetBR(10) & Queue(14,:)==10))/max(UserType(k,10),1) - CallBlockRatio(k,10)/100)*100;
            
        end   
        
        
    end % Iteration Loop
    
    
    Speech_sat =mean(Speech_satisfied_user);
    Video_sat=mean(Video_satisfied_user);
    SWIS_sat =mean(SWIS_satisfied_user);
    PoC_sat = mean(PoC_satisfied_user);
    Streaming_sat = mean(Streaming_satisfied_user);
    MMS_sat = mean(MMS_satisfied_user);
    Dialup_sat=mean(Dialup_satisfied_user);
    WAP_sat= mean(WAP_satisfied_user);
    
    for i=1:NofApplicationServices
        AvUserType(i) = mean(UserType(:,i));
    end
    
    
    if (Speech_sat<Limit(3) | SWIS_sat<Limit(8) | PoC_sat <Limit(7) | Streaming_sat<Limit(5) | MMS_sat<Limit(6) | Dialup_sat<Limit(9) | WAP_sat<Limit(10) | Video_sat<Limit(4))
        break;
        %end    
        
    end %condition for exiting main loop (increasing number of users)
    
    Number_of_users = NUsers %displays main loop iteration variable on the screen
    
end


% figure(1)
% clf
% hold on
% plot(Loading_rt,'r');% Lrt in each iteration
% plot(Loading_nrt,'g');% Lrt in each iteration
% plot(Loading_nrt+Loading_rt,'b');
% %plot(PtxTarget-Loading_rt,'k');
% xlabel('Iteration')
% ylabel('L_r_t(r)   L_n_r_t(g)  L_t_o_t(b) (%)')
% grid
% 

%figure(2)
%clf
%hold on
%plot(Throughput_rt,'r');% Lrt in each iteration
%plot(Throughput_nrt,'g');% Lrt in each iteration
%plot(Throughput_nrt+Throughput_rt,'b');% Lrt in each iteration
%xlabel('Iteration')
%ylabel('Cell Throughput   rt(r)   nrt(g)  tot(b) (kb/s)')
%grid
% 
for i=1:NofApplicationServices
    AvCallBlockRatio(i) = mean(CallBlockRatio(:,i));
end

for i=1:NofApplicationServices
    AvUserType(i) = mean(UserType(:,i));
end

for i=1:NofApplicationServices
    AvServiceThroughput(i) = sum(ServiceThroughput(:,i))/sum(UserType(:,i)); %avg allocated bitrate per user (including non admitted)
end

for i=1:NofApplicationServices
    AvAllocatedBitRate(i) = sum(ServiceThroughput(:,i))/sum(AdmittedUserType(:,i)); %avg allocated bitrate per user (excluding non admitted)
end

AvCallBlockRatio
AvAllocatedBitRate
AvServiceThroughput
AvUserType
sum(AvUserType)

std(UserType(:,7))


Speech_sat
Video_sat
SWIS_sat
PoC_sat
Streaming_sat
MMS_sat
Dialup_sat
WAP_sat

mean(Throughput_rt)
mean(Throughput_nrt)
tot = mean(Throughput_rt)+mean(Throughput_nrt)
