%% Clear Workspace and Close figures
clear; close all; clc;

%% Intialize Laplace variable
s = zpk('s');

addpath('./mat/');

% Results                                                         :ignore:
% Let's load the measurement results.

load('meas_stiff_probe.mat', 't', 'd', 'dp', 'F')

%% Sampling time [s]
Ts = (t(end) - t(1))/(length(t)-1);

%% Remove first second
t = t(ceil(1/Ts):end);
d = d(ceil(1/Ts):end);
dp = dp(ceil(1/Ts):end);
F = F(ceil(1/Ts):end);


%% Remove Offset
t = t - t(1);
F = F - mean(F(1:ceil(1/Ts)));



% The time domain measured force and displacement are shown in Figure [[fig:mahr_time_domain]].


%% Time Domain plots
figure;
tiledlayout(2, 1, 'TileSpacing', 'None', 'Padding', 'None');

ax1 = nexttile;
plot(t, F);
ylabel('Force [N]'); set(gca, 'XTickLabel',[]);

ax2 = nexttile;
plot(t, d);
xlabel('Time [s]'); ylabel('Displacement [m]');



% #+RESULTS:
% : Stiffness is 0.039 [N/mm]

% This is very close to the 0.04 [N/mm] written in the [[file:doc/tmp3m0cvmue_7888038c-cdc8-48d8-a837-35de02760685.pdf][Millimar 1318 probe datasheet]].

% And compare the linear fit with the raw measurement data (Figure [[fig:mahr_stiffness_f_d_plot]]).


figure;
hold on;
plot(F, d, 'DisplayName', 'Raw data');
plot(F, F/(d\F), 'DisplayName', 'Linear fit');
hold off;
xlabel('Measured Force [N]');
ylabel('Measured Displacement [m]');
legend('location', 'southeast');
