% Script illustrating the use of the function TVDENOISE.
% 
% Jakub Wagner, June 30, 2021
% Institute of Radioelectronics and Multimedia Technology
% Warsaw University of Technology


% Load exemplary data representative of human gait:
%   data.x - horizontal coordinates of the position of a foot along
%            the walking direction
%   data.t - time moments at which the foot position was estimated
data = load('gait_data');


% Denoise the data by constraining the 1-norm of the 2nd derivative
rp1 = 1e-4;
x1 = tvdenoise(data.x, data.t, 'ConstraintOrder', 2, 'RegPar', rp1);

% Optimise the regularisation parameter automatically
regpars = logspace(-5, -3, 10);
[x2, ~, rp2] = tvdenoise(data.x, data.t, 'ConstraintOrder', 2, 'RegPar', regpars);

% Estimate the first derivative
rp3 = 6e-4;
[v, x3] = tvdenoise(data.x, data.t, 'ConstraintOrder', 2, ...
    'RegPar', rp3, 'DerivativeOrder', 1);


% Plot results
figure

subplot(2,2,1)
plot(data.t, data.x)
title('Data')

subplot(2,2,2)
plot(data.t, x1)
title(sprintf('Denoised data\nRegularisation parameter: %.2d', rp1))

subplot(2,2,3)
plot(data.t, x2)
title(sprintf('Denoised data\nRegularisation parameter: %.2d\n(optimised automatically)', rp2))

subplot(2,2,4)
plot(data.t, x3, data.t, v)
title(sprintf('Denoised data\nand estimates of first derivative\nRegularisation parameter = %.2d', rp3))