% function [S1, S2, b1, b2] = osb(Hmag_sq, No, E, theta, mask, ...
% gap, bitcap, cb)
%
% This is the main program call to osb.  It sets up calculation of w1 
% and calls a subroutine that in turn sets up w2, which in turn calls
% another routine that does the main crosstalk calculations.
%
% Inputs 
%
% Hmag_sq is a N x 2 x 2 where N is FFT size. N inferred from this.
% No      is a 1 x U white-noise power spectra density matrix.
%    If Hmag_sq is complex BB, then No should be the one-sided PSD.
% E       is a 1 x U energy vector.
% theta   is a 1 x U user-rate weighting vector.
% mask    is an N x U PSD maximum allowed.
% gap     is the (non-dB) linear gap (so 1 if 0 dB gap).
% bitcap  is a 1 x U maximum number of bits allowed per tone.
% cb      is 2 for real baseband and 1 for cplex bband
%
% Outputs
%
% S1      is user 1's Nx1 PSD
% S2      is user 2's Nx1 PSD
% b1      is user 1's Nx1 bit distribution
% b2      is user 2's Nx1 bit distribution
%
% calls optimize_l2.m, which calls optimize_s.m
% ----------------------------------------------------------

function [S1, S2, b1, b2] = osb(Hmag_sq, No, E, theta, mask, gap, bitcap,cb)

N = size(Hmag_sq,1);

b1 = zeros(N,1); b2 = zeros(N,1);
S1 = zeros(N,1); S2 = zeros(N,1);

w_1_l = 0; % lower limit on Lagrange for energy weight
w_1_u = 1; % upper limit on Lagrange for energy weight
%
% update OSB, get new upper limit in next call
[S1, S2, b1, b2] = optimize_l2(Hmag_sq, No, E(2), theta, w_1_u, mask, gap, bitcap, cb);

while sum(S1) > E(1) % too much energy
    w_1_u = 2*w_1_u; % reduce upper limmit
    [S1, S2, b1, b2] = optimize_l2(Hmag_sq, No, E(2), theta, w_1_u, mask, gap, bitcap, cb);    
end

err = 1e-1;
% err = 1;
while (w_1_u - w_1_l)>err | sum(S1)>E(1) % too much energy
    lambda_1 = (w_1_u + w_1_l)/2;
    [S1, S2, b1, b2] = optimize_l2(Hmag_sq, No, E(2), theta, lambda_1, mask, gap, bitcap,cb);    
    if (sum(S1) - E(1)) >= 0
         w_1_l = lambda_1; % increase lower limit
    else
         w_1_u = lambda_1; % decrease upper limit
    end
end

        