Home > voicebox > zerocros.m

zerocros

PURPOSE ^

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

SYNOPSIS ^

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

DESCRIPTION ^

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: This function is called by:

SOURCE CODE ^

0001 function [t,s]=zerocros(y,m,x)
0002 %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; 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: zerocros.m 6563 2015-08-16 16:56:24Z 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 General Public License as published by
0029 %   the Free Software Foundation; either version 2 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 General Public License for more details.
0036 %
0037 %   You can obtain a copy of the GNU General Public License from
0038 %   http://www.gnu.org/copyleft/gpl.html or by writing to
0039 %   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
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     plot(1:n,y,'-',t,zeros(length(t),1),'o');
0067 end

Generated on Tue 19-Sep-2017 12:07:31 by m2html © 2003