

% Compute a depth rate across a long timebase so as to minimize
% susceptability to noise in the depth sensor or jitter in the time stamp.

nn = length(depth.nav);
delta = 50;
drate = depth.nav(delta:nn)-depth.nav(1:(nn-delta+1));

% Find portion of run that are going up, and portion going down
threshold = median(abs(depth.nav(delta:nn)-depth.nav(1:(nn-delta+1))))/3;
i_up = find(drate>threshold);
i_down = find(drate<-threshold);

% Create a vector that contains the direction of motion
step = depth.nav * 0;
step(i_up+delta/2)= 1;
step(i_down+delta/2)= -1;

% Create and array marking the start and end of yoyos
last = 0; jj = 1;
ii = find(step ~= 0);
ttt = time.nav(ii);
step = step(ii);

for i = 1:length(step)
    if last ~= step(i)
        yoyo(jj, 1:4) = [jj step(i) ttt(i) 0];
        if jj > 1
            yoyo(jj-1, 4) = ttt(i-1);
        end;
        jj = jj+1;
        last = step(i);
    end;
end;
yoyo(jj-1, 4) = ttt(length(ttt));

plot(time.nav(i_up+delta/2), depth.nav(i_up+delta/2), '.');
hold on
plot(time.nav(i_down+delta/2), depth.nav(i_down+delta/2), 'r.');
plot(time.nav, depth.nav, 'g');

clear delta drate i_up i_down step threshold ttt last ii jj