Daniel@0: function varargout = mirzerocross(orig,varargin) Daniel@0: % mirzeroscross(x) computes the sign-changes rate along the signal x, Daniel@0: % i.e., how many time the waveform crosses the X-axis. When applied on Daniel@0: % an audio waveform, gives a notion of noise. Daniel@0: % Optional argument: Daniel@0: % mirzerocross(...,'Per',p) precises the temporal reference for the Daniel@0: % rate computation. Daniel@0: % Possible values: Daniel@0: % p = 'Second': number of sign-changes per second (Default). Daniel@0: % p = 'Sample': number of sign-changes divided by the total Daniel@0: % number of samples. Daniel@0: % The 'Second' option returns a result equal to the one returned Daniel@0: % by the 'Sample' option multiplied by the sampling rate. Daniel@0: % mirzerocross(...,'Dir',d) precises the definition of sign change. Daniel@0: % Possible values: Daniel@0: % d = 'One': number of sign-changes from negative to positive Daniel@0: % only (or, equivalently, from positive to negative only). Daniel@0: % (Default) Daniel@0: % d = 'Both': number of sign-changes in both ways. Daniel@0: % The 'Both' option returns a result equal to twice the one Daniel@0: % returned by the 'One' option. Daniel@0: Daniel@0: Daniel@0: per.key = 'Per'; Daniel@0: per.type = 'String'; Daniel@0: per.choice = {'Second','Sample'}; Daniel@0: per.default = 'Second'; Daniel@0: option.per = per; Daniel@0: Daniel@0: dir.key = 'Dir'; Daniel@0: dir.type = 'String'; Daniel@0: dir.choice = {'One','Both'}; Daniel@0: dir.default = 'One'; Daniel@0: option.dir = dir; Daniel@0: Daniel@0: specif.option = option; Daniel@0: Daniel@0: varargout = mirfunction(@mirzerocross,orig,varargin,nargout,specif,@init,@main); Daniel@0: Daniel@0: Daniel@0: function [x type] = init(x,option) Daniel@0: if not(isamir(x,'mirdata')) Daniel@0: x = miraudio(x); Daniel@0: end Daniel@0: type = 'mirscalar'; Daniel@0: Daniel@0: Daniel@0: function z = main(a,option,postoption) Daniel@0: if iscell(a) Daniel@0: a = a{1}; Daniel@0: end Daniel@0: d = get(a,'Data'); Daniel@0: f = get(a,'Sampling'); Daniel@0: v = cell(1,length(d)); Daniel@0: for h = 1:length(d) Daniel@0: v{h} = cell(1,length(d{h})); Daniel@0: for i = 1:length(d{h}) Daniel@0: di = d{h}{i}; Daniel@0: nc = size(di,2); Daniel@0: nf = size(di,3); Daniel@0: nl = size(di,1); Daniel@0: zc = sum( di(2:end,:,:).*di(1:(end-1),:,:) < 0 ) /nl; Daniel@0: if strcmp(option.per,'Second') Daniel@0: zc = zc*f{h}; Daniel@0: end Daniel@0: if strcmp(option.dir,'One') Daniel@0: zc = zc/2; Daniel@0: end Daniel@0: v{h}{i} = zc; Daniel@0: end Daniel@0: end Daniel@0: z = mirscalar(a,'Data',v,'Title','Zero-crossing rate');