% demo.m
%
% Savitzky-Golay Filter and Least-Squares Filte

% size of the data
n = 1000;

% mean deviation of noise
delta = 0.1;

% Parameters of the Savitzky-Golay Filter
nl = 16;        % number of points to the left
nr = 16;        % number of points to the right
M  = 4;         % degree of the least squares polynomial

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% generate smooth function
x = [0:n-1]'/(n-1);
F = exp (- 100*(x - 1/5).^2) + exp (-  500*(x - 2/5).^2) + ...
    exp (-2500*(x - 3/5).^2) + exp (-12500*(x - 4/5).^2);

% plot smooth data
plot (x, F, 'b');
title (['smooth data (n = ' int2str(n) ')']);
pause (3);

% generate noisy data
randn ('seed', 0);
f = F + delta * randn (size (x));

% plot noisy data
plot (x, f, 'r', x, F, 'b');
title (['noisy data (n = ' int2str(n) ')']);
pause (3);

% smooth by Savitzky-Golay filter
t0 = cputime;
g = SavGol (f, nl, nr, M);
t1 = cputime;

% plot results
plot (x (1+nl:n-nr), g (1+nl:n-nr), 'r', x, F, 'b-.');
title (['Savitzky-Golay Filter (nl = ' int2str(nl) ...
        ', nr = ' int2str(nr) ', M = ' int2str(M) ')']);
xlabel ([num2str(t1-t0) ' seconds of CPU-time']);
pause (3);

% smooth by least squares filter
t0 = cputime;
g = lsq (f, delta);
t1 = cputime;

% plot results
plot (x, g, 'r', x, F, 'b-.');
title (['Least-Squares Filter (delta = ' num2str(delta) ')']);
xlabel ([num2str(t1-t0) ' seconds of CPU-time']);
pause (3);



