Omp 算法
% 1-D 信号压缩传感的实现(正交匹配追踪法 Orthogonal Matching Pursuit)
% 测量数 M>=K*log(N/K),K 是稀疏度,N 信号长度,可以近乎完全重构
% 编程人--香港大学电子工程系 沙威 Email: wsha@eee.hku.hk
% 编程时间:2008 年 11 月 18 日
% 文档下载: http://www.eee.hku.hk/~wsha/Freecode/freecode.htm
% 参考文献:Joel A. Tropp and Anna C. Gilbert
% Signal Recovery From Random Measurements Via Orthogonal Matching
% Pursuit,IEEE TRANSACTIONS ON INFORMATION THEORY, VOL. 53, NO. 12,
% DECEMBER 2007.
clc;clear
%% 1. 时域测试信号生成
% 稀疏度(做 FFT 可以看出来)
K=7;
% 信号长度
N=256;
% 测量数(M>=K*log(N/K),至少 40,但有出错的概率)
M=64;
% 信号频率 1
f1=50;
% 信号频率 2
f2=100;
% 信号频率 3
f3=200;
% 信号频率 4
f4=400;
fs=800;
% 采样频率
ts=1/fs; % 采样间隔
Ts=1:N;
% 采样序列
x=0.3*cos(2*pi*f1*Ts*ts)+0.6*cos(2*pi*f2*Ts*ts)+0.1*cos(2*pi*f3*Ts*ts)+0.9*cos(2*pi*f4*Ts*ts)
; % 完整信号
%% 2. 时域信号压缩传感
Phi=randn(M,N);
s=Phi*x.';
% 测量矩阵(高斯分布白噪声)
% 获得线性测量
%% 3. 正交匹配追踪法重构信号(本质上是 L_1 范数最优化问题)
m=2*K;
Psi=fft(eye(N,N))/sqrt(N);
T=Phi*Psi';
矩阵)
% 傅里叶正变换矩阵
% 算法迭代次数(m>=K)
% 恢复矩阵(测量矩阵*正交反变换
hat_y=zeros(1,N);
Aug_t=[];
r_n=s;
for times=1:m;
代次数为 K)
for col=1:N;
% 待重构的谱域(变换域)向量
% 增量矩阵(初始值为空矩阵)
% 残差值
% 迭代次数(有噪声的情况下,该迭
% 恢复矩阵的所有列向量
product(col)=abs(T(:,col)'*r_n);
% 恢复矩阵的列向量和残差的投影系数
(内积值)
end
[val,pos]=max(product);
Aug_t=[Aug_t,T(:,pos)];
T(:,pos)=zeros(M,1);
为了简单我把它置零)
% 最大投影系数对应的位置
% 矩阵扩充
% 选中的列置零(实质上应该去掉,
aug_y=(Aug_t'*Aug_t)^(-1)*Aug_t'*s;
r_n=s-Aug_t*aug_y;
pos_array(times)=pos;
% 最小二乘,使残差最小
% 残差
% 纪录最大投影系数的位置
end
hat_y(pos_array)=aug_y;
hat_x=real(Psi'*hat_y.');
%% 4. 恢复信号和原始信号对比
figure(1);
hold on;
plot(hat_x,'k.-')
plot(x,'r')
legend('Recovery','Original')
norm(hat_x.'-x)/norm(x)
% 重构的谱域向量
% 做逆傅里叶变换重构得到时域信号
% 重建信号
% 原始信号
% 重构误差
%A-稀疏系数矩阵
%D-字典/测量矩阵(已知)
%X-测量值矩阵(已知)
%K-稀疏度
function A=OMP(D,X,L)
[n,P]=size(X);
[n,K]=size(D);
for k=1:P
a=[];
x=X(:,k);
residual=x;%残差
indx=zeros(L,1);%索引集
for j=1:L
proj=D'*residual;%D 转置与 residual 相乘,得到与 residual 与 D 每一列的内积值
pos=find(abs(proj)==max(abs(proj)));%找到内积最大值的位置
pos=pos(1);%若最大值不止一个,取第一个
indx(j)=pos;%将这个位置存入索引集的第 j 个值
a=pinv(D(:,indx(1:j)))*x;%indx(1:j)表示第一列前 j 个元素
residual=x-D(:,indx(1:j))*a;
end
temp=zeros(K,1);
temp(indx)=a;
A(:,k)=temp;%只显示非零值及其位置
end
最近学习 K-SVD 算法的过程中,稀疏编码部分使用了 OMP 追踪算法,特作此总结。
求解问题:
其中 D 是过完备的字典,已经给定,Y 是原始信号,X 待求。
OMP 算法的本质思想是:以贪婪迭代的方法选择 D 的列,使得在每次迭代的过程中所选择
的列与当前冗余向量最大程度的相关,从原始信号向量中减去相关部分并反复迭代,只到迭
代次数达到稀疏度 K,停止迭代。
核心算法步骤如下:
相关 Matlab 代码如下:
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.
% ||X-DA||
% 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);
%the kth signal sample
residual=x;
%initial the residual vector
indx=zeros(L,1);
%initial the index vector
%the jth iter
for j=1:1:L,
%compute the inner product
proj=D'*residual;
%find the max value and its index
[maxVal,pos]=max(abs(proj));
%store the index
pos=pos(1);
indx(j)=pos;
%solve the Least squares problem.
a=pinv(D(:,indx(1:j)))*x;
%compute the residual in the new dictionary
residual=x-D(:,indx(1:j))*a;
%the precision is fill our demand.
%
%
%
if sum(residual.^2) < 1e-6
break;
end
end;
temp=zeros(K,1);
temp(indx(1:j))=a;
A(:,k)=sparse(temp);
end;
return;