V_SCHMITT Pass input signal X through a v_schmitt trigger SCHMITT(X,[LOW HIGH]) gives low and high thresholds. LOW and HIGH can be scalars or can be vectors specifiying different thresholds for each X element. SCHMITT(X,HYSTERESIS) specifies the thresholds as MAX-DELTA and MIN+DELTA where DELTA=(MAX-MIN)*(1-HYSTERESIS)/2 and MAX and MIN are the max and min of X. HYSTERESIS must be in the range 0 to 1 and represents the fraction of MAX-MIN between the two threshold values; default is 0.5. MINWID specifies the minimum width of a pulse (in samples). Pulses thinner than this will be ignored. Output Y takes values -1, +1 according to whether X<LOW or X>HIGH most recently. Y may be 0 for an initial segment if neither condition is initially true. For [Y,T]=SCHMITT(...) Y contains alternate +1 and -1 values and T contains the sample numbers at which X crossed the thresholds.
0001 function [y,t]=v_schmitt(x,thresh,minwid) 0002 %V_SCHMITT Pass input signal X through a v_schmitt trigger 0003 % SCHMITT(X,[LOW HIGH]) gives low and high thresholds. LOW and HIGH can be 0004 % scalars or can be vectors specifiying different thresholds for each X element. 0005 % 0006 % SCHMITT(X,HYSTERESIS) specifies the thresholds as MAX-DELTA and MIN+DELTA where 0007 % DELTA=(MAX-MIN)*(1-HYSTERESIS)/2 and MAX and MIN are the max and min of X. 0008 % HYSTERESIS must be in the range 0 to 1 and represents the fraction of MAX-MIN between 0009 % the two threshold values; default is 0.5. 0010 % 0011 % MINWID specifies the minimum width of a pulse (in samples). Pulses thinner than this will 0012 % be ignored. 0013 % 0014 % Output Y takes values -1, +1 according to whether X<LOW or X>HIGH most recently. 0015 % Y may be 0 for an initial segment if neither condition is initially true. 0016 % For [Y,T]=SCHMITT(...) Y contains alternate +1 and -1 values and T contains 0017 % the sample numbers at which X crossed the thresholds. 0018 0019 0020 % Copyright (C) Mike Brookes 1998 0021 % Version: $Id: v_schmitt.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 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<3 0043 minwid=0; 0044 if nargin<2 0045 thresh=0.5; 0046 end 0047 end 0048 if length(thresh)<2 0049 xmax=max(x); 0050 xmin=min(x); 0051 low=(xmax-xmin)*(1-thresh)/2; 0052 high=xmax-low; 0053 low=xmin+low; 0054 else 0055 low=thresh(1); 0056 high=thresh(2); 0057 end 0058 c=(x>high)-(x<low); 0059 c(2:end)=c(2:end).*(c(2:end)~=c(1:end-1)); 0060 t=find(c); 0061 t(1+find(c(t(2:end))==c(t(1:end-1))))=[]; % remove duplicates 0062 if minwid>=1 0063 t(t(2:end)-t(1:end-1)<minwid)=[]; 0064 t(1+find(c(t(2:end))==c(t(1:end-1))))=[]; % remove duplicates 0065 end 0066 if nargout>1 y=c(t); 0067 else 0068 y=zeros(size(c)); 0069 if ~isempty(t) 0070 y(t)=2*c(t); 0071 y(t(1))=c(t(1)); 0072 y=cumsum(y); 0073 end 0074 end 0075 if ~nargout 0076 xmax=max(x); 0077 xmin=min(x); 0078 if high-low<0.1*(xmax-xmin) 0079 high=xmax; 0080 low=xmin 0081 end 0082 plot([x(:) low+(y(:)+1)*(high-low)/2]); 0083 end