function [ res, Ryy ] = detect2( y )
%DETECT1 Given the output signal y of the double echo system returns the
%delays N1 and N2 in time of the echos (one with the greatest magnitude
%first) and the auto-correlation function for y.
%@requires y is a row vector
Ryy = conv(y, flipud(y));
L = length(Ryy);
dRs = filter([-1 2 -1], 1, Ryy);
dR = dRs([2:L]);
[cm, ci] = max(dR);
% the greatest peak should be at the center
if ci ~= (L+1)/2
    ci
    error('DETECT2: the greatest peak is not at the center');
end
reg = dR([1:ci-2]);
% we will find the r greatest peaks, once we discard values close to the
% center peak
r = 10;
res = zeros(1, r);
for i=1:r,
    [nm, ni] = max(reg);
    res(i) = ci-ni;
    % now let's set that peak to null and keep looking
    reg(ni) = 0;
end
% look for N1, N2, N1-N2, and N2-N1
found = 0;
for i=1:r,
    for j=2:r,
        N1 = res(i);
        N2 = res(j);
        if index(res, abs(N1-N2))~=0 && index(res, abs(N2-N1))~=0
            found = 1;
            break;
        end
    end
end
if found==0
    res
    error('DETECT2: no consistent N1 and N2 found');
end