comparison toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirinharmonicity.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function varargout = mirinharmonicity(orig,varargin)
2 % ih = mirinharmonicity(x) estimates the inharmonicity of x, i.e., the
3 % amount of partials that are not multiples of the fundamental
4 % frequency.
5 % x can be either an audio file, a miraudio or a mirspectrum object.
6 % WARNING: This function presupposes that there is only one fundamental
7 % frequency.
8 % Optional argument:
9 % mirinharmonicity(...,'f0',f) bases the computation of the
10 % inharmonicity on the fundamental frequency indicated by f.
11 % Default value: f = mirpitch(...,'Mono')
12 % [ih,s] = mirinharmonicity(x) also displays the spectrum used for the
13 % computation of the inharmonicity.
14 % [ih,s,p] = mirinharmonicity(x) also displays the result of the
15 % estimation of the fundamental frequency.
16
17 f0.key = 'f0';
18 f0.default = [];
19 option.f0 = f0;
20
21 frame.key = 'Frame';
22 frame.type = 'Integer';
23 frame.number = 2;
24 frame.default = [0 0];
25 frame.keydefault = [.1 .125];
26 option.frame = frame;
27
28 specif.option = option;
29
30 varargout = mirfunction(@mirinharmonicity,orig,varargin,nargout,specif,@init,@main);
31
32
33 function [i type] = init(x,option)
34 if isamir(x,'miraudio')
35 if option.frame.length.val
36 s = mirspectrum(x,'Frame',option.frame.length.val,...
37 option.frame.length.unit,...
38 option.frame.hop.val,...
39 option.frame.hop.unit);
40 else
41 s = mirspectrum(x);
42 end
43 else
44 s = x;
45 end
46 if isempty(option.f0)
47 if option.frame.length.val
48 p = mirpitch(x,'Mono','Frame',option.frame.length.val,...
49 option.frame.length.unit,...
50 option.frame.hop.val,...
51 option.frame.hop.unit);
52 else
53 p = mirpitch(x,'Mono');
54 end
55 else
56 p = option.f0;
57 end
58 i = {s,p};
59 type = {'mirscalar','mirspectrum','mirscalar'};
60
61
62 function ih = main(x,option,postoption)
63 if isa(x{2},'mirdesign')
64 x = x{1};
65 end
66 s = x{1};
67 p = x{2};
68 if iscell(p)
69 p = p{1};
70 end
71 m = get(s,'Magnitude');
72 f = get(s,'Frequency');
73 fp1 = get(s,'FramePos');
74 if isnumeric(p)
75 pf = {{{p}}};
76 else
77 pf = get(p,'Data');
78 fp2 = get(p,'FramePos');
79 end
80 v = cell(1,length(m));
81 for h = 1:length(m)
82 v{h} = cell(1,length(m{h}));
83 for i = 1:length(m{h})
84 mi = m{h}{i};
85 fi = f{h}{i};
86 pfi = pf{h}{i};
87 v{h}{i} = zeros(1,size(mi,2),size(mi,3));
88 if not(size(mi,2) == size(pfi,2))
89 beg = find(fp2{h}{i}(1,:) == fp1{h}{i}(1,1));
90 if isempty(beg) || (beg + size(mi,2)-1 > size(pfi,2))
91 error('ERROR IN MIRINHARMONICITY: The ''f0'' argument should have the same frame decomposition than the main input.');
92 end
93 pfi = pfi(:,beg:beg+size(mi,2)-1);
94 end
95 for j = 1:size(mi,3)
96 for k = 1:size(mi,2)
97 mk = mi(:,k,j);
98 fk = fi(:,k,j);
99 pfk = pfi(:,k);
100 if isempty(pfk{1})
101 v{h}{i}(1,k,j) = NaN;
102 else
103 r = fk/pfk{1}(1);
104 rr = 2*abs(r-round(r));
105 if isempty(rr)
106 v{h}{i}(1,k,j) = NaN;
107 else
108 v{h}{i}(1,k,j) = sum(rr.*mk) / sum(mk);
109 end
110 end
111 end
112 end
113 end
114 end
115 ih = mirscalar(s,'Data',v,'Title','Inharmonicity');
116 if isa(p,'mirdata')
117 ih = {ih s p};
118 else
119 ih = {ih s};
120 end