boblsturm@0
|
1 classdef CSS < handle
|
boblsturm@0
|
2 properties
|
boblsturm@0
|
3 NMF_features
|
boblsturm@0
|
4 Activations
|
boblsturm@0
|
5 Cost
|
boblsturm@0
|
6 Synthesis_method
|
boblsturm@0
|
7 Synthesis
|
boblsturm@0
|
8 end
|
boblsturm@0
|
9
|
boblsturm@0
|
10 methods
|
boblsturm@0
|
11 function obj = CSS(varargin)
|
boblsturm@0
|
12 if nargin == 2
|
boblsturm@0
|
13 obj.NMF_features = varargin{1};
|
boblsturm@0
|
14 obj.Synthesis_method = varargin{2};
|
boblsturm@0
|
15 end
|
boblsturm@0
|
16 end
|
boblsturm@0
|
17 end
|
boblsturm@0
|
18
|
boblsturm@0
|
19 methods
|
boblsturm@0
|
20 function obj = nmf(obj, corpus_sound, target_sound, varargin)
|
boblsturm@0
|
21 if( nargin == 4 )
|
boblsturm@0
|
22 pct_prune = varargin{1}
|
boblsturm@0
|
23 else
|
boblsturm@0
|
24 pct_prune = 1
|
boblsturm@0
|
25 end
|
boblsturm@0
|
26 nmf_alg = obj.NMF_features.Algorithm;
|
boblsturm@0
|
27 target_spect = abs(target_sound.Features.STFT.S);
|
boblsturm@0
|
28 corpus_spect = abs(corpus_sound.Features.STFT.S);
|
boblsturm@0
|
29 [corpus_spect, pruned_frames, frames_to_keep] = prune_corpus( target_spect, corpus_spect, pct_prune );
|
boblsturm@0
|
30
|
boblsturm@0
|
31 switch nmf_alg
|
boblsturm@0
|
32 case 'Euclidean'
|
boblsturm@0
|
33 if length(fieldnames(obj.NMF_features)) > 1
|
boblsturm@0
|
34 [H, obj.Cost] = nmf_euclidean(target_spect, corpus_spect, obj.NMF_features);
|
boblsturm@0
|
35 else
|
boblsturm@0
|
36 [H, obj.Cost] = nmf_euclidean(target_spect, corpus_spect);
|
boblsturm@0
|
37 end
|
boblsturm@0
|
38 case 'Divergence'
|
boblsturm@0
|
39 if length(fieldnames(obj.NMF_features)) > 1
|
boblsturm@0
|
40 [H, obj.Cost] = nmf_divergence(target_spect, corpus_spect, obj.NMF_features);
|
boblsturm@0
|
41 else
|
boblsturm@0
|
42 [H, obj.Cost] = nmf_divergence(target_spect, corpus_spect);
|
boblsturm@0
|
43 end
|
boblsturm@0
|
44 case 'Sparse NMF'
|
boblsturm@0
|
45 if length(fieldnames(obj.NMF_features)) > 1
|
boblsturm@0
|
46 [~, H, deleted, obj.Cost] = SA_B_NMF(target_spect, corpus_spect, 5, obj.NMF_features);
|
boblsturm@0
|
47 else
|
boblsturm@0
|
48 [~, H, deleted, obj.Cost] = SA_B_NMF(target_spect, corpus_spect, 5 );
|
boblsturm@0
|
49 end
|
boblsturm@0
|
50
|
boblsturm@0
|
51 H( deleted, : ) = 0;
|
boblsturm@0
|
52 obj.Activations = H;
|
boblsturm@0
|
53 end
|
boblsturm@0
|
54
|
boblsturm@0
|
55 if size( frames_to_keep ) > 0
|
boblsturm@0
|
56 tmp = zeros( size( corpus_sound.Features.STFT.S,2 ), size( target_spect,2 ) );
|
boblsturm@0
|
57 for i = 1:length( frames_to_keep )
|
boblsturm@0
|
58 tmp(frames_to_keep(i), :) = H(i,:);
|
boblsturm@0
|
59 end
|
boblsturm@0
|
60 H = tmp;
|
boblsturm@0
|
61 end
|
boblsturm@0
|
62 % H( pruned_frames, : ) = 0;
|
boblsturm@0
|
63 % % Pad activations to size of corpus frames
|
boblsturm@0
|
64 % % since pruned frames maximum can be < size of corpus
|
boblsturm@0
|
65 % H( setdiff( 1:( size( corpus_spect, 2 ) + length( pruned_frames ) ), 1:size( H, 1 ) ), : ) = 0;
|
boblsturm@0
|
66 obj.Activations = H;
|
boblsturm@0
|
67 end
|
boblsturm@0
|
68
|
boblsturm@0
|
69 function obj = synthesize(obj, corpus_sound)
|
boblsturm@0
|
70 synth_method = obj.Synthesis_method;
|
boblsturm@0
|
71 win = corpus_sound.Features.window;
|
boblsturm@0
|
72 W = abs(corpus_sound.Features.STFT.S);
|
boblsturm@0
|
73 H = obj.Activations;
|
boblsturm@0
|
74
|
boblsturm@0
|
75 switch synth_method
|
boblsturm@0
|
76 case 'ISTFT'
|
boblsturm@0
|
77 parameters = [];
|
boblsturm@0
|
78 parameters.synHop = win.Hop;
|
boblsturm@0
|
79 parameters.win = window(lower(win.Type), win.Length);
|
boblsturm@0
|
80
|
boblsturm@0
|
81 reconstruction = W*H;
|
boblsturm@0
|
82 padding = size(reconstruction, 1)*2 - win.Length - 2;
|
boblsturm@0
|
83 if padding >= 0
|
boblsturm@0
|
84 parameters.zeroPad = padding;
|
boblsturm@0
|
85 end
|
boblsturm@0
|
86
|
boblsturm@0
|
87 obj.Synthesis = istft(reconstruction, parameters);
|
boblsturm@0
|
88 case 'Template Addition'
|
boblsturm@0
|
89 obj.Synthesis = templateAdditionResynth(corpus_sound.Signal, H, win);
|
boblsturm@0
|
90 end
|
boblsturm@0
|
91 end
|
boblsturm@0
|
92
|
boblsturm@0
|
93 function plot_activations(obj, varargin)
|
boblsturm@0
|
94 if(nargin > 1)
|
boblsturm@0
|
95 maxDb = varargin{1};
|
boblsturm@0
|
96 else
|
boblsturm@0
|
97 maxDb = -45;
|
boblsturm@0
|
98 end
|
boblsturm@0
|
99
|
boblsturm@0
|
100 H = obj.Activations;
|
boblsturm@0
|
101
|
boblsturm@0
|
102 HdB = 20*log10(H./max(max(H)));
|
boblsturm@0
|
103 HdB = HdB - maxDb;
|
boblsturm@0
|
104 HdB(HdB < 0) = 0;
|
boblsturm@0
|
105 imagesc(HdB);
|
boblsturm@0
|
106 cmap = colormap('gray');
|
boblsturm@0
|
107 cmap(1,:) = 0*ones(1,3);
|
boblsturm@0
|
108 colormap(flipud(cmap))
|
boblsturm@0
|
109 colorbar
|
boblsturm@0
|
110 axis xy; grid on;
|
boblsturm@0
|
111 set(gca, 'Layer', 'top');
|
boblsturm@0
|
112 ylabel('Template');
|
boblsturm@0
|
113 xlabel('Time');
|
boblsturm@0
|
114 grid on;
|
boblsturm@0
|
115 set(gca,'FontSize',16);
|
boblsturm@0
|
116 end
|
boblsturm@0
|
117
|
boblsturm@0
|
118 function plot_cost(obj)
|
boblsturm@0
|
119 plot(obj.Cost);
|
boblsturm@0
|
120 xlabel('Iteration');
|
boblsturm@0
|
121 ylabel('Cost');
|
boblsturm@0
|
122 title('Cost vs. Iteration');
|
boblsturm@0
|
123 grid on
|
boblsturm@0
|
124 end
|
boblsturm@0
|
125 end
|
boblsturm@0
|
126 end |