annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirzerocross.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function varargout = mirzerocross(orig,varargin)
Daniel@0 2 % mirzeroscross(x) computes the sign-changes rate along the signal x,
Daniel@0 3 % i.e., how many time the waveform crosses the X-axis. When applied on
Daniel@0 4 % an audio waveform, gives a notion of noise.
Daniel@0 5 % Optional argument:
Daniel@0 6 % mirzerocross(...,'Per',p) precises the temporal reference for the
Daniel@0 7 % rate computation.
Daniel@0 8 % Possible values:
Daniel@0 9 % p = 'Second': number of sign-changes per second (Default).
Daniel@0 10 % p = 'Sample': number of sign-changes divided by the total
Daniel@0 11 % number of samples.
Daniel@0 12 % The 'Second' option returns a result equal to the one returned
Daniel@0 13 % by the 'Sample' option multiplied by the sampling rate.
Daniel@0 14 % mirzerocross(...,'Dir',d) precises the definition of sign change.
Daniel@0 15 % Possible values:
Daniel@0 16 % d = 'One': number of sign-changes from negative to positive
Daniel@0 17 % only (or, equivalently, from positive to negative only).
Daniel@0 18 % (Default)
Daniel@0 19 % d = 'Both': number of sign-changes in both ways.
Daniel@0 20 % The 'Both' option returns a result equal to twice the one
Daniel@0 21 % returned by the 'One' option.
Daniel@0 22
Daniel@0 23
Daniel@0 24 per.key = 'Per';
Daniel@0 25 per.type = 'String';
Daniel@0 26 per.choice = {'Second','Sample'};
Daniel@0 27 per.default = 'Second';
Daniel@0 28 option.per = per;
Daniel@0 29
Daniel@0 30 dir.key = 'Dir';
Daniel@0 31 dir.type = 'String';
Daniel@0 32 dir.choice = {'One','Both'};
Daniel@0 33 dir.default = 'One';
Daniel@0 34 option.dir = dir;
Daniel@0 35
Daniel@0 36 specif.option = option;
Daniel@0 37
Daniel@0 38 varargout = mirfunction(@mirzerocross,orig,varargin,nargout,specif,@init,@main);
Daniel@0 39
Daniel@0 40
Daniel@0 41 function [x type] = init(x,option)
Daniel@0 42 if not(isamir(x,'mirdata'))
Daniel@0 43 x = miraudio(x);
Daniel@0 44 end
Daniel@0 45 type = 'mirscalar';
Daniel@0 46
Daniel@0 47
Daniel@0 48 function z = main(a,option,postoption)
Daniel@0 49 if iscell(a)
Daniel@0 50 a = a{1};
Daniel@0 51 end
Daniel@0 52 d = get(a,'Data');
Daniel@0 53 f = get(a,'Sampling');
Daniel@0 54 v = cell(1,length(d));
Daniel@0 55 for h = 1:length(d)
Daniel@0 56 v{h} = cell(1,length(d{h}));
Daniel@0 57 for i = 1:length(d{h})
Daniel@0 58 di = d{h}{i};
Daniel@0 59 nc = size(di,2);
Daniel@0 60 nf = size(di,3);
Daniel@0 61 nl = size(di,1);
Daniel@0 62 zc = sum( di(2:end,:,:).*di(1:(end-1),:,:) < 0 ) /nl;
Daniel@0 63 if strcmp(option.per,'Second')
Daniel@0 64 zc = zc*f{h};
Daniel@0 65 end
Daniel@0 66 if strcmp(option.dir,'One')
Daniel@0 67 zc = zc/2;
Daniel@0 68 end
Daniel@0 69 v{h}{i} = zc;
Daniel@0 70 end
Daniel@0 71 end
Daniel@0 72 z = mirscalar(a,'Data',v,'Title','Zero-crossing rate');