n = 4096
m = 64
s = 15
axf = [1 n -1.25 1.25];
axd = [1 m -1 1];
set(0,'DefaultTextFontSize', 14)
set(0,'DefaultLineMarkerSize', 15)
D = zeros(n,m);
for i = 1:m
D(:,i) = cos((i - 1) * pi * (1:n) / n);
end
figure(1)
imagesc(D)
title('Dictionary elements')
colormap(gray(256))
figure(2)
subplot(4,1,1)
plot(D(:,1),'k-')
axis(axf)
title('Dictionary elements')
subplot(4,1,2)
plot(D(:,2),'k-')
axis(axf)
subplot(4,1,3)
plot(D(:,3),'k-')
axis(axf)
subplot(4,1,4)
plot(D(:,4),'k-')
axis(axf)
rng(42)
tau = 0.6180339887498948;
k = sort(floor(((1:s)' * tau - floor((1:s)' * tau)) * n + 1));
A = zeros(s,m);
for i = 1:s
A(i,:) = D(k(i),:);
end
figure(3)
imagesc(A)
title('Sampled dictionary elements')
colormap(gray(256))
figure(4)
subplot(4,1,1)
plot(k, A(:,1),'k.')
axis(axf)
title('Sampled dictionary elements')
subplot(4,1,2)
plot(k, A(:,2),'k.')
axis(axf)
subplot(4,1,3)
plot(k, A(:,3),'k.')
axis(axf)
subplot(4,1,4)
plot(k, A(:,4),'k.')
axis(axf)
x = zeros(m,1);
x(1) = 0.1;
x(2) = 0.75;
x(7) = 0.25;
x(23) = 0.1;
sparsity = sum(x > 0)
f = sum(D*x,2);
fn = f + randn(n,1) * 0.05;
b = fn(k);
figure(5)
subplot(2,1,1)
plot(fn,'k-','color',[1,1,1]*0.75)
hold on
plot(sum(D*x,2),'k-')
plot(k,b,'k.')
hold off
axis(axf)
title('Original signal and noisy samples')
subplot(2,1,2)
stem(x,'k.')
axis(axd)
title('Original dictionary coefficients')
x = ones(m,1);
for l=1:1000
x = A' * ((A * ((abs(x) * ones(1,s)).*A')) \ b) .* abs(x);
end
figure(6)
subplot(2,1,1)
stem(x,'k.')
axis(axd)
title('Recovered coefficients')
subplot(2,1,2)
plot(f,'k-','color',[1,1,1] * 0.75)
hold on
plot(sum(D*x,2),'k-')
plot(k,b,'k.')
hold off
axis(axf)
title('Reconstructed signal')
[sortedValues,sortIndex] = sort(abs(x),'descend');
x(sortIndex((sparsity + 1):m)) = 0;
figure(7)
subplot(2,1,1)
stem(x,'k.')
axis(axd)
title('Tresholded coefficients')
subplot(2,1,2)
plot(f,'k-','color',[1,1,1] * 0.75)
hold on
plot(sum(D*x,2),'k-')
plot(k,b,'k.')
hold off
axis(axf)
title('Reconstructed signal')