boblsturm@0
|
1 function [ Y, PrunedFrames, KeepCorpusFrames ] = prune_corpus( target, corpus, reduction_coef )
|
boblsturm@0
|
2
|
boblsturm@0
|
3 if reduction_coef == 1
|
boblsturm@0
|
4 Y = corpus;
|
boblsturm@0
|
5 PrunedFrames = [];
|
boblsturm@0
|
6 KeepCorpusFrames = [];
|
boblsturm@0
|
7 return;
|
boblsturm@0
|
8 end
|
boblsturm@0
|
9 [targetRows, targetCols]= size( target );
|
boblsturm@0
|
10 [corpusRows, corpusCols]= size( corpus );
|
boblsturm@0
|
11
|
boblsturm@0
|
12 % Matrix: distances of every target frame to every other target frame
|
boblsturm@0
|
13 % i.e. Self-similarity matrix
|
boblsturm@0
|
14 TargetSelfSimMat = zeros(targetCols);
|
boblsturm@0
|
15
|
boblsturm@0
|
16 % Matrix: distances of every target frame to every corpus frame
|
boblsturm@0
|
17 % Matrix( i, j ) = Similarity of corpus frame i to target frame j
|
boblsturm@0
|
18 % TargetToCorpSimMat = -1*ones(WCols, targetCols);
|
boblsturm@0
|
19
|
boblsturm@0
|
20 % Calculate self-similarity of target (euclidean dist)
|
boblsturm@0
|
21 X2 = sum(target.^2,1);
|
boblsturm@0
|
22 TargetSelfSimMat = bsxfun(@plus,X2,X2')-2*(target'*target);
|
boblsturm@0
|
23 % TargetSelfSimMat = exp(-(1/10) * DistanceMat);
|
boblsturm@0
|
24
|
boblsturm@0
|
25 % Calculate the distance between each corpus frame to first target
|
boblsturm@0
|
26 % frame
|
boblsturm@0
|
27 KeepCorpusFrames = [];
|
boblsturm@0
|
28 RemainingTargetFrames = 1:targetCols;
|
boblsturm@0
|
29 RemainingCorpusFrames = 1:corpusCols;
|
boblsturm@0
|
30
|
boblsturm@0
|
31 while ~isempty(RemainingTargetFrames)
|
boblsturm@0
|
32 Dist = repmat(target(:,RemainingTargetFrames(1)),1, ...
|
boblsturm@0
|
33 length(RemainingCorpusFrames)) - corpus(:,RemainingCorpusFrames);
|
boblsturm@0
|
34 Distances = sum(Dist.^2,1);
|
boblsturm@0
|
35 % compute mean distances
|
boblsturm@0
|
36 meanDistance = mean(Distances);
|
boblsturm@0
|
37
|
boblsturm@0
|
38 % find those corpus frames closer than reduction_coef of mean
|
boblsturm@0
|
39 idxcorpuskeep = Distances < reduction_coef*meanDistance;
|
boblsturm@0
|
40 KeepCorpusFrames = [KeepCorpusFrames RemainingCorpusFrames(idxcorpuskeep)];
|
boblsturm@0
|
41
|
boblsturm@0
|
42 % find those target frames within twice that to next frame
|
boblsturm@0
|
43 distancetonext = TargetSelfSimMat(RemainingTargetFrames(1)+1,RemainingTargetFrames(1));
|
boblsturm@0
|
44 idxtarget = TargetSelfSimMat(RemainingTargetFrames,RemainingTargetFrames(1)) ...
|
boblsturm@0
|
45 < 2*distancetonext;
|
boblsturm@0
|
46
|
boblsturm@0
|
47 if ~any( idxtarget ) && distancetonext == 0
|
boblsturm@0
|
48 idxtarget = RemainingTargetFrames(2);
|
boblsturm@0
|
49 end
|
boblsturm@0
|
50
|
boblsturm@0
|
51 % shrink corpus
|
boblsturm@0
|
52 RemainingCorpusFrames = RemainingCorpusFrames(~idxcorpuskeep);
|
boblsturm@0
|
53
|
boblsturm@0
|
54 % shrink target frames index
|
boblsturm@0
|
55 RemainingTargetFrames = RemainingTargetFrames(~idxtarget);
|
boblsturm@0
|
56
|
boblsturm@0
|
57 fprintf( '%d remaining frames...\n', size( RemainingTargetFrames ) );
|
boblsturm@0
|
58 end
|
boblsturm@0
|
59
|
boblsturm@0
|
60 KeepCorpusFrames = sort( KeepCorpusFrames );
|
boblsturm@0
|
61 Y = corpus( :, KeepCorpusFrames );
|
boblsturm@0
|
62 PrunedFrames = setdiff( 1:corpusCols, KeepCorpusFrames );
|
boblsturm@0
|
63
|
boblsturm@0
|
64 fprintf( 'Pruned %d frames...\n', size( PrunedFrames ) );
|
boblsturm@0
|
65
|
boblsturm@0
|
66 end |