function I = zerocross(x, mode)
    // Finds zero-crossings of a signal
    // Usage:
    //       I = zerocross(x, mode)
    //
    // where x:    vector containing the signal
    //       I:    vector of indexes of samples where x becomes 0
    //       mode: mode of detection (see details below)
    //
    // As x may never become 0, this function actually detects
    // samples where signal changes sign. How this is done depends
    // on the mode parameter:
    // 
    // mode   behaviour
    // --------------------------------------------------------
    // 1      x(i)   < 0  and  x(i+1) >= 0    (positive slope)
    // 2      x(i-1) < 0  and  x(i)   >= 0    (positive slope) 
    // 3      x(i)   > 0  and  x(i+1) <= 0    (negative slope)
    // 4      x(i-1) > 0  and  x(i)   <= 0    (negative slope)
    // 13     same as 1 or 3
    // 14     same as 1 or 4
    // 23     same as 2 or 3
    // 24     same as 2 or 4
    // 
    // 1 and 3 detect the sample before zero crossing, while
    // 2 and 4 detect the sample after or while zero crossing. 
    // 1 and 2 detect positive-slope crossings while 3 and 4
    // detect negative-slope crossings.
    //
    // If mode is not given, then the default is 13
    //
    // NOTE: Use 2, 4 and 24 to detect samples that are in effect
    //       exactly 0.
    //
    // --------------------------
    // Author: Federico Miyara
    // Date:   2010-04-09

    %nargin = argn(2)
    
    if %nargin<2
        mode = 13;
    end

    switch mode
    case 1
        I = find((x(1:$-1)<0) & (x(2:$)>=0));
    case 2
        I = find((x(1:$-1)<0) & (x(2:$)>=0)) + 1;
    case 3
        I = find((x(1:$-1)>0) & (x(2:$)<=0));
    case 4
        I = find((x(1:$-1)>0) & (x(2:$)<=0))+1;
    case 13
        I = find(((x(1:$-1)<0) & (x(2:$)>=0)) + ...
                 ((x(1:$-1)>0) & (x(2:$)<=0)));
    case 14
        I = [find((x(1:$-1)<0) & (x(2:$)>=0)), ...
             find((x(1:$-1)>0) & (x(2:$)<=0)) + 1];
        I = gsort(I,'g','i');
    case 23
        I = [find((x(1:$-1)<0) & (x(2:$)>=0)) + 1,...
             find((x(1:$-1)>0) & (x(2:$)<=0))]; 
        I = gsort(I,'g','i');
    case 24
        I = find(((x(1:$-1)<0) & (x(2:$)>=0)) | ...
                 ((x(1:$-1)>0) & (x(2:$)<=0))) + 1;       
    end

endfunction

