% L1 minimization demonstration
% min || x ||_1  s.t. Phi*x = y
%
% by Wing-Kin Ma, Dec, 2008

close all; clear all; clc

N= 200; %data length, or dim of x
M= 100; %no. of measurement, or dim. of y
prob= .65; %factor for controlling the sparsity (1- not sparse, 0.5- all zero)

p= prob*rand(N,1); p= round(p);
x= p.*randn(N,1);

%-----
N= length(x); 
Phi= randn(M,N);
y= Phi*x;

cvx_begin
    variable hx(N);
    minimize( norm( hx, 1 ) );
    subject to
        Phi*hx == y;
cvx_end

figure(1); clf;
subplot(211);
plot(x,'r');
title('1-norm minimization','fontsize',16);
hold on;
plot(hx,'b');
legend('Original','Reconstructed');

%---
W= Phi*Phi'; u= W\y; hx_ls= Phi'*u; %reconstruction by 2-norm minimization

subplot(212);
plot(x,'r');
title('2-norm minimization','fontsize',16);
hold on;
plot(hx_ls,'b');




