Mercurial > hg > nimfks
comparison src/matlab/SA_B_NMF.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 % Author: Dr. Elio Quinton | |
2 | |
3 function [ W, H, deleted, finalCost ] = SA_B_NMF(V, W, lambda, varargin ) | |
4 %SENMF Summary of this function goes here | |
5 % Detailed explanation goes here | |
6 if nargin > 2 | |
7 nmf_params = varargin{1}; | |
8 iterations = nmf_params.Iterations; | |
9 lambda = nmf_params.Lambda; | |
10 elseif nargin == 2 | |
11 iterations = 500; | |
12 lambda = 5; | |
13 end | |
14 | |
15 waitbarHandle = waitbar(0, 'Starting sparse NMF synthesis...'); | |
16 | |
17 targetDim=size(V); | |
18 sourceDim=size(W); | |
19 K=sourceDim(2); | |
20 M=targetDim(2); | |
21 H=random('unif',0, 1, K, M); | |
22 | |
23 deleted = []; | |
24 H = 0.01 * ones(size(H));% + (0.01 * rand(size(H))); | |
25 cost = get_cost(V, W, H, lambda); % Function get_cost defined at the end of this file. | |
26 | |
27 converged = 0; | |
28 convergence_threshold = 50; % Note that this is a threshold on the derivative of the cost, i.e. how much it decays at each iteration. This value might not be ideal for our use case. | |
29 | |
30 myeps = 10e-3; % A bigger eps here helps pruning out unused components | |
31 V = V + myeps; % Note that V here is the `target' audio magnitude spectrogram | |
32 | |
33 % num_h_iter = 1; | |
34 err = 0.0001; | |
35 % max_iter = 1000; | |
36 max_iter = iterations; | |
37 | |
38 | |
39 | |
40 iter = 0; | |
41 omit = 0; | |
42 | |
43 while ~converged && iter < max_iter | |
44 | |
45 waitbar(iter/(iterations-1), waitbarHandle, ['Computing approximation...Iteration: ', num2str(iter), '/', num2str(iterations-1)]) | |
46 iter = iter + 1; | |
47 %% sparse NMF decomposition | |
48 if iter > 0 | |
49 | |
50 % Update H. | |
51 R = W*H+eps; | |
52 RR = 1./R; | |
53 RRR = RR.^2; | |
54 RRRR = sqrt(R); | |
55 | |
56 pen = lambda./(sqrt(H + eps)); | |
57 | |
58 H = H .* (((W' * (V .* RRR.* RRRR)) ./ ((W' * (RR .* RRRR) + pen))).^(1/3)); | |
59 | |
60 | |
61 %Update W: REMOVE THIS FOR OUR USE CASE | |
62 % nn = sum(sqrt(H')); | |
63 % NN = lambda * repmat(nn,size(V,1),1); | |
64 % NNW = NN.*W; | |
65 % | |
66 % R = W*H+eps; | |
67 % RR = 1./R; | |
68 % RRR = RR.^2; | |
69 % RRRR = sqrt(R); | |
70 % W = W .* ( ((V .* RRR.* RRRR)*H') ./ ( ((RR .* RRRR)*H') + NNW + eps)).^(1/3); | |
71 % Update W: stop deleting here | |
72 | |
73 | |
74 else | |
75 % Non-sparse first iteration. Not sure it is necessary | |
76 % in our particular use case. We might want to get rid of | |
77 % it later, but it should not harm anyway. | |
78 R = W*H; | |
79 RR = 1./R; | |
80 RRR = RR.^2; | |
81 RRRR = sqrt(R); | |
82 H = H .* (((W' * (V .* RRR.* RRRR)) ./ (W' * (RR .* RRRR) + eps)).^(1/3)); | |
83 | |
84 %Update W: REMOVE THIS FOR OUR USE CASE | |
85 % R = W*H + myeps; | |
86 % RR = 1./R; | |
87 % RRR = RR.^2; | |
88 % RRRR = sqrt(R); | |
89 % W = W .* ((((V .* RRR.* RRRR)*H') ./ (((RR .* RRRR)*H') + eps)).^(1/3)); | |
90 % Update W: stop deleting here | |
91 end | |
92 | |
93 %% normalise and prune templates if their total activation reaches 0. | |
94 todel = []; | |
95 shifts = []; | |
96 for i = 1:size(W,2) | |
97 % nn = norm(W(:,i)); % W is not being updated so this is of no use | |
98 nn = sum(H(i,:)); % Check if norm of rows of H get to zero instead. | |
99 if nn == 0 | |
100 todel = [todel i]; | |
101 % disp(['Deleting ' int2str(length(todel))]); % This is printing a message everytime templates are deleted | |
102 else | |
103 nn = norm(W(:,i)); % Still normalise against norm of Templates to avoid division by zero. | |
104 W(:,i) = W(:,i) / nn; | |
105 H(i,:) = H(i,:) * nn; | |
106 end | |
107 end | |
108 | |
109 if( length(deleted) == 0 ) | |
110 deleted = [deleted todel]; | |
111 else | |
112 shifts = zeros(1, length(todel)); | |
113 for i = 1:length(shifts) | |
114 shifts(i) = length( deleted( deleted >= todel(i) ) ); | |
115 end | |
116 deleted = [deleted todel+shifts]; | |
117 end | |
118 W(:,todel) = []; | |
119 H(todel,:) = []; | |
120 | |
121 %% get the cost and monitor convergence | |
122 if (mod(iter, 5) == 0) || (iter == 1) | |
123 | |
124 new_cost = get_cost(V, W, H, lambda); | |
125 | |
126 if omit == 0 && cost - new_cost < cost * err & iter > convergence_threshold | |
127 converged = 0; | |
128 % | |
129 end | |
130 | |
131 cost = new_cost; | |
132 finalCost(iter) = cost; | |
133 omit = 0; | |
134 | |
135 % disp([int2str(iter) ' ' num2str(cost)]); % this prints the cost function at each iteration. Could be commented out (printing is slow in matlab) | |
136 elseif iter > 1 | |
137 finalCost(iter) = finalCost(iter - 1); | |
138 end | |
139 | |
140 end | |
141 | |
142 close(waitbarHandle); | |
143 end | |
144 | |
145 | |
146 | |
147 function cost = get_cost(V, W, H, lambda) | |
148 R = W*H+eps; | |
149 hcost = sum(sum( (sqrt(V) - sqrt(R).^2 )./sqrt(R) )); | |
150 nn = 2 * lambda * sum(sum(sqrt(H))); | |
151 cost = hcost + nn; | |
152 | |
153 end | |
154 |