function [ N, Ryy ] = detect1( y )
%DETECT1 Given the output signal y of the echo system returns the delay N
%in time of the echo and the auto-correlation function for y.
%@requires y is a column-vector
Ryy = conv(y, flipud(y));
L = length(Ryy);
% y[n] = x[n]-x[n-1] + x[n]-x[n+1] = -x[n+1] + 2x[n] - x[n-1]
% y[n-1] = -x[n] + 2x[n-1] - x[n-2]
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('DETECT1: the greatest peak is not at the center');
end
% find the next greatest peak, once we discard values close to the center peak
[nm, ni] = max(dR([1:ci-200]));
N = ci-ni;
