function [User, max_diff] = CVX_QCQP_TxBF(User,Cells,BS,Num_Cell,Power_Mode,Chn,...
    Total_BS,Total_Ant, Kz, b,Per_BS_Coop,Per_BS_Coop_Bkhaul,Per_BS_ServAnt,Per_BS_CompressionAnt)

%%%% This function finds the optimal Tx BF using cvx conditioned on
%%%% different power constraints and sparse constraints

N = Total_Ant;

%fprintf( 'call cvx solver \n');
cvx_begin quiet
variable V(N,1) complex; %Tx BF

minimize norm( Kz*V - b)
subject to

for m = 1:Num_Cell
    for l = 1:Total_BS
        
        %         if size(Per_BS_Coop(m,l).Coop_Matrix,2) ~= size(Per_BS_ServAnt(m,l).index,2)
        %             size(Per_BS_Coop(m,l).Coop_Matrix,2)
        %             size(Per_BS_ServAnt(m,l).index,2)
        %         end
        
        %if ~isempty(Per_BS_Coop(m,l).Coop_Matrix) && ~isempty(Per_BS_ServAnt(m,l).index)
        %             size(Per_BS_Coop(m,l).Coop_Matrix,2)
        %             size(Per_BS_ServAnt(m,l).index,2)
        if ~isempty(Per_BS_ServAnt(m,l).index)
            %norm(Per_BS_Coop(m,l).Coop_Matrix * V(Per_BS_ServAnt(m,l).index)) <= sqrt(BS(m,l).bf_power_constraint); %per BS power constraint
            norm(V(Per_BS_ServAnt(m,l).index)) <= sqrt(BS(m,l).total_power_budget / (1 + BS(m,l).quant_wgt)); %per BS power constraint
            %       norm(Per_BS_Coop_Bkhaul(m,l).Coop_Matrix * V(Per_BS_ServAnt(m,l).index)) <= sqrt(per_BS_bkhaul_constraint(m,l)); %per BS backhaul constraint
            
        end
        
%         if ~isempty(Per_BS_CompressionAnt(m,l).index)
%             norm(V(Per_BS_CompressionAnt(m,l).index)) <= sqrt(BS(m,l).compression_power_constraint);
%         end
        
    end
end

cvx_end

cvx_status

%assign cvx solution to beam_tx and find the max beam_tx difference for convergence check
max_diff = 0;
ant_head = 1;
for l = 1:Num_Cell
    for ik = 1:length(Cells(l).Scheduled_User)
        k = Cells(l).Scheduled_User(ik);
        previous_beam = User(l,k).beam_tx;
        
        ant_tail = ant_head + User(l,k).ServingAnt -1;
        
        User(l,k).beam_tx = V(ant_head:ant_tail);
        diff = norm(User(l,k).beam_tx - previous_beam)/norm(previous_beam);
        
        if diff > max_diff
            max_diff = diff;
        end
        
        ant_head = ant_tail+1;
        
    end
end