# Make a histogram of the error (in milliamps)

a=load("log-locked");
len=size(a,1)
keith=a(:,3);
calib=a(1,4)
dac=a(:,5);
adc=a(:,6);
lookup=load("../../../firmware/lookup.inc");
meas=-(lookup(dac + 1)/64 - calib * adc);
keith_amps = keith * 50;         # 50 turns
p = polyfit(meas, keith_amps, 1)
meas_amps = meas * p(1) + p(2);  # calibration fit
err_amps = keith_amps - meas_amps;

[n, x] = hist(err_amps*1000, [-15:0.5:15]);

for i = 0:30
  cumulative(i+1)=sum(n((31-i):(31+i))/len);
endfor
figure(1);
plot(x(31:61),cumulative);


savedata = [ x' n' ];
save("-ascii", "hist.dat", "savedata");

figure(2);
bar(x,n)
