% Test Completed 2020-08-24 3PM PST
% Eric Martin
% Notes:
% Samples taken ~1hz manually.
% Started at -100000 JV command on the pump
% Sped up to -200000 JV command for most of the drawdown
% Slowed to -100000 JV commands for the final section.
% Sensor behaves linearly

clear; close all;
load InnerBellowFullToEmptyProfile_20200824_Parsed.mat

%

% Plot Raw Data
plot(time, intval,'.');
xlabel('Elapsed Time')
ylabel('ADC Value')
title('Raw Values')

%

% Subselect Data
full = intval(1:59);
full_t = time(1:59);

emptee = intval(1917:end);
emptee_t = time(1917:end);
plot(time,intval,'.', full_t,full, 's', emptee_t, emptee,'o');
xlabel('Elapsed Time')
ylabel('ADC Value')
title('Raw Values, Full & Empty Selected')
legend('raw data','full','empty')

full_mean = mean(full)
full_std = std(full)

emptee_mean = mean(emptee)
emptee_std = std(emptee)

%

% Find the cal
x = [emptee_mean full_mean];
y = [0 100];
a = polyfit(x,y,1);
fprintf('Calibration is %0.10g x ADC + %0.6f',a(1),a(2));

%

% Plot pre and post cal
postcal = polyval(a,intval);
plot(time,precal, '.', time, postcal,'.')
xlabel('Elapsed Time')
ylabel('Inner Bellows % Full')
title('Pre and Post Calibration Comparison')
legend('precal', 'postcal')