function [User, Total_Ant, Kz, b,Per_BS_Coop,Per_BS_Coop_Bkhaul,Per_BS_ServAnt,Per_BS_CompressionAnt]= ...
    Compute_Kz_UL(Num_Cell,User,Cells, Num_TxAnt_Macro,Num_TxAnt_Pico,L_Macro,L_Pico,Chn,Sim)
%%
Total_BS = L_Macro+L_Pico;
ant_gain = 1;
Total_Ant = 0;

%compute parameters for cvx solver to compute Tx BF
Kz = [];
b = [];
for m = 1:Num_Cell
    for l = 1:Total_BS
        Per_BS_Coop(m,l).Coop_Matrix = [];
        Per_BS_Coop_Bkhaul(m,l).Coop_Matrix = [];
        Per_BS_ServAnt(m,l).index = [];
        Per_BS_CompressionAnt(m,l).index = [];
    end
end

for l = 1:Num_Cell
    for ik = 1:length(Cells(l).Scheduled_User)
        k = Cells(l).Scheduled_User(ik);
        
        ant_head = 1;
        for iBS = 1:size(User(l,k).ServingCluster,1)
            curr_cell = User(l,k).ServingCluster(iBS,1);
            curr_BS = User(l,k).ServingCluster(iBS,2);
            
            flag = Check_Data_Sharing_BS(curr_cell, curr_BS, l, k, User);
            
            if curr_BS <= L_Macro %Macro BS
                temp2 = ones(Num_TxAnt_Macro,1)*sqrt(User(l,k).prev_inst_rate*User(l,k).bkhaul_wgt(curr_cell,curr_BS));
                Per_BS_ServAnt(curr_cell,curr_BS).index = [Per_BS_ServAnt(curr_cell,curr_BS).index (Total_Ant+ant_head):(Total_Ant+ant_head+Num_TxAnt_Macro-1)];
                %Per_BS_Coop(curr_cell,curr_BS).Coop_Matrix = blkdiag(Per_BS_Coop(curr_cell,curr_BS).Coop_Matrix, eye(Num_TxAnt_Macro));
                Per_BS_Coop_Bkhaul(curr_cell,curr_BS).Coop_Matrix = blkdiag(Per_BS_Coop_Bkhaul(curr_cell,curr_BS).Coop_Matrix, diag(temp2));
                
                if flag == 0
                    Per_BS_CompressionAnt(curr_cell,curr_BS).index = [Per_BS_CompressionAnt(curr_cell,curr_BS).index (Total_Ant+ant_head):(Total_Ant+ant_head+Num_TxAnt_Macro-1)];
                end
                
                ant_head = ant_head + Num_TxAnt_Macro;
            else    %Pico BS
                temp2 = ones(Num_TxAnt_Pico,1)*sqrt(User(l,k).prev_inst_rate*User(l,k).bkhaul_wgt(curr_cell,curr_BS));
                Per_BS_ServAnt(curr_cell,curr_BS).index = [Per_BS_ServAnt(curr_cell,curr_BS).index (Total_Ant+ant_head):(Total_Ant+ant_head+Num_TxAnt_Pico-1)];
                %Per_BS_Coop(curr_cell,curr_BS).Coop_Matrix = blkdiag(Per_BS_Coop(curr_cell,curr_BS).Coop_Matrix, eye(Num_TxAnt_Pico));
                Per_BS_Coop_Bkhaul(curr_cell,curr_BS).Coop_Matrix = blkdiag(Per_BS_Coop_Bkhaul(curr_cell,curr_BS).Coop_Matrix, diag(temp2));
                
                if flag == 0
                    Per_BS_CompressionAnt(curr_cell,curr_BS).index = [Per_BS_CompressionAnt(curr_cell,curr_BS).index (Total_Ant+ant_head):(Total_Ant+ant_head+Num_TxAnt_Pico-1)];
                end
                
                ant_head = ant_head + Num_TxAnt_Pico;
            end
        end
        
        
        Kz_UL_intf = zeros(User(l,k).ServingAnt,User(l,k).ServingAnt);
        for m = 1:Num_Cell
            for ikk = 1:length(Cells(m).Scheduled_User)
                kk = Cells(m).Scheduled_User(ikk);
                
                h = (User(m,kk).beam_rx)' * Get_Chn(Chn, User(l,k).ServingCluster, m, kk, Sim)';
              %  Kz_UL_intf = Kz_UL_intf + User(m,kk).wgt_mse*(h'*h)*ant_gain;
                
                if (l==m) && (k==kk)
                    Kz_UL_intf = Kz_UL_intf + User(m,kk).wgt_mse*(h'*h)*ant_gain;
                else
                    Kz_UL_intf = Kz_UL_intf + User(m,kk).wgt_mse*(h'*h)*ant_gain*Sim.snr_gap;
                end
                
                
            end
        end
        
        User(l,k).Kz_UL_intf = Kz_UL_intf;
        
        Kz = blkdiag(Kz,sqrtm(Kz_UL_intf)); %matrix in the obj
        b_k = pinv(sqrtm(Kz_UL_intf))*User(l,k).wgt_mse*Get_Chn(Chn, User(l,k).ServingCluster, l, k, Sim)*User(l,k).beam_rx;
        b = [b;b_k];    %vector in the obj
        
        Total_Ant = Total_Ant + User(l,k).ServingAnt;
        
    end
end