deltaT=str2num(datestr(diff(AllData(:,1)),'ss.FFF'));
Z=cumtrapz(AllData(:,1),AllData(:,2));
figure('Name', 'Accelerations');
plot(AllData(:,1),Z,'r');
legend('Velx');
xlabel('Time (mm:ss)');
ylabel('Acceleration (g)');
title([fileName ' Accelerations (g)  ' datestr(AllData(1,1))]);
AccX=[];
AccY=[];
AccZ=[];

AccX=AccData(:,2);
AccX(isnan(AccX))=[];
AccY=AccData(:,3);
AccY(isnan(AccY))=[];
AccZ=AccData(:,4);
AccZ(isnan(AccZ))=[];
Time=AccData(:,1);
Time(isnan(AccData(:,2)))=[];

%grid data
% X=BedsElapsedTime.*3600;    %time in elapsed sec
% V=DepthData(:,2);
% Xq=linspace(0,X(end),size(X,1));
% 
% Vq=interp1(X,V,Xq);
% figure;plot(X,V,'o',Xq,Vq)

EpochSecs=(Time-719529)*86400;
BedsElapsedTime=zeros(size(EpochSecs)); %in seconds
for i=1:size(EpochSecs,1),
        BedsElapsedTime(i,1)=(EpochSecs(i,1)-EpochSecs(1,1));
end

figure;plot(BedsElapsedTime,AccZ)
Time_grid=linspace(0,BedsElapsedTime(end),size(BedsElapsedTime,1))';


%try filtering

AccZfilt=medfilt1(AccZ,20);
AccZfilt=AccZfilt-mean(AccZfilt);
velocityZ=(cumsum(AccZfilt(2:end)*g).*diff(Time));

figure;subplot(2,1,1); plot(Time(2:end),velocityZ);
xlabel('Time');ylabel('Velocity (m/sec)');
datetick('x','MM:SS','keepticks','keeplimits');
title('Z-axis motion');
subplot(2,1,2); plot(Time,AccZ);hold on; plot(Time,AccZfilt,'r');
datetick('x','MM:SS','keepticks','keeplimits')
xlabel('Time');ylabel('Acceleration(g)');



g = 9.806; % m/s^2 at 38 degrees latitude.
Ts = .02;   %Sample Period; 50 Hz sample rate.

AccTest=ones(size(AccX));
Vtest=cumsum(AccTest*g)*Ts;

velocityX=(cumsum(AccX*g))*Ts;
velocityY=cumsum((AccY*g))*Ts;
velocityZ=(cumsum(AccZ*g)*Ts);


velocityX=(cumtrapz(AccX*g))*Ts;
velocityY=cumtrapz((AccY*g))*Ts;
velocityZ=(cumtrapz(AccZ*g)*Ts);

figure;subplot(2,1,1); plot(Time,velocityX);
xlabel('Time');ylabel('Velocity (m/sec)');
datetick('x','MM:SS','keepticks','keeplimits');
title('X-axis motion');
subplot(2,1,2); plot(Time,AccX);
datetick('x','MM:SS','keepticks','keeplimits')
xlabel('Time');ylabel('Acceleration(g)');


figure;subplot(2,1,1); plot(Time,velocityY);
xlabel('Time');ylabel('Velocity (m/sec)');
title('Y-axis motion');
datetick('x','MM:SS','keepticks','keeplimits')
subplot(2,1,2); plot(Time,AccY);
datetick('x','MM:SS','keepticks','keeplimits')
xlabel('Time');ylabel('Acceleration(g)');

figure;subplot(2,1,1); plot(Time,velocityZ);
xlabel('Time');ylabel('Velocity (m/sec)');
title('Z-axis motion');
datetick('x','MM:SS','keepticks','keeplimits')
subplot(2,1,2); plot(Time,AccZ);
datetick('x','MM:SS','keepticks','keeplimits');
xlabel('Time');ylabel('Acceleration(g)');



AccTest=ones(10,1)';

Vtest=cumsum(AccTest)*g*Ts;

Vtest2=cumtrapz(linspace(0,size(AccZ,1)*Ts,size(AccZ,1)),AccZ*g);
figure;plot(FakeT,Vtest2);
acc2=diff(Vtest2)./Ts;
figure; plot(FakeT(1:end-1),acc2);
figure;subplot(2,1,1); plot(linspace(0,10*Ts,10),Vtest);
xlabel('Time');ylabel('Velocity (m/sec)');
title('Test-axis motion');
%datetick('x','MM:SS','keepticks','keeplimits');
subplot(2,1,2); plot(linspace(0,10*Ts,10),AccTest);
%datetick('x','MM:SS','keepticks','keeplimits');
xlabel('Time');ylabel('Acceleration(g)');

FakeT=linspace(0,size(velocityZ,1)*Ts,size(velocityZ,1));
figure;subplot(2,1,1); plot(FakeT,velocityZ);
xlabel('Time(secs)');ylabel('Velocity (m/sec)');
title('Z-axis motion');
%datetick('x','MM:SS','keepticks','keeplimits')
subplot(2,1,2); plot(FakeT,AccZ);
%datetick('x','MM:SS','keepticks','keeplimits');
xlabel('Time (secs)');ylabel('Acceleration(g)');

Z=cumtrapz(AccZ*g);
Z=Z.*Ts;
figure;subplot(2,1,1); plot(FakeT,Z);


AccSoS=sqrt(AccX.^2+AccY.^2+AccZ.^2);
figure; plot(Time,AccSoS);
datetick('x','HH:MM','keepticks','keeplimits')
xlabel('Time');ylabel('Accel Mag(g)');

AccSoS_filt= medfilt1(AccSoS,10);
hold on; plot(Time, AccSoS_filt,'r');
load DepthData;

figure; [ax,h1,h2]=plotyy(Time,AccSoS_filt,DepthData(:,1),DepthData(:,2));
datetick(ax(1),'x','HH:MM','keepticks','keeplimits');
datetick(ax(2),'x','HH:MM','keepticks','keeplimits');
xlabel('Time');ylabel('Accel Mag Filtered(g)');
ylabel(ax(2),'Depth (dBar)');

figure; [ax,h1,h2]=plotyy(Time,AccSoS,DepthData(:,1),DepthData(:,2));
datetick(ax(1),'x','HH:MM','keepticks','keeplimits');
datetick(ax(2),'x','HH:MM','keepticks','keeplimits');
xlabel('Time');ylabel('Accel Mag (g)');
ylabel(ax(2),'Depth (dBar)');
