ZeroCrossings - Test zero crossings in a given time series. This assumes a minimum of 10 samples per positive/negative phase. USAGE [up,down] = ZeroCrossings(samples,<options>) samples the <a href="matlab:help samples">samples</a> to process <options> optional list of property-value pairs (see table below) ========================================================================= Properties Values ------------------------------------------------------------------------- 'smooth' standard deviation for Gaussian kernel (default = 0) ========================================================================= OUTPUT up logical indices indicating upward zero crossings down logical indices indicating downward zero crossings NOTE This finds the points in the signal closest to the zero crossings (the actual zero crossings may not appear in the time series). Hence, if the signal changes very abruptly, e.g. if X(t)=A and X(t+1)=-A (where A is some large constant), this function will consider index t as a downward zero crossing, although the value A is very different from 0.
0001 function [up,down] = ZeroCrossings(data,varargin) 0002 0003 %ZeroCrossings - Test zero crossings in a given time series. 0004 % 0005 % This assumes a minimum of 10 samples per positive/negative phase. 0006 % 0007 % USAGE 0008 % 0009 % [up,down] = ZeroCrossings(samples,<options>) 0010 % 0011 % samples the <a href="matlab:help samples">samples</a> to process 0012 % <options> optional list of property-value pairs (see table below) 0013 % 0014 % ========================================================================= 0015 % Properties Values 0016 % ------------------------------------------------------------------------- 0017 % 'smooth' standard deviation for Gaussian kernel (default = 0) 0018 % ========================================================================= 0019 % 0020 % OUTPUT 0021 % 0022 % up logical indices indicating upward zero crossings 0023 % down logical indices indicating downward zero crossings 0024 % 0025 % NOTE 0026 % 0027 % This finds the points in the signal closest to the zero crossings 0028 % (the actual zero crossings may not appear in the time series). 0029 % Hence, if the signal changes very abruptly, e.g. if X(t)=A 0030 % and X(t+1)=-A (where A is some large constant), this function 0031 % will consider index t as a downward zero crossing, although 0032 % the value A is very different from 0. 0033 0034 % Copyright (C) 2004-2011 by Michaƫl Zugaro 0035 % 0036 % This program is free software; you can redistribute it and/or modify 0037 % it under the terms of the GNU General Public License as published by 0038 % the Free Software Foundation; either version 3 of the License, or 0039 % (at your option) any later version. 0040 0041 if nargin < 1, 0042 error('Incorrect number of parameters (type ''help <a href="matlab:help ZeroCrossings">ZeroCrossings</a>'' for details).'); 0043 end 0044 0045 if size(data,2) ~= 2, 0046 error('Parameter ''data'' is not a Nx2 matrix (type ''help <a href="matlab:help ZeroCrossings">ZeroCrossings</a>'' for details).'); 0047 end 0048 0049 % Defaults 0050 smooth = 0; 0051 0052 % Parse parameter list 0053 for j = 1:2:length(varargin), 0054 if ~ischar(varargin{j}), 0055 error(['Parameter ' num2str(j+7) ' is not a property (type ''help <a href="matlab:help ZeroCrossings">ZeroCrossings</a>'' for details).']); 0056 end 0057 switch(lower(varargin{j})), 0058 case 'smooth', 0059 smooth = varargin{j+1}; 0060 if ~isdvector(smooth,'>=0') || length(smooth) > 2, 0061 error('Incorrect value for property ''smooth'' (type ''help <a href="matlab:help ZeroCrossings">ZeroCrossings</a>'' for details).'); 0062 end 0063 otherwise, 0064 error(['Unknown property ''' num2str(varargin{j}) ''' (type ''help <a href="matlab:help ZeroCrossings">ZeroCrossings</a>'' for details).']); 0065 end 0066 end 0067 0068 data(:,2) = Smooth(data(:,2),smooth); 0069 0070 % Find downward and upward going zero-crossings 0071 previous = data(1:end-1,2); 0072 current = data(2:end,2); 0073 down = previous > 0 & current < 0;down(end+1) = 0; 0074 up = previous < 0 & current > 0;up(end+1) = 0; 0075