% Example computations related to one-dimensional deconvolution.
% Demonstration of generalized Tikhonov regularization.
%
% The following routines must be computed before this one:
% DC2_discretedata_comp.m
%
% Jennifer Mueller and Samuli Siltanen, October 2012

% Regularization parameter
alpha = .5;

% Plot parameters
lwidth = .5;
thickline = 2;
fsize = 20;
msize = 4;

% Load data computed using a very fine grid, avoiding inverse crime
load DC2_discretedata A x xx n m mn mIC sigma

% Choose data 
data = mn;

% Construct regularization matrix of size (n)x(n). This implements difference
% between consecutive values assuming periodic boundary conditions.
L = eye(n);
L = L-[L(:,end),L(:,1:end-1)];

% Construct original signal for comparison
signal = DC_target(xx);

% Reconstruct from noisy data using Tikhonov regularization.
syst  = [A; sqrt(alpha)*L];
m_aug = [data(:);zeros(n,1)];
rec   = syst\m_aug;

% Calculate relative error
truth  = DC_target(x);
relerr = round(norm(rec(:)-truth(:))/norm(truth(:))*100);

% Take a look at the reconstruction
figure(6)
clf
plot(xx,signal,'k','linewidth',lwidth)
hold on
plot(x,rec,'r','linewidth',3*lwidth)
text(0.9,1.5,[num2str(relerr),'%'],'fontsize',fsize)

% Axis settings
axis([0 1 -.5 2])
set(gca,'xtick',[0 1/2 1],'fontsize',fsize)
set(gca,'ytick',[0  1 2],'fontsize',fsize)
set(gca,'PlotBoxAspectRatio' , [2 1 1])
box off
