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;%j=L了
A(:,k)=sparse(temp); %使用稀疏矩阵存储
end;
return;
%=============================================
% 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;%j=L了
A(:,k)=sparse(temp); %使用稀疏矩阵存储
end;
return;