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