//datacrunchpitot.sce
//www.basicairdata.eu JLJ 2013
//Sample data handling of pitot sensor data
//Data file hcla12x10Hz must be placed in the current directory
clear all;
xdel(winsid());
y=read("hcla12x10Hz",-1,1);
//attention
detrend(y,'l');

meany=mean(y);
sdevy=stdev(y);
[samplesize zz]=size(y)
samplet=linspace(1,16870,16870)
hand1=figure(1,"BackgroundColor",[1,1,1]);
plot(samplet,y);
title("Sensor Output plotting","fontsize",4)
xlabel("Sample number","fontsize",4)
ylabel("Decimal count, sensor output","fontsize",4)
hand2=figure(2,"BackgroundColor",[1,1,1]);
histplot(5,y,normalization=%f)
title("Histogram of samples","fontsize",4)
xlabel("Sample value, divided in bins","fontsize",4)
ylabel("Total occurences","fontsize",4)

samplefreq=nfreq(y)
disp(samplefreq)
samplefreq(:,2)=samplefreq(:,2)/samplesize;
disp(samplefreq)
[rsamplefreq csamplefreq]=size(samplefreq)
//Calculate mean
//Redundant procedure, just compare to meany value
//Microcontroller friendly implementation
sadd=0;
for i=1:rsamplefreq
    sadd=sadd+samplefreq(i,1)*samplefreq(i,2);
end
disp(sadd)
//Calculation of standard deviation of y
sadd=0;
for i=1:rsamplefreq
    sadd=sadd+(samplefreq(i,1)-meany)^2*samplefreq(i,2);
end
disp(sadd^0.5)
//Remove offset z=y-offset
z=y-meany;
hand=figure(3,"BackgroundColor",[1,1,1]);
plot(samplet,z);
title("Sensor Output plotting, offset removed","fontsize",4)
xlabel("Sample number","fontsize",4)
ylabel("Decimal count, sensor output","fontsize",4)
meanz=mean(z);
sdevz=stdev(z);
disp(meanz);
disp(sdevz);
hand2=figure(4,"BackgroundColor",[1,1,1]);
histplot(5,z,normalization=%f)
title("Histogram of samples without offset","fontsize",4)
xlabel("Sample value, divided in bins","fontsize",4)
ylabel("Total occurences","fontsize",4)
samplefreqz=nfreq(z)
disp(samplefreqz)
min(z)
max(z)
[a,b,sig]=reglin(samplet,z')
