a = load("dactest.log");

for i = 0:0x3ff; 
  amin(i+1) = min(a(find(a(:,2) == i),3));
  amax(i+1) = max(a(find(a(:,2) == i),3));
  amean(i+1) = mean(a(find(a(:,2) == i),3));
  astd(i+1) = std(a(find(a(:,2) == i),3));
end

# First line is the maximum deviation that we saw
# (ie. 14 bits means "1 part in 2^14, over the full 10V scale")
# Second line is 3 standard deviations, so 99.9% for a normal distribution.

t = 1:1024;

figure(1);
plot(t, log2(10./(amax(t)-amin(t))), ';Max;', t, log2(10./(3*astd(t))), ';99.9%;');
xlabel("DAC command")
ylabel("Deviation from mean (bits)");

figure(2);
p = polyfit(t, amean(t), 1)
plot(t, log2(10 ./ ((t * p(1) + p(2)) - amean(t))));
xlabel("DAC command")
ylabel("Average error from linear (bits)");

if input('Print? [y/N] ', 's') == 'y'
     disp('Printing');
     figure(1);
     print('-landscape');
     figure(2);
     print('-landscape');
end
