# v_zerocros

## PURPOSE

V_ZEROCROS finds the zeros crossings in a signal [T,S]=(Y,M,X)

## SYNOPSIS

function [t,s]=v_zerocros(y,m,x)

## DESCRIPTION

    V_ZEROCROS finds the zeros crossings in a signal [T,S]=(Y,M,X)
     Inputs:  y = input waveform
              m = mode string containing:
                  'p' - positive crossings only
                  'n' - negative crossings only
                  'b' - both (default)
                  'r' - round to sample values
              x = x-axis values corresponding to y [default 1:length(y)]

     Outputs: t = x-axis positions of zero crossings
              s = estimated slope of y at the zero crossing

     This routine uses linear interpolation to estimate the position of a zero crossing
     A zero crossing occurs between y(n) and y(n+1) iff (y(n)>=0) ~= (y(n+1)>=0)

## CROSS-REFERENCE INFORMATION

This function calls:

- v_axisenlarge V_AXISENLARGE - enlarge the axes of a figure (f,h)

This function is called by:

- v_dypsa V_DYPSA Derive glottal closure instances from speech [gci,goi] = (s,fs)

## SOURCE CODE

    0001 function [t,s]=v_zerocros(y,m,x)
    0002 %V_ZEROCROS finds the zeros crossings in a signal [T,S]=(Y,M,X)
    0003 % Inputs:  y = input waveform
    0004 %          m = mode string containing:
    0005 %              'p' - positive crossings only
    0006 %              'n' - negative crossings only
    0007 %              'b' - both (default)
    0008 %              'r' - round to sample values
    0009 %          x = x-axis values corresponding to y [default 1:length(y)]
    0010 %
    0011 % Outputs: t = x-axis positions of zero crossings
    0012 %          s = estimated slope of y at the zero crossing
    0013 %
    0014 % This routine uses linear interpolation to estimate the position of a zero crossing
    0015 % A zero crossing occurs between y(n) and y(n+1) iff (y(n)>=0) ~= (y(n+1)>=0)
    0016
    0017 % Example: y=sin(2*pi*(0:1000)/200); y(1:100:1001)=0; v_zerocros(y);
    0018 % Note that we get a zero crossing at the end but not at the start.
    0019
    0020 %       Copyright (C) Mike Brookes 2003-2015
    0021 %      Version: $Id: v_zerocros.m 10865 2018-09-21 17:22:45Z dmb $
    0022 %
    0023 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0024 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0025 %
    0026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0027 %   This program is free software; you can redistribute it and/or modify
    0028 %   it under the terms of the GNU Lesser General Public License as published by
    0029 %   the Free Software Foundation; either version 3 of the License, or
    0030 %   (at your option) any later version.
    0031 %
    0032 %   This program is distributed in the hope that it will be useful,
    0033 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0034 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0035 %   GNU Lesser General Public License for more details.
    0036 %
    0037 %   You can obtain a copy of the GNU Lesser General Public License from
    0038 %   https://www.gnu.org/licenses/ .
    0039 %
    0040 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0041
    0042 if nargin<2 || ~numel(m)
    0043     m='b';
    0044 end
    0045 s=y>=0;
    0046 k=s(2:end)-s(1:end-1);
    0047 if any(m=='p')
    0048     f=find(k>0);
    0049 elseif any(m=='n')
    0050     f=find(k<0);
    0051 else
    0052     f=find(k~=0);
    0053 end
    0054 s=y(f+1)-y(f);
    0055 t=f-y(f)./s;
    0056 if any(m=='r')
    0057     t=round(t);
    0058 end
    0059 if nargin>2
    0060     tf=t-f; % fractional sample
    0061     t=x(f).*(1-tf)+x(f+1).*tf;
    0062     s=s./(x(f+1)-x(f));
    0063 end
    0064 if ~nargout
    0065     n=length(y);
    0066     if nargin>2
    0067         plot(x,y,'-b',t,zeros(length(t),1),'or');
    0068     else
    0069         plot(1:n,y,'-b',t,zeros(length(t),1),'or');
    0070     end
    0071     v_axisenlarge([-1 -1.05]);
    0072 end