close all clear all % curve fit for sharp gp2d12 % M Holden 4/05 % from data sheet v = [2.4 1 .63 .5]; % volts l = [10 30 50 80]/2.54; % inches figure(1);plot(v,1./l);grid on; invl = 1./l; [P, S]= polyfit(v,invl, 1) vfit = linspace(min(v),max(v),30); invlfit = vfit*P(1) + P(2); lfit = 1./invlfit; figure(1);plot(l,v,'r+',lfit,vfit,'b-');grid on; % convert to adc units: slope = P(1)*5/1024; offset = P(2); fprintf('const float sensor_slope = %f; // (1/inch) per adc count\n', slope); fprintf('const float sensor_offset = %f; // (1/inch)\n', offset); figure(2);plot(l,v*1024/5,1./(vfit*1024/5*slope + offset), vfit*1024/5);