annotate src/matlab/prune_corpus.m @ 0:c52bc3e8d3ad tip

user: boblsturm branch 'default' added README.md added assets/.DS_Store added assets/playButton.jpg added assets/stopButton.png added assets/swapButton.jpg added data/.DS_Store added data/fiveoctaves.mp3 added data/glock2.wav added data/sinScale.mp3 added data/speech_female.mp3 added data/sweep.wav added nimfks.m.lnk added src/.DS_Store added src/matlab/.DS_Store added src/matlab/AnalysisCache.m added src/matlab/CSS.m added src/matlab/DataHash.m added src/matlab/ExistsInCache.m added src/matlab/KLDivCost.m added src/matlab/LoadFromCache.m added src/matlab/SA_B_NMF.m added src/matlab/SaveInCache.m added src/matlab/Sound.m added src/matlab/SynthesisCache.m added src/matlab/chromagram_E.m added src/matlab/chromagram_IF.m added src/matlab/chromagram_P.m added src/matlab/chromsynth.m added src/matlab/computeSTFTFeat.m added src/matlab/controller.m added src/matlab/decibelSliderReleaseCallback.m added src/matlab/drawClickCallBack.m added src/matlab/fft2chromamx.m added src/matlab/hz2octs.m added src/matlab/ifgram.m added src/matlab/ifptrack.m added src/matlab/istft.m added src/matlab/nimfks.fig added src/matlab/nimfks.m added src/matlab/nmfFn.m added src/matlab/nmf_beta.m added src/matlab/nmf_divergence.m added src/matlab/nmf_euclidean.m added src/matlab/prune_corpus.m added src/matlab/rot_kernel.m added src/matlab/templateAdditionResynth.m added src/matlab/templateDelCb.m added src/matlab/templateScrollCb.m
author boblsturm
date Sun, 18 Jun 2017 06:26:13 -0400
parents
children
rev   line source
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