boblsturm@0: classdef CSS < handle boblsturm@0: properties boblsturm@0: NMF_features boblsturm@0: Activations boblsturm@0: Cost boblsturm@0: Synthesis_method boblsturm@0: Synthesis boblsturm@0: end boblsturm@0: boblsturm@0: methods boblsturm@0: function obj = CSS(varargin) boblsturm@0: if nargin == 2 boblsturm@0: obj.NMF_features = varargin{1}; boblsturm@0: obj.Synthesis_method = varargin{2}; boblsturm@0: end boblsturm@0: end boblsturm@0: end boblsturm@0: boblsturm@0: methods boblsturm@0: function obj = nmf(obj, corpus_sound, target_sound, varargin) boblsturm@0: if( nargin == 4 ) boblsturm@0: pct_prune = varargin{1} boblsturm@0: else boblsturm@0: pct_prune = 1 boblsturm@0: end boblsturm@0: nmf_alg = obj.NMF_features.Algorithm; boblsturm@0: target_spect = abs(target_sound.Features.STFT.S); boblsturm@0: corpus_spect = abs(corpus_sound.Features.STFT.S); boblsturm@0: [corpus_spect, pruned_frames, frames_to_keep] = prune_corpus( target_spect, corpus_spect, pct_prune ); boblsturm@0: boblsturm@0: switch nmf_alg boblsturm@0: case 'Euclidean' boblsturm@0: if length(fieldnames(obj.NMF_features)) > 1 boblsturm@0: [H, obj.Cost] = nmf_euclidean(target_spect, corpus_spect, obj.NMF_features); boblsturm@0: else boblsturm@0: [H, obj.Cost] = nmf_euclidean(target_spect, corpus_spect); boblsturm@0: end boblsturm@0: case 'Divergence' boblsturm@0: if length(fieldnames(obj.NMF_features)) > 1 boblsturm@0: [H, obj.Cost] = nmf_divergence(target_spect, corpus_spect, obj.NMF_features); boblsturm@0: else boblsturm@0: [H, obj.Cost] = nmf_divergence(target_spect, corpus_spect); boblsturm@0: end boblsturm@0: case 'Sparse NMF' boblsturm@0: if length(fieldnames(obj.NMF_features)) > 1 boblsturm@0: [~, H, deleted, obj.Cost] = SA_B_NMF(target_spect, corpus_spect, 5, obj.NMF_features); boblsturm@0: else boblsturm@0: [~, H, deleted, obj.Cost] = SA_B_NMF(target_spect, corpus_spect, 5 ); boblsturm@0: end boblsturm@0: boblsturm@0: H( deleted, : ) = 0; boblsturm@0: obj.Activations = H; boblsturm@0: end boblsturm@0: boblsturm@0: if size( frames_to_keep ) > 0 boblsturm@0: tmp = zeros( size( corpus_sound.Features.STFT.S,2 ), size( target_spect,2 ) ); boblsturm@0: for i = 1:length( frames_to_keep ) boblsturm@0: tmp(frames_to_keep(i), :) = H(i,:); boblsturm@0: end boblsturm@0: H = tmp; boblsturm@0: end boblsturm@0: % H( pruned_frames, : ) = 0; boblsturm@0: % % Pad activations to size of corpus frames boblsturm@0: % % since pruned frames maximum can be < size of corpus boblsturm@0: % H( setdiff( 1:( size( corpus_spect, 2 ) + length( pruned_frames ) ), 1:size( H, 1 ) ), : ) = 0; boblsturm@0: obj.Activations = H; boblsturm@0: end boblsturm@0: boblsturm@0: function obj = synthesize(obj, corpus_sound) boblsturm@0: synth_method = obj.Synthesis_method; boblsturm@0: win = corpus_sound.Features.window; boblsturm@0: W = abs(corpus_sound.Features.STFT.S); boblsturm@0: H = obj.Activations; boblsturm@0: boblsturm@0: switch synth_method boblsturm@0: case 'ISTFT' boblsturm@0: parameters = []; boblsturm@0: parameters.synHop = win.Hop; boblsturm@0: parameters.win = window(lower(win.Type), win.Length); boblsturm@0: boblsturm@0: reconstruction = W*H; boblsturm@0: padding = size(reconstruction, 1)*2 - win.Length - 2; boblsturm@0: if padding >= 0 boblsturm@0: parameters.zeroPad = padding; boblsturm@0: end boblsturm@0: boblsturm@0: obj.Synthesis = istft(reconstruction, parameters); boblsturm@0: case 'Template Addition' boblsturm@0: obj.Synthesis = templateAdditionResynth(corpus_sound.Signal, H, win); boblsturm@0: end boblsturm@0: end boblsturm@0: boblsturm@0: function plot_activations(obj, varargin) boblsturm@0: if(nargin > 1) boblsturm@0: maxDb = varargin{1}; boblsturm@0: else boblsturm@0: maxDb = -45; boblsturm@0: end boblsturm@0: boblsturm@0: H = obj.Activations; boblsturm@0: boblsturm@0: HdB = 20*log10(H./max(max(H))); boblsturm@0: HdB = HdB - maxDb; boblsturm@0: HdB(HdB < 0) = 0; boblsturm@0: imagesc(HdB); boblsturm@0: cmap = colormap('gray'); boblsturm@0: cmap(1,:) = 0*ones(1,3); boblsturm@0: colormap(flipud(cmap)) boblsturm@0: colorbar boblsturm@0: axis xy; grid on; boblsturm@0: set(gca, 'Layer', 'top'); boblsturm@0: ylabel('Template'); boblsturm@0: xlabel('Time'); boblsturm@0: grid on; boblsturm@0: set(gca,'FontSize',16); boblsturm@0: end boblsturm@0: boblsturm@0: function plot_cost(obj) boblsturm@0: plot(obj.Cost); boblsturm@0: xlabel('Iteration'); boblsturm@0: ylabel('Cost'); boblsturm@0: title('Cost vs. Iteration'); boblsturm@0: grid on boblsturm@0: end boblsturm@0: end boblsturm@0: end