% Copyright (C) 2016 by Deanna Needell, Rayan Saab, and Tina Woolf

% This file is part of ''Weighted L1-Minimization for Sparse Recovery under
%                        Arbitrary Prior Information''

% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.

% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.

% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

% wrapper_rho_runWeightedL1_TwoSupportEstimates.m

%% Cleanup and Setup
clc;
close all;
clearvars;
rng('default') % seed the random number generator to produce a predictable sequence of numbers

% Parameters
n         = 256;  % signal dimension
s         = 16;   % sparsity level
mlist     = [20:10:100 120:20:250]; % number of measurements
ntrials   = 500;  % number of trials
sigma     = 0.01; % noise standard deviation
saveMat1  = 0;    % save data from runWeightedL1_TwoSupportEstimates function call
saveMat2  = 0;    % save data from this script
saveFigs1 = 0;    % save figure from runWeightedL1_TwoSupportEstimates function call
saveFigs2 = 0;    % save figure generated at the end of this script

% Single support estimate paramters
rho   = 1;
alpha = 1;
w     = [0.5 0.25];

% Two support estimate parameters
rho1   = [0 0.2 0.5 0.8 1];
rho2   = [1 0.8 0.5 0.2 0];
alpha1 = 1;
alpha2 = 1;
w1     = 0.5;
w2     = 0.25;

%% Run Weighted L1

% do runs with w=0.5 for single support estimate
for ii = 1:length(rho1)-1
   [error_WL1_single{ii}, error_WL1_double{ii}] = runWeightedL1_TwoSupportEstimates(n,s,mlist,ntrials,sigma,saveMat1,saveFigs1,...
    rho,alpha,w(1),rho1(ii),alpha1,w1,rho2(ii),alpha2,w2); 
end

% do a single run with w=0.25 for single support estimate
ii = ii+1;
[error_WL1_single{ii}, error_WL1_double{ii}] = runWeightedL1_TwoSupportEstimates(n,s,mlist,ntrials,sigma,saveMat1,saveFigs1,...
    rho,alpha,w(2),rho1(ii),alpha1,w1,rho2(ii),alpha2,w2);

%% Plot Results

figure; 
set(gcf,'WindowStyle','normal')
myMarkers = {'v','^','x','s','+','o'};
count = 0;
for ii = 1:length(rho1)  
    count = count+1;
    plot(mlist,mean(error_WL1_double{ii},2),'-o','linewidth',2,'marker',myMarkers{ii}); hold all;
    legendString{count} = ['\rho_1=' sprintf('%1.1f',rho1(ii)) ', \rho_2=' sprintf('%1.1f',rho2(ii))];
end
plot(mlist,mean(error_WL1_single{1},2),'--k>','linewidth',2);
plot(mlist,mean(error_WL1_single{end},2),'--k<','linewidth',2);
legendString{count+1} = ['w=' num2str(w(1))];
legendString{count+2} = ['w=' num2str(w(2))];
hl = legend(legendString,1);
set(hl,'fontsize',14);
xlabel('Measurements','fontsize',20);
ylabel('Mean Recovery Error','fontsize',20);
set(gca,'fontsize',20);
%title({['n=' num2str(n) ', s=' num2str(s) ', trials=' num2str(ntrials) ', rho=' num2str(rho) ', alpha=' num2str(alpha)];...
%    [ 'alpha1=' num2str(alpha1) ', w1=' num2str(w1) ', alpha2=' num2str(alpha2) ', w2=' num2str(w2)]},'fontsize',20);
grid on;
xlim([mlist(1) mlist(end)]);
set(gcf,'PaperPositionMode','Auto');
set(gcf,'Position',[837    30   547   410]);
filename = sprintf('weightedL1_TwoSupportEstimates_rho_%ds_%dn_%dtrials_%1.2fsigma_%1.2falpha',s,n,ntrials,sigma,alpha);
if (saveFigs2)
    print('-depsc','-r0',[filename '.eps']);
    saveas(gcf,[filename '.png'],'png');
    saveas(gcf,[filename '.fig'],'fig');
end

if(saveMat2)
    filename = sprintf('wrapper_weightedL1_TwoSupportEstimates_rho_%ds_%dn_%dtrials_%1.2fsigma_%1.2frho_%1.2falpha_%1.2falpha1_%1.2fw1_%1.2falpha2_%1.2fw2',...
    s,n,ntrials,sigma,rho,alpha,alpha1,w1,alpha2,w2);
   save([filename '.mat']); 
end