-
Notifications
You must be signed in to change notification settings - Fork 1
/
OMP.m
34 lines (34 loc) · 952 Bytes
/
OMP.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function [A]=OMP(D,Y,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).
% Y - the signals to represent
% L - the max. number of coefficients for each signal.
% output arguments:
% A - sparse coefficient matrix.
%=============================================
[n,P]=size(Y);
[n,K]=size(D);
for k=1:1:P,
a=[];
x=Y(:,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;