├── Greedy_Algorithms_OMP_CoSaMP.m ├── README.md └── testing_CoSaMPL.m /Greedy_Algorithms_OMP_CoSaMP.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | %%%%%%%%%%%%%%%%%%%%%%%% CoSaMP ALGORITHM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 4 | %INPUTS: Sensing Matrix A, linear measurments y, dimensions of A (mxN), 5 | %sparsity s, solution x (to calculate rel err), nb_terations we want to 6 | %perform 7 | %OUTPUTS: X^# (approxiamtion vector), and current_iteration 8 | function [x_sharp, residual_plot, current_iteration]=COSAMP(A,y,m,N,s,nb_iterations,x) 9 | x_sharp=zeros(N,1); 10 | residual_plot=zeros(nb_iterations,1); 11 | % for k=1:nb_iterations 12 | current_iteration = 0; 13 | error=10; 14 | while (error > 10e-6) && (current_iteration<120) 15 | 16 | %CoSaMP 1: U^(n+1) = supp(x^n) Union L_2s(A*(y-Ax^n)) 17 | residual=y-A*x_sharp; 18 | U = union(find(x_sharp),L_s(A'*residual,2*s)); 19 | U = unique(U,'rows',"sorted"); 20 | 21 | %CoSaMP 2: u^(n+1) in argmin{|y-Az|_2 : supp(z) subset U^(n+1)} 22 | A_U = restrict_matrix(A,U,m); 23 | 24 | z = A_U\y; 25 | % z = lsqr(A_U,y); 26 | u = unrestrict_vector(z,U,N); 27 | 28 | %CoSaMP 3: x^(n+1) = H_s(u^(n+1)) 29 | H = L_s(u,s); 30 | x_sharp = H_s(H,u,N); 31 | % residual_plot(k,1) = norm(A*x_sharp-y); 32 | error=norm(x-x_sharp); 33 | current_iteration=current_iteration+1; 34 | % residual_plot(k,1) = norm(x-x_sharp)/norm(x); 35 | end 36 | end 37 | 38 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 39 | %%%%%%%%%%%%%%%%%%%%%%%%%% CoSaMPL ALGORITHM %%%%%%%%%%%%%%%%%%%%%%%%%%% 40 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 41 | %INPUTS: Sensing Matrix A, linear measurments y, dimensions of A (mxN), 42 | %sparsity vector s, levels M, and solution x (to calculate rel err) 43 | %OUTPUTS: X^# (approxiamtion vector), and current_iteration 44 | function [x_sharp,current_iteration]= COSAMPL(A,y,m,N,s,M,x) 45 | x_sharp=zeros(N,1); 46 | current_iteration = 0; 47 | error=10; 48 | while (error > 10e-6) && (current_iteration<120) 49 | 50 | %CoSaMP 1: U^(n+1) = supp(x^n) Union L_2s(A*(y-Ax^n)) 51 | residual=y-A*x_sharp; 52 | U = union(find(x_sharp),L_s_M(A'*residual,2*s,M)); 53 | U = unique(U,'rows',"sorted"); 54 | 55 | %CoSaMP 2: u^(n+1) in argmin{|y-Az|_2 : supp(z) subset U^(n+1)} 56 | A_U = restrict_matrix(A,U,m); 57 | 58 | z = A_U\y; 59 | % z = lsqr(A_U,y); 60 | u = unrestrict_vector(z,U,N); 61 | 62 | %CoSaMP 3: x^(n+1) = H_s(u^(n+1)) 63 | H = L_s_M(u,s,M); 64 | x_sharp = H_s_M(H,u,N); 65 | % residual_plot(k,1) = norm(A*x_sharp-y); 66 | error=norm(x-x_sharp); 67 | current_iteration=current_iteration+1; 68 | % residual_plot(k,1) = norm(x-x_sharp)/norm(x); 69 | end 70 | end 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 72 | %%%%%%%%%%%%%%%%%%%%%%%%%%% OMP ALGORITHM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 73 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 74 | %INPUTS: Sensing Matrix A, linear measurments y, dimensions of A (mxN),nb_terations we want to 75 | %perform 76 | %OUTPUTS: X^# (approxiamtion vector), and current iteration k, and a plot 77 | %of the residual 78 | 79 | function [x_sharp, residual_plot,k] = OMP(A,y,N,m,nb_iterations) 80 | residual_plot=zeros(nb_iterations,1); 81 | residual=y;S=[]; 82 | for k=1:nb_iterations 83 | vals=A'*(residual); 84 | [M,j] = max(abs(vals)); 85 | S(length(S)+1,1)=j; 86 | S=unique(S,'rows',"sorted"); 87 | A_s=restrict_matrix(A,S,m); 88 | z=A_s\y; 89 | residual=y-A_s*z; 90 | residual_plot(k,1)=norm(residual); 91 | end 92 | 93 | x_sharp=unrestrict_vector(z,S,N); 94 | 95 | end 96 | 97 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 98 | %%%%%%%%%%%%%%%%%%%%%%%%%%% OMPL ALGORITHM %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 99 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 100 | %INPUTS: Sensing Matrix A, linear measurments y, dimensions of A (mxN), 101 | %sparsity vector s, and levels M. 102 | %OUTPUTS: X^# (approxiamtion vector), and current iteration k 103 | function [x_sharp,k] = OMPL(A,y,s,M,m,N) 104 | residual=y;S=[]; 105 | nb_iterations=sum(s,'all'); 106 | for k=1:nb_iterations+nb_iterations/2 107 | vals=A'*(residual); 108 | [s,j]=argmax(vals,s,M,S); 109 | S=union(S,j); 110 | S=unique(S,'rows',"sorted"); 111 | A_s=restrict_matrix(A,S',m); 112 | z=A_s\y; 113 | x_sharp=unrestrict_vector(z,S',N); 114 | %remove smallest entry of z here when sum(s,'all')=0, to decide which 115 | %is the smaller, do A*resid, remoce that one 116 | residual=y-A*x_sharp; 117 | end 118 | end 119 | 120 | 121 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 122 | %%%%%%%%%%%%%%%%%%%%%%%%%%% AUX FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 123 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 124 | 125 | function plot_residuals_vs_iterations(residual,type) 126 | semilogy(residual) 127 | ylim([10e-16 10e-6]) 128 | title('Residual as a function of k using') 129 | legend('2-norm of residual') 130 | xlabel('k iterations') 131 | ylabel('$\|y-Ax^{(k)}\|_2$','Interpreter','latex') 132 | end 133 | 134 | %L_{s,M} operator, selects s largest entries, of each level 135 | function max_index_set = L_s_M(z,s,M) 136 | M=union(0,M); 137 | max_index_set =zeros(sum(s,'all'),1); 138 | counter = 1; 139 | for i=2:length(M) 140 | lower_bound = (M(i-1)+1); 141 | upper_bound= M(i); 142 | current_level=abs(z(lower_bound:upper_bound)); 143 | current_sparsity_level=s(i-1); 144 | for k=1:current_sparsity_level 145 | [Max,j]=max(current_level); 146 | max_index_set(counter,1)=j+M(i-1); 147 | current_level(j)=0; 148 | counter = counter+1; 149 | end 150 | 151 | end 152 | max_index_set=sort(max_index_set, 'ascend'); 153 | end 154 | 155 | %argmax function for in levels, used in OMP only 156 | function [s,j] = argmax(z,s,M,S) 157 | M=union(0,M); 158 | for i=2:length(M) 159 | lower_bound = (M(i-1)+1); 160 | upper_bound = M(i); 161 | if(sum(s,'all')==0) 162 | z_restricted=zeros(length(S),1); 163 | for a=1:length(S) 164 | z_restricted(a)=z(S(a)); 165 | end 166 | % z_removed_index=H_s(S,z,256); 167 | [m,index]=min(abs(z_restricted)); 168 | % z_restricted(index)=0; 169 | index_to_remove=S(index); 170 | for n=1:length(M) 171 | if(index_to_remove>M(n)) 172 | else 173 | s(n-1)=s(n-1)+1; 174 | break 175 | end 176 | end 177 | end 178 | if(s(i-1) == 0) 179 | z(lower_bound:upper_bound)=0; 180 | end 181 | end 182 | [maximum,j]=max(abs(z)); 183 | for i=1:length(M) 184 | if(j>M(i)) 185 | else 186 | s(i-1)=s(i-1)-1; 187 | break 188 | end 189 | end 190 | end 191 | 192 | %checks coherance of the matrix A 193 | function coherance = check_coherance(A) 194 | dimension_A=size(A); 195 | j=1; 196 | total_inner_products=nchoosek(dimension_A(2),2); 197 | inner_products=zeros(total_inner_products,1); 198 | for i=1:dimension_A(2)-1 199 | for k=i+1:dimension_A(2) 200 | inner_products(j,1)=dot(A(:,i),A(:,k)); 201 | j=j+1; 202 | end 203 | end 204 | [M,index]=max(abs(inner_products)); 205 | coherance=M; 206 | end 207 | 208 | %check if matrix A staisfies the RIP of order s with a given RIC 209 | function RIC=check_RIP(RIC,A,x) 210 | if (1-RIC)*norm(x)^2 <=norm(A*x)^2 && norm(A*x)^2 <= (1+RIC)*norm(x)^2 211 | RIC=true; %(1) 212 | else 213 | RIC=false; %(0) 214 | end 215 | end 216 | %L_{s} operator, selects s largest entries 217 | function max_index_set = L_s(z,s) 218 | max_index_set =[]; 219 | for i=1:s 220 | z=abs(z); 221 | [M,j]=max(z); 222 | max_index_set(length(max_index_set)+1,1)=j; 223 | z(j)=0; 224 | end 225 | % max_index_set=unique(max_index_set,'rows',"sorted"); 226 | max_index_set=sort(max_index_set, 'ascend'); 227 | end 228 | 229 | %Hard thresholding operator 230 | function x = H_s(H,u,N) 231 | x=zeros(N,1); 232 | for i=1:length(H) 233 | x(H(i))=u(H(i)); 234 | end 235 | end 236 | 237 | %fn to restrict a matrix 238 | function matrix_to_restrict = restrict_matrix(matrix, index_set,m) 239 | % index_set=index_set'; 240 | matrix_to_restrict = zeros(m,length(index_set)); 241 | for i=1:length(index_set) 242 | matrix_to_restrict(:,i)=matrix(:,index_set(i,1)); 243 | end 244 | end 245 | 246 | %function to unrestrict a vector 247 | function vector_to_unrestrict =unrestrict_vector(vector,index_set,N) 248 | vector_to_unrestrict=zeros(N,1); 249 | for i=1:length(index_set) 250 | vector_to_unrestrict(index_set(i,1))=vector(i,1); 251 | end 252 | end 253 | 254 | %fn to generate a random signal 255 | function y=generate_signal(m,N) 256 | y=zeros(m,1); 257 | for i=1:m 258 | y(i,1)=sin((12*pi)*((i-1)/N- 1.1)) + 2*max(1-abs(i/6 -7),0); 259 | end 260 | 261 | end 262 | 263 | %Hard thresholding operator for in level model 264 | function x = H_s_M(H,u,N) 265 | x=zeros(N,1); 266 | for i=1:length(H) 267 | x(H(i))=u(H(i)); 268 | end 269 | end 270 | 271 | 272 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Compressed-Sensing 2 | Testing greedy algorithms OMP and COSAMP using structured sparsity 3 | -------------------------------------------------------------------------------- /testing_CoSaMPL.m: -------------------------------------------------------------------------------- 1 | %Phase Transition Plot - CoSaMPL 4 levels 2 | clear all; 3 | close all; 4 | N=256; 5 | probability=zeros(15,23); %matrix with number of iterations until signal recovered 6 | sparsities=4:4:60; 7 | m_dim=20:10:240; 8 | for z=1:50 9 | z 10 | for k=1:23 %m 11 | m=m_dim(k); 12 | for j=1:15%s 13 | sparsity=sparsities(j); 14 | A = randn(m,N)/sqrt(m); %normally distributed random (Gaussian) matrix 15 | x = zeros(N,1); 16 | while (nnz(x(1:N/4))<(sparsity/2)) 17 | x(randi([1,N/4],1)) = randi([1,10])/sqrt(m); 18 | end 19 | while (nnz(x(N/2+1:3*N/4))<(sparsity/2)) 20 | x(randi([N/2+1,3*N/4],1)) = randi([1,10])/sqrt(m); 21 | end 22 | 23 | y=A*x; 24 | [x_sharp_cosampl,iteration]=COSAMPL(A,y,m,N,[sparsity/2,0,sparsity/2,0],[N/4,N/2,3*N/4,N],x); 25 | rel_error=norm(x-x_sharp_cosampl)/norm(x); 26 | if rel_error < 10e-4 27 | probability(16-j,k)=probability(16-j,k)+1; 28 | end 29 | end 30 | end 31 | end 32 | %phase transition plot 33 | probability=probability/50; 34 | imagesc(1-probability) 35 | title('CoSaMPL-4 levels') 36 | xlabel('size of m'); 37 | ylabel('total sparsity'); 38 | set(gca,'Xtick',1:12,'XTickLabel',{'20', '40', '60', '80','100', '120', '140', '160','180','200','220','240'}) 39 | set(gca,'Ytick',1:15,'YTickLabel',{'60', '56', '52', '48','44', '40', '36', '32','28','24','20','16','12','8','4'}) 40 | colorbar 41 | colormap(gray) 42 | 43 | %Phase Transition Plot - CoSaMPL 2 levels 44 | clear all; 45 | close all; 46 | 47 | N=256; 48 | probability=zeros(15,23); %matrix with number of iterations until signal recovered 49 | sparsities=4:4:60; 50 | m_dim=20:10:240; 51 | for z=1:50 52 | z 53 | for k=1:23%m 54 | m=m_dim(k); 55 | for j=1:15%s 56 | sparsity=sparsities(j); 57 | A = randn(m,N)/sqrt(m); %normally distributed random (Gaussian) matrix 58 | x = zeros(N,1); 59 | while (nnz(x(1:N/2))<(3*sparsity/4)) 60 | x(randi([1,N/2],1)) = randi([1,10])/sqrt(m); 61 | end 62 | while (nnz(x(N/2+1:N))<(sparsity/4)) 63 | x(randi([N/2+1,N],1)) = randi([1,10])/sqrt(m); 64 | end 65 | 66 | y=A*x; 67 | [x_sharp_cosampl,iteration]=COSAMPL(A,y,m,N,[3*sparsity/4,sparsity/4],[N/2,N],x); 68 | rel_error=norm(x-x_sharp_cosampl)/norm(x); 69 | if rel_error < 10e-4 70 | probability(16-j,k)=probability(16-j,k)+1; 71 | end 72 | end 73 | end 74 | end 75 | %phase transition plot 76 | probability=probability/50; 77 | imagesc(1-probability) 78 | title('CoSaMPL-2 levels') 79 | xlabel('size of m'); 80 | ylabel('total sparsity'); 81 | set(gca,'Xtick',1:12,'XTickLabel',{'20', '40', '60', '80','100', '120', '140', '160','180','200','220','240'}) 82 | set(gca,'Ytick',1:15,'YTickLabel',{'60', '56', '52', '48','44', '40', '36', '32','28','24','20','16','12','8','4'}) 83 | colorbar 84 | colormap(gray) --------------------------------------------------------------------------------