idamnjanovic@10: function data=generateAudioDenoiseProblem(au, trainnum, blocksize, dictsize, overlap, sigma, gain, maxval, initdict); idamnjanovic@21: %%% Audio Denoising Problem idamnjanovic@21: % idamnjanovic@21: % Centre for Digital Music, Queen Mary, University of London. idamnjanovic@21: % This file copyright 2010 Ivan Damnjanovic. idamnjanovic@21: % idamnjanovic@21: % This program is free software; you can redistribute it and/or idamnjanovic@21: % modify it under the terms of the GNU General Public License as idamnjanovic@21: % published by the Free Software Foundation; either version 2 of the idamnjanovic@21: % License, or (at your option) any later version. See the file idamnjanovic@21: % COPYING included with this distribution for more information. idamnjanovic@21: % idamnjanovic@10: % idamnjanovic@10: % idamnjanovic@10: % generateAudioDenoiseProblem is part of the SMALLbox and generate a idamnjanovic@10: % problem for comaprison of Dictionary Learning/Sparse Representation idamnjanovic@10: % techniques in audio denoising scenario. It is based on KSVD image idamnjanovic@10: % denoise demo by Ron Rubinstein (see bellow). idamnjanovic@10: % The fuction takes as an optional input idamnjanovic@10: % au - audio samples to be denoised idamnjanovic@10: % trainnum - number of frames for training idamnjanovic@10: % blocksize - 1D frame size (eg 512) idamnjanovic@10: % dictsize - number of atoms to be trained idamnjanovic@10: % overlap - ammount of overlaping frames between 0 and 1 idamnjanovic@21: % idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: disp(' '); idamnjanovic@10: disp(' ********** Denoising Problem **********'); idamnjanovic@10: disp(' '); idamnjanovic@10: disp(' This function reads an audio, adds random Gaussian noise,'); idamnjanovic@10: disp(' that can be later denoised by using dictionary learning techniques.'); idamnjanovic@10: disp(' '); idamnjanovic@10: idamnjanovic@10: FS=filesep; idamnjanovic@10: if ~ exist( 'sigma', 'var' ) || isempty(sigma), sigma = 26.74; end idamnjanovic@10: if ~ exist( 'gain', 'var' ) || isempty(gain), gain = 1.15; end idamnjanovic@10: idamnjanovic@10: if ~ exist( 'initdict', 'var' ) || isempty(initdict), initdict = 'odct'; end idamnjanovic@10: if ~ exist( 'overlap', 'var' ) || isempty(overlap), overlap = 15/16; end idamnjanovic@10: %% prompt user for wav file %% idamnjanovic@10: %ask for file name idamnjanovic@10: idamnjanovic@10: TMPpath=pwd; idamnjanovic@10: if ~ exist( 'au', 'var' ) || isempty(au) idamnjanovic@10: [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m')); idamnjanovic@10: cd([pathstr1,FS,'data',FS,'audio',FS,'wav']); idamnjanovic@10: [filename,pathname] = uigetfile({'*.wav;'},'Select a wav file'); idamnjanovic@10: [pathstr, name, ext, versn] = fileparts(filename); idamnjanovic@10: data.name=name; idamnjanovic@10: idamnjanovic@10: au = wavread(filename); idamnjanovic@10: au = mean(au,2); % turn it into mono. idamnjanovic@10: end; idamnjanovic@10: if ~ exist( 'maxval', 'var' ) || isempty(maxval), maxval = max(au); end idamnjanovic@10: idamnjanovic@10: %% generate noisy audio %% idamnjanovic@10: idamnjanovic@10: disp(' '); idamnjanovic@10: disp('Generating noisy audio...'); idamnjanovic@10: sigma = max(au)/10^(sigma/20); idamnjanovic@10: n = randn(size(au)) .* sigma; idamnjanovic@10: aunoise = au + n;% here we can load noise audio if available idamnjanovic@10: % for example: wavread('icassp06_x.wav');% idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: %% set parameters %% idamnjanovic@10: idamnjanovic@10: x = aunoise; idamnjanovic@10: if ~ exist( 'blocksize', 'var' ) || isempty(blocksize),blocksize = 512;end idamnjanovic@10: if ~ exist( 'dictsize', 'var' ) || isempty(dictsize), dictsize = 2048;end idamnjanovic@10: idamnjanovic@10: if ~ exist( 'trainnum', 'var' ) || isempty(trainnum),trainnum = (size(x,1)-blocksize+1);end idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: p=1; idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: % idamnjanovic@10: % msgdelta = 5; idamnjanovic@10: % idamnjanovic@10: % verbose = 't'; idamnjanovic@10: % if (msgdelta <= 0) idamnjanovic@10: % verbose=''; idamnjanovic@10: % msgdelta = -1; idamnjanovic@10: % end idamnjanovic@10: % idamnjanovic@10: % idamnjanovic@10: % % initial dictionary % idamnjanovic@10: % idamnjanovic@10: if (strcmpi(initdict,'odct')) idamnjanovic@10: initdict = odctndict(blocksize,dictsize,p); idamnjanovic@10: elseif (strcmpi(initdict,'data')) idamnjanovic@10: clear initdict; % causes initialization using random examples idamnjanovic@10: else idamnjanovic@10: error('Invalid initial dictionary specified.'); idamnjanovic@10: end idamnjanovic@10: idamnjanovic@10: if exist( 'initdict', 'var' ) idamnjanovic@10: initdict = initdict(:,1:dictsize); idamnjanovic@10: end idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: % noise mode % idamnjanovic@10: % if (isfield(params,'noisemode')) idamnjanovic@10: % switch lower(params.noisemode) idamnjanovic@10: % case 'psnr' idamnjanovic@10: % sigma = maxval / 10^(params.psnr/20); idamnjanovic@10: % case 'sigma' idamnjanovic@10: % sigma = params.sigma; idamnjanovic@10: % otherwise idamnjanovic@10: % error('Invalid noise mode specified'); idamnjanovic@10: % end idamnjanovic@10: % elseif (isfield(params,'sigma')) idamnjanovic@10: % sigma = params.sigma; idamnjanovic@10: % elseif (isfield(params,'psnr')) idamnjanovic@10: % sigma = maxval / 10^(params.psnr/20); idamnjanovic@10: % else idamnjanovic@10: % error('Noise strength not specified'); idamnjanovic@10: % end idamnjanovic@10: idamnjanovic@10: % params.Edata = sqrt(prod(blocksize)) * sigma * gain; % target error for omp idamnjanovic@10: % params.codemode = 'error'; idamnjanovic@10: % idamnjanovic@10: % params.sigma = sigma; idamnjanovic@10: % params.noisemode = 'sigma'; idamnjanovic@10: % idamnjanovic@10: % idamnjanovic@10: % % make sure test data is not present in params idamnjanovic@10: % if (isfield(params,'testdata')) idamnjanovic@10: % params = rmfield(params,'testdata'); idamnjanovic@10: % end idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: %%%% create training data %%% idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: X = buffer( x(1:trainnum),blocksize, overlap*blocksize); idamnjanovic@10: idamnjanovic@10: % remove dc in blocks to conserve memory % idamnjanovic@10: % bsize = 2000; idamnjanovic@10: % for i = 1:bsize:size(X,2) idamnjanovic@10: % blockids = i : min(i+bsize-1,size(X,2)); idamnjanovic@10: % X(:,blockids) = remove_dc(X(:,blockids),'columns'); idamnjanovic@10: % end idamnjanovic@10: data.Original = au; idamnjanovic@10: data.Noisy = aunoise; idamnjanovic@10: data.b = X; idamnjanovic@10: data.m = size(X,1); idamnjanovic@10: data.n = size(X,2); idamnjanovic@10: data.p = dictsize; idamnjanovic@10: data.blocksize=blocksize; idamnjanovic@10: data.sigma = sigma; idamnjanovic@10: data.gain = gain; idamnjanovic@10: data.maxval = maxval; idamnjanovic@10: data.initdict= initdict; idamnjanovic@10: data.signalDim=1; idamnjanovic@10: cd(TMPpath); idamnjanovic@10: