Mercurial > hg > camir-aes2014
comparison toolboxes/MIRtoolbox1.3.2/MIRToolbox/mircluster.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 [a d] = mircluster(a,varargin) | |
2 % c = mircluster(a,f) clusters the segments in the audio sequence(s) | |
3 % contained in the audio object a, along the analytic feature(s) | |
4 % f, using the k-means strategy. Multiple analytic features have to | |
5 % be grouped into one array of cells. | |
6 % Example: | |
7 % sg = mirsegment(a); | |
8 % mircluster(sg, mirmfcc(sg)) | |
9 % mircluster(sg, {mirmfcc(sg), mircentroid(sg)}) | |
10 % c = mircluster(d) clusters the frame-decomposed data d into groups | |
11 % using K-means clustering. | |
12 % Example: | |
13 % cc = mirmfcc(a,'Frame'); | |
14 % mircluster(cc) | |
15 % Optional argument: | |
16 % mircluster(...,n) indicates the maximal number of clusters. | |
17 % Default value: n = 2. | |
18 % mircluster(...,'Runs',r) indicates the maximal number of runs. | |
19 % Default value: r = 5. | |
20 % | |
21 % Requires SOM Toolbox (included in the MIRtoolbox distribution). | |
22 | |
23 | |
24 nruns.key = 'Runs'; | |
25 nruns.type = 'Integer'; | |
26 nruns.default = 5; | |
27 option.nruns = nruns; | |
28 | |
29 nclust.position = 2; | |
30 nclust.type = 'Integer'; | |
31 nclust.default = 2; | |
32 option.nclust = nclust; | |
33 | |
34 specif.option = option; | |
35 | |
36 specif.nochunk = 1; | |
37 | |
38 d = a; | |
39 if isa(a,'mirdesign') | |
40 if not(get(a,'Eval')) | |
41 % During bottom-up construction of the general design | |
42 | |
43 [unused option] = miroptions(@mircluster,a,specif,varargin); | |
44 type = get(a,'Type'); | |
45 a = mirdesign(@mircluster,a,option,{},struct,type); | |
46 a = set(a,'NoChunk',1); | |
47 else | |
48 % During top-down evaluation initiation | |
49 | |
50 e = evaleach(a); | |
51 if iscell(e) | |
52 e = e{1}; | |
53 end | |
54 a = mircluster(e,varargin{:}); | |
55 end | |
56 else | |
57 if not(isa(a,'mirdata')) | |
58 mirerror('mircluster','The input should be either frame- or segment-decomposed.'); | |
59 end | |
60 | |
61 if isempty(varargin) || (not(isa(varargin{1},'mirdata') || ... | |
62 (iscell(varargin{1}) && ... | |
63 isa(varargin{1}{1},'mirdata')))) | |
64 % mircluster version for frame-decomposed data: | |
65 % frames are clustered into groups using K-means clustering. | |
66 [unused option] = miroptions(@mircluster,a,specif,varargin); | |
67 da = get(a,'Data'); | |
68 lva = length(da); % Number of audio files in the audio object. | |
69 c = cell(1,lva); | |
70 display('Clustering frames...'); | |
71 if mirwaitbar | |
72 handle = waitbar(0,'Clustering frames...'); | |
73 else | |
74 handle = 0; | |
75 end | |
76 for j = 1:lva % For each audio file,... | |
77 va = []; % Data transmitted to the kmeans_cluster function. | |
78 v = da{j}; | |
79 if iscell(v) | |
80 v = uncell(v,-Inf); %v{1}; | |
81 end | |
82 if size(v,4)>1 | |
83 v(end+1:2*end,:,:,1) = v(:,:,:,2); | |
84 v(:,:,:,2) = []; | |
85 end | |
86 % Standardization | |
87 %stv = std(v,0,2); | |
88 %stv(find(stv == 0)) = 1; | |
89 va(end+1:end+size(v,1),:,:) = v;%... | |
90 %(v - repmat(mean(v,2),[1 size(v,2) ])) ... | |
91 %./ repmat(stv,[1 size(v,2) ]); | |
92 if isa(a,'mirscalar') | |
93 m = get(a,'Mode'); | |
94 if not(isempty(m)) | |
95 m = m{j}; | |
96 val = []; | |
97 for l = 1:nseg | |
98 vl = m{l}; | |
99 if iscell(vl) | |
100 vl = vl{1}; | |
101 end | |
102 val(:,l) = vl; | |
103 end | |
104 stv = std(val,0,2); | |
105 stv(find(stv == 0)) = 1; | |
106 va(end+1:end+size(val,1),:) = ... | |
107 (val - repmat(mean(val,2),[1 size(val,2) ])) ... | |
108 ./ repmat(stv,[1 size(val,2) ]); | |
109 end | |
110 end | |
111 if size(va,3)>1 | |
112 mel = 1; | |
113 va = reshape(va,size(va,2),size(va,3))'; | |
114 else | |
115 mel = 0; | |
116 end | |
117 [cc, p, err, ind] = kmeans_clusters(va',option.nclust,option.nruns); | |
118 [minind select] = min(ind); | |
119 c{j}.centr = cc{select}'; | |
120 c{j}.index = p{select}; | |
121 c{j}.weight = zeros(1,size(cc{select},1)); | |
122 c{j}.covar = zeros(size(cc{select}')); | |
123 ii = 1; | |
124 for i = 1:size(c{j}.centr,2) | |
125 clus = va(:,c{j}.index == ii); | |
126 if isempty(clus) | |
127 higher = find(c{j}.index > ii); | |
128 c{j}.index(higher) = c{j}.index(higher)-1; | |
129 c{j}.centr(:,ii) = []; | |
130 c{j}.weight(ii) = []; | |
131 c{j}.covar(:,ii) = []; | |
132 else | |
133 c{j}.weight(ii) = size(clus,2)/size(va,2); | |
134 if c{j}.weight(ii) == 0 | |
135 pause | |
136 end | |
137 c{j}.covar(:,ii) = mean((clus'-ones(1,size(clus,1))*c{j}.centr(:,ii)).^2); | |
138 ii = ii+1; | |
139 end | |
140 end | |
141 if handle | |
142 waitbar(j/lva,handle); | |
143 end | |
144 end | |
145 if handle | |
146 delete(handle) | |
147 end | |
148 a = set(a,'Clusters',c); | |
149 else | |
150 % mircluster version for segmented audio: | |
151 % segments are clustered into groups using K-means clustering. | |
152 da = varargin{1}; | |
153 varargin(1) = []; | |
154 [unused option] = miroptions(@mircluster,a,specif,varargin); | |
155 display('Clustering segments...'); | |
156 if isa(da,'mirdata') || (iscell(da) && isa(da{1},'mirdata')) | |
157 if not(iscell(da)) | |
158 da = {da}; | |
159 end | |
160 vala = get(a,'Data'); % Data contained in the audio object a. | |
161 lva = length(vala); % Number of audio files in the audio object. | |
162 clus = cell(1,lva); | |
163 for j = 1:lva % For each audio file,... | |
164 va = []; % Data transmitted to the kmeans_cluster function. | |
165 nseg = length(vala{j}); % Number of segments in the audio file. | |
166 for i = 1:length(da) % For each analytic feature,... | |
167 v = get(da{i},'Data'); | |
168 v = v{j}; | |
169 if iscell(v) | |
170 v = uncell(v,-Inf); %v{1}; | |
171 end | |
172 val = []; | |
173 if size(v,4)>1 | |
174 v(end+1:2*end,:,:,1) = v(:,:,:,2); | |
175 v(:,:,:,2) = []; | |
176 end | |
177 | |
178 % Standardization | |
179 stv = std(v,0,2); | |
180 stv(find(stv == 0)) = 1; | |
181 va(end+1:end+size(v,1),:) = ... | |
182 (v - repmat(mean(v,2),[1 size(v,2) ])) ... | |
183 ./ repmat(stv,[1 size(v,2) ]); | |
184 if isa(da{i},'mirscalar') | |
185 m = get(da{i},'Mode'); | |
186 if not(isempty(m)) | |
187 m = m{j}; | |
188 val = []; | |
189 for l = 1:nseg | |
190 vl = m{l}; | |
191 if iscell(vl) | |
192 vl = vl{1}; | |
193 end | |
194 val(:,l) = vl; | |
195 end | |
196 stv = std(val,0,2); | |
197 stv(find(stv == 0)) = 1; | |
198 va(end+1:end+size(val,1),:) = ... | |
199 (val - repmat(mean(val,2),[1 size(val,2) ])) ... | |
200 ./ repmat(stv,[1 size(val,2) ]); | |
201 end | |
202 end | |
203 | |
204 end | |
205 [cc, p, err, ind] = kmeans_clusters(va',min(option.nclust,nseg),option.nruns); | |
206 clus{j} = p{end}; | |
207 end | |
208 a = set(a,'Clusters',clus); | |
209 t = get(a,'Time'); | |
210 fp = get(a,'FramePos'); | |
211 for j = 1:lva % For each audio file,... | |
212 aj = vala{j}; | |
213 tj = t{j}; | |
214 fpj = fp{j}; | |
215 clj = clus{j}; | |
216 k = 2; | |
217 while k <= length(aj) | |
218 if clj(k) == clj(k-1) | |
219 aj{k-1} = [aj{k-1};aj{k}]; | |
220 aj(k) = []; | |
221 tj{k-1} = [tj{k-1};tj{k}]; | |
222 tj(k) = []; | |
223 fpj{k-1} = [fpj{k-1}(1);fpj{k}(2)]; | |
224 fpj(k) = []; | |
225 clj(k) = []; | |
226 k = k-1; | |
227 end | |
228 k = k+1; | |
229 end | |
230 vala{j} = aj; | |
231 t{j} = tj; | |
232 fp{j} = fpj; | |
233 cl{j} = clj; | |
234 end | |
235 a = set(a,'Data',vala,'Time',t,'FramePos',fp,'Clusters',cl); | |
236 end | |
237 end | |
238 end |