-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathksvddemo.m
81 lines (55 loc) · 1.62 KB
/
ksvddemo.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function ksvddemo
%KSVDDEMO K-SVD training demonstration.
% KSVDDEMO generates random sparse examples over a random overcomplete
% dictionary, adds noise, and uses K-SVD to recover the dictionary from
% the examples.
%
% To run the demo, type KSVDDEMO from the Matlab prompt.
%
% See also KSVDDENOISEDEMO.
% Ron Rubinstein
% Computer Science Department
% Technion, Haifa 32000 Israel
% ronrubin@cs
%
% May 2009
disp(' ');
disp(' ********** K-SVD Demo **********');
disp(' ');
disp(' This demo generates a random dictionary and random sparse examples over this');
disp(' dictionary. It then adds noise to the examples, and uses K-SVD to recover');
disp(' the dictionary. The demo plots the convergence of the K-SVD target function,');
disp(' and computes the fraction of correctly recovered atoms.');
disp(' ');
% dictionary dimensions
n = 20;
m = 50;
% number of examples
L = 1500;
% sparsity of each example
k = 3;
% noise power (dB)
snr = 20;
%% generate random dictionary and data %%
D = normcols(randn(n,m));
Gamma = zeros(m,L);
for i = 1:L
p = randperm(m);
Gamma(p(1:k),i) = randn(k,1);
end
X = D*Gamma;
X = normcols(X) + 10^(-snr/20)*normcols(randn(n,L));
%% run k-svd training %%
params.data = X;
params.Tdata = k;
params.dictsize = m;
params.iternum = 30;
params.memusage = 'high';
[Dksvd,g,err] = ksvd(params,'');
%% show results %%
figure; plot(err); title('K-SVD error convergence');
xlabel('Iteration'); ylabel('RMSE');
printf(' Dictionary size: %d x %d', n, m);
printf(' Number of examples: %d', L);
[dist,ratio] = dictdist(Dksvd,D);
printf(' Ratio of recovered atoms: %.2f%%\n', ratio*100);