%%
cd asp-master
addpath(pwd)
as_setup
cd ../

%%
clear
close all
clc
%%
rng(1)
N = 512;
k = 20;
D = dct(eye(N));
Dinv = idct(eye(N));
A = [eye(N) D];

x = zeros(2*N,1);
p = randperm(2*N,k);
x(p) = 2*randn(k,1);
b = A*x;

alpha = Dinv*b;

opts          = as_setparms;
opts.loglevel = 1;
inform        = [];

[xhat,inform] = as_bpdn(A,b,[],opts,inform); 


%% now with noise
rng(1)
sigma = 0.01;
N = 512;
k = 20;
D = dct(eye(N));
Dinv = idct(eye(N));
A = [eye(N) D];

x = zeros(2*N,1);
p = randperm(2*N,k);
x(p) = 2*randn(k,1);
b = A*x + sigma*randn(N,1);

alpha = Dinv*b;

opts          = as_setparms;
opts.loglevel = 1;
inform        = [];
lambda = .05;
[xhat,inform] = as_bpdn(A,b,lambda,opts,inform);

%% now lets do some 1D compressed sensing, redundant
rng(1)
N = 512;
k = 5;
D = dct(eye(N));
Dinv = idct(eye(N));
A = [eye(N) D];

x = zeros(2*N,1);
p = randperm(2*N,k);
x(p) = randn(k,1);
b = A*x;

s = ceil(2*k*log(N));
G = randn(s,N);
b2 = G*b;

opts          = as_setparms;
opts.loglevel = 1;
inform        = [];

[xhat,inform] = as_bpdn(G*A,b2,[],opts,inform);
bhat = A*xhat;

%% now lets do some 1D compressed sensing, incoherent
rng(1)
N = 512;
k = 5;
D = dct(eye(N));
Dinv = idct(eye(N));
A = D;

x = zeros(N,1);
p = randperm(N,k);
x(p) = randn(k,1);
b = A*x;

s = ceil(k*log(N));
I = eye(N);
idx = randperm(N,s);
DS = I(idx,:);
b2 = DS*b;

bt = zeros(N,1);
bt(idx) = b(idx);

opts          = as_setparms;
opts.loglevel = 1;
inform        = [];

[xhat,inform] = as_bpdn(DS*A,b2,[],opts,inform);



