% The true signal z is m-sparse in the domain of PSI, having coefficients% g, m of which are nonzero and the remaining (M-m) of which are zero.%% z = PSI * g%% The coloumns of PSI are a basis for the domain of sparsity.%% The signal z is subsampled by a matrix PHI, where each column of PHI% measures z, ultimately yielding a collection of measurements y.%% y = PHI' * z% = PHI' * PSI * g%% In this example, entries of z that are measured are populated into y% while those not measured are left as zero.% % Given y, PHI, and PSI, we seek to recover g (and effectively z).% % PHI: L-by-M measurement matrix% PSI: L-by-L sparsity basis% y: M-by-1 data vector (observations/measurements)% m: degree of sparsity of true signal in domain of PSI% % g: L-by-1 estimate of sparsity coefficients% H: L-by-1 sparse channel estimate (PSI * g)clc;clearvars;% dimensionality of true signalL=100;% let's create a sparsity matrixA=rand(L,L);[Q,R]=qr(A);% take orthonormal matrixPSI=Q;% sparsity basis% sparsity levelm=5;% random sampler againM=round(2*m^2);% number of samples to takeB=zeros(L,M);idx_samples=randperm(L,M);r=idx_samples;c=1:M;B(sub2ind(size(B),r,c))=1;% only sample a portionPHI=B;% sparsity coefficientss=zeros(L,1);s(randperm(L,m))=rand(m,1);% only m are nonzero% true signalz=PSI*s;% measured signaly=PHI'*z;% estimate sparsity coefficients and resulting signal[g,H]=omp_based_cs(PHI,PSI,y,m);% let's see how we didfigure(1);subplot(3,1,1);plot(z,'-r');holdon;stem(idx_samples,y,'-b');holdoff;gridon;xlabel('Sample');ylabel('Amplitude');legend('Truth','Observed','Location','Best');title('Signal');subplot(3,1,2);stem(s,'ro');holdon;stem(g,'bx');holdoff;gridon;xlabel('Sample');ylabel('Amplitude');legend('Truth','Predicted','Location','Best');title('Sparse Coefficients');subplot(3,1,3);stem(z,'ro');holdon;stem(H,'bx');holdoff;gridon;xlabel('Sample');ylabel('Amplitude');legend('Truth','Reconstructed','Location','Best');title('Signal Reconstruction');
function[g,h]=omp_based_cs(PHI,PSI,y,m)% OMP_BASED_CS Compressed sensing estimation using OMP.%% We observe the true signal (PSI * g) via measurements made with PHI.%% y = PHI' * PSI * g % % Args:% PHI: L-by-M measurement matrix% PSI: L-by-L sparsity basis % y: M-by-1 data vector (observations)% m: sparsity level of truth% % Returns:% g: L-by-1 estimate of sparsity coefficients% h: L-by-1 sparse channel estimate (PSI * g)%% Reference:% "Signal Recovery From Random Measurements Via Orthogonal Matching % Pursuit" by Joel A. Tropp and Anna C. Gilbert.r=y;% init residualJ=[];% init index setB=[];% init atomsA=PHI'*PSI;% for each sparsity degreefori=1:midx=argmax(A'*r);% greedy selectionJ=[Jidx];% update index setB=[BA(:,idx)];% update our atomsx=pinv(B)*y;% projection weightsa=B*x;% projectr=y-a;% calculate new residualend% sparse coefficients[~,L]=size(PSI);g=zeros(L,1);g(J,1)=x;% signal reconstructionh=PSI*g;return