echo on
load vessel.mat

subplot(211)
tid=1:length(z);tid=tid*4/60;
plot(tid,z(:,2)),title(' \kappa-number of Inflow','fontsize',14)
subplot(212)
plot(tid,z(:,1)),title(' \kappa-number of Outflow','fontsize',14)
% Original Data. Press return to continue.
pause

subplot(211)
plot(tid,flode),title('Inflow ','fontsize',14)
subplot(212)
plot(tid,niva),title('Vessel Level','fontsize',14)
% Original Data. Press return to continue.
pause
subplot
% Let's build an ARX model using all the data in a 
% "blind" unthoughtful manner
rawdata=[z flode niva];N=length(rawdata);rawdata=dtrend(rawdata);
compare(rawdata,arx(rawdata(1:floor(N/2),:),[4 4 4 4 1 1 1]));
% Not a very good fit!
% Press return to continue
pause
% Let's now resample the data
 pf=flode./niva;
plot(pf)
% The "natural time". Press return to continue
pause

ll=table1([cumsum(pf+0.0001),[1:1501]'],[pf(1)+0.1:sum(pf)]');
  zz=table1([[1:1501]' z],ll);
 
subplot(211)
tid=1:length(zz);tid=tid*4/60;
plot(tid,zz(:,2)),title(' \kappa-number of Inflow','fontsize',14)
subplot(212)
plot(tid,zz(:,1)),title(' \kappa-number of Outflow','fontsize',14)
% The resampled kappa-numbers. Press return to continue
pause 
subplot
% Let's detrend and try an ARX model:
 zz=dtrend(zz);
 compare(zz,arx(zz,[4 4 1]));
 % Press return to continue
 pause
 % Now decimate a factor 3:
 zdt=idresamp(zz,3);
 
idplot(zd)
% The decimated data. Press return to continue
pause
  
% Let's estimate a good value of the delay:
V=arxstruc(zd,zd,struc(4,4,1:20));
nn=selstruc(V,0);
% The estimated delay
nn(3)
 
yh=compare(zdt,arx(zdt(1:200,:),[4 4 nn(3)]));
% Looks quite good!