在网上找的OMP算法,其中L是稀疏度,即每个信号对应的系数(系数矩阵的一列)中非零元的个数最大值,默认为该列元素的个数(也等于字典的原子个数)。K-SVD算法中的字典原子个数一般远大于信号维度,如输入143×143的图片,原子个数要远大于143×143,这样才能保证其为超完备字典.
function [A]=OMP(D,X,L);
% Sparse coding of a group of signals based on a given
% dictionary and specified number of atoms to use.
% input arguments:
% D - the dictionary (its columns MUST be normalized).
% X - the signals to represent
% L - the max. number of coefficients for each signal.
% output arguments:
% A - sparse coefficient matrix.
%=============================================
[n,P]=size(X);
[n,K]=size(D);
for k=1:1:P,
a=[];
x=X(:,k);
residual=x;
indx=zeros(L,1);
for j=1:1:L,
proj=D'*residual;
[maxVal,pos]=max(abs(proj));
pos=pos(1);
indx(j)=pos;
a=pinv(D(:,indx(1:j)))*x;
residual=x-D(:,indx(1:j))*a;
if sum(residual.^2) < 1e-6
break;
end
end;
temp=zeros(K,1);
temp(indx(1:j))=a;
A(:,k)=sparse(temp);
end;
return;