comparison toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirdist.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 d = mirdist(x,y,dist)
2 % d = mirdist(x,y) evaluates the distance between x and y.
3 % x is the feature values corresponding to one audio file, and y is the
4 % values (for the same feature) corrdponding to one (or several)
5 % audio files.
6 % If x and y are not decomposed into frames,
7 % d = mirdist(x,y,f) specifies distance function.
8 % Default value: f = 'Cosine'
9 % If x and y are composed of clustered frames (using mircluster), the
10 % cluster signatures are compared using Earth Mover Distance.
11 % (Logan, Salomon, 2001)
12 % If x and y contains peaks, the vectors representing the peak
13 % distributions are compared using Euclidean distance.
14 % (used with mirnovelty in Jacobson, 2006)
15 %
16 % The Earth Mover Distance is based on the implementation by Yossi Rubner,
17 % wrapped for Matlab by Elias Pampalk.
18
19 if not(isa(x,'mirdata'))
20 x = miraudio(x);
21 end
22 if not(isa(y,'mirdata'))
23 y = miraudio(y);
24 end
25
26 clx = get(x,'Clusters');
27 if isempty(clx{1})
28 px = get(x,'PeakPos');
29 if not(iscell(px)) || isempty(px{1}) || ...
30 not(iscell(px{1})) || isempty(px{1}{1}) || not(iscell(px{1}{1}))
31 if nargin < 3
32 dist = 'Cosine';
33 end
34
35 d = get(x,'Data');
36 dd = d{1}{1};
37 if iscell(dd)
38 dd = dd{1};
39 end
40 if size(dd,2)>1
41 if size(dd,1)>1
42 error('ERROR IN MIRDIST: If the input is decomposed into frames, they should first be clustered.');
43 else
44 dd = dd';
45 end
46 end
47
48 e = get(y,'Data');
49 dt = cell(1,length(e));
50 for h = 1:length(e)
51 ee = e{h}{1};
52 if iscell(ee)
53 ee = ee{1};
54 end
55 if size(ee,2)>1
56 if size(ee,1)>1
57 error('ERROR IN MIRDIST: If the input is decomposed into frames, they should first be clustered.');
58 else
59 ee = ee';
60 end
61 end
62 if isempty(ee)
63 if isempty(dd)
64 dt{h}{1} = 0;
65 else
66 dt{h}{1} = Inf;
67 end
68 else
69 if length(dd)<length(ee)
70 dd(length(ee)) = 0;
71 %ee = ee(1:length(d));
72 elseif length(ee)<length(dd)
73 ee(length(dd)) = 0;
74 %dd = dd(1:length(ee));
75 end
76 if length(dd) == 1
77 dt{h}{1} = abs(dd-ee);
78 elseif norm(dd) && norm(ee)
79 dt{h}{1} = pdist([dd(:)';ee(:)'],dist);
80 else
81 dt{h}{1} = NaN;
82 end
83 end
84 end
85 else
86 % Euclidean distance between vectors to compare data with peaks
87 % (used with mirnovelty in Jacobson, 2006).
88 sig = pi/4;
89 dx = get(x,'Data');
90 nx = length(px{1}{1}{1});
91 cx = mean(px{1}{1}{1}/length(dx{1}{1}));
92 dy = get(y,'Data');
93 py = get(y,'PeakPos');
94 dt = cell(1,length(py));
95 for h = 1:length(py)
96 ny = length(py{h}{1}{1});
97 cy = mean(py{h}{1}{1}/length(dy{h}{1}));
98 dt{h}{1} = sqrt((nx*cos(sig*cx)-ny*cos(sig*cy))^2 ...
99 +(nx*sin(sig*cx)-ny*sin(sig*cy))^2);
100 end
101 end
102 else
103 % Earth Mover's Distance to compare clustered data.
104 cly = get(y,'Clusters');
105 dt = cell(1,length(cly));
106 for h = 1:length(cly)
107 cost = zeros(length(clx{1}.weight),length(cly{h}.weight));
108 for i = 1:length(clx{1}.weight)
109 for j = 1:length(cly{h}.weight)
110 covx = clx{1}.covar(:,i);
111 covy = cly{h}.covar(:,j);
112 mux = clx{1}.centr(:,i);
113 muy = cly{h}.centr(:,j);
114 cost(i,j) = sum(covx./covy + covy./covx + ...
115 (mux-muy).^2.*(1./covx + 1./covy) - 2);
116 end
117 end
118 dt{h}{1} = emd_wrapper(cost,clx{1}.weight,cly{h}.weight);
119 end
120 end
121 d = mirscalar(y,'Data',dt,'Title',[get(y,'Title'),' Distance'],...
122 'Name',get(x,'Name'),'Name2',get(y,'Name'));