

clear;clc;


% problem dimension
N = 25;       % row number of the dictionary matrix 
M = 80;      % column number of the dictionary matrix
D = 5;        % number of active sources
L = 1;
iterNum = 1000; % number of experiments

LAMBDA = logspace(-3,-0.5,31);


for it = 1 : iterNum
    fprintf('\n\nRunning %d:\n',it);

    % dictionary matrix with columns draw uniformly from the surface of a unit hypersphere
    Phi = randn(N,M);
    Phi = Phi./(ones(N,1)*sqrt(sum(Phi.^2)));
   
    % Generate D nonzero coefficients  
    nonzeroW = sign(randn(D,1)).* ( rand(D,1)*0.5 + 0.5 );

    % The locations of nonzero rows are randomly chosen
    ind = randperm(M);
    indice = ind(1:D);
    Wgen = zeros(M,L);
    Wgen(indice,:) = nonzeroW;
    

    % noiseless signal
    signal = Phi * Wgen;

    % observation noise
    %stdnoise = std(signal)*10^(-SNR/20);
    stdnoise = 0.1;
    noise = randn(N,L);
    %noise = noise./( sqrt(sum(noise.^2,2)) * ones(1,L) );
    noise = noise * stdnoise;

    % noisy signal
    T = signal + noise;

    for n = 1 : length(LAMBDA)

        lambda = LAMBDA(n);    % 

      
        %============================ MSBL ==========================
        epsilon =  sqrt(lambda)*sqrt(N)*sqrt(1 + 2*sqrt(2)/sqrt(N));
        x0 = Phi'*T;
        Weight4 = l1qc_logbarrier(x0, Phi, [], T, epsilon, 1e-3);
        
        % Failure rate
        F4 = perfSupp(Weight4,indice,'firstlargest', D);
        fail_L1(it,n) = (F4~=1);

        % MSE
        mse_L1(it,n) = (norm(Wgen - Weight4,'fro')/norm(Wgen,'fro'))^2;
  
    end

    mean(fail_L1)
    mean(mse_L1)
    save data2_L1;
end

