comparison src/matlab/nmf_divergence.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
comparison
equal deleted inserted replaced
-1:000000000000 0:c52bc3e8d3ad
1 function [Y, cost] = nmf_divergence(V, W, varargin)
2
3 if nargin > 2
4 nmf_params = varargin{1};
5 L = nmf_params.Iterations;
6 convergence = nmf_params.Convergence_criteria;
7 r = nmf_params.Repition_restriction;
8 p = nmf_params.Polyphony_restriction;
9 c = nmf_params.Continuity_enhancement;
10 rot = nmf_params.Continuity_enhancement_rot;
11 pattern = nmf_params.Diagonal_pattern;
12 endtime = nmf_params.Modification_application;
13 rng(nmf_params.Random_seed);
14 elseif nargin == 2
15 L = 10;
16 convergence = 0;
17 r = -1;
18 p = -1;
19 c = -1;
20 pattern = 'Diagonal';
21 endtime = false;
22 rng('shuffle');
23 end
24
25 waitbarHandle = waitbar(0, 'Starting NMF synthesis...');
26
27 cost=0;
28 K=size(W, 2);
29 M=size(V, 2);
30
31 H=random('unif',0, 1, K, M);
32
33 P=zeros(K, M);
34 R=zeros(K, M);
35 C=zeros(K, M);
36
37 V = V+1E-6;
38 W = W+1E-6;
39 den = sum(W);
40
41 for l=1:L-1
42 waitbar(l/(L-1), waitbarHandle, ['Computing approximation...Iteration: ', num2str(l), '/', num2str(L-1)])
43
44 recon = W*H;
45 for mm = 1:size(H,2)
46 num = V(:,mm).*(1./recon(:,mm));
47 num2 = num'*W./den;
48 H(:,mm) = H(:, mm).*num2';
49 end
50
51 if((r > 0 && ~endtime) || (r > 0 && endtime && l==L-1))
52 waitbar(l/(L-1), waitbarHandle, ['Repition Restriction...Iteration: ', num2str(l), '/', num2str(L-1)])
53 for k = 1:size(H, 1)
54 for m = 1:size(H, 2)
55 if(m>r && (m+r)<=M && H(k,m)==max(H(k,m-r:m+r)))
56 R(k,m)=H(k,m);
57 else
58 R(k,m)=H(k,m)*(1-(l+1)/L);
59 end
60 end
61 end
62
63 H = R;
64 end
65
66 if((p > 0 && ~endtime) || (p > 0 && endtime && l==L-1))
67 waitbar(l/(L-1), waitbarHandle, ['Polyphony Restriction...Iteration: ', num2str(l), '/', num2str(L-1)])
68 P = zeros(size(H));
69 mask = zeros(size(H,1),1);
70 for m = 1:size(H, 2)
71 [~, sortedIndices] = sort(H(:, m),'descend');
72 mask(sortedIndices(1:p)) = 1;
73 mask(sortedIndices(p+1:end)) = (1-(l+1)/L);
74 P(:,m)=H(:,m).*mask;
75 end
76 H = P;
77 end
78
79 if((c > 0 && ~endtime) || (c > 0 && endtime && l==L-1))
80 waitbar(l/(L-1), waitbarHandle, ['Continuity Enhancement...Iteration: ', num2str(l), '/', num2str(L-1)])
81 switch pattern
82 case 'Diagonal'
83 C = conv2(H, rot_kernel( eye(c), rot ), 'same'); %Default
84 case 'Reverse'
85 C = conv2(H, flip(eye(c)), 'same'); %Reverse
86 case 'Blur'
87 C = conv2(H, flip(ones(c)), 'same'); %Blurring
88 case 'Vertical'
89 M = zeros(c, c); %Vertical
90 M(:, floor(c/2)) = 1;
91 C = conv2(P, M, 'same');
92 end
93
94 H = C;
95 end
96
97 if ~endtime
98 recon = W*H;
99 for mm = 1:size(H,2)
100 num = V(:,mm).*(1./recon(:,mm));
101 num2 = num'*W./den;
102 H(:,mm) = H(:, mm).*num2';
103 end
104 end
105
106 cost(l)=KLDivCost(V, W*H);
107 if(l>3 && (abs(((cost(l)-cost(l-1)))/max(cost))<=convergence))
108 break;
109 end
110 end
111
112 fprintf('Iterations: %i/%i\n', l, L);
113 fprintf('Convergence Criteria: %i\n', convergence*100);
114 fprintf('Repitition: %i\n', r);
115 fprintf('Polyphony: %i\n', p);
116 fprintf('Continuity: %i\n', c);
117
118 Y=H;
119 Y = Y./max(max(Y)); %Normalize activations
120
121 close(waitbarHandle);
122 end