annotate Problems/generateAudioDenoiseProblem.m @ 247:ecce33192fcc tip

Added tag ver_2.1 for changeset cef4500b936f
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Wed, 31 Oct 2012 12:24:44 +0000
parents 9c418bea7f6a
children
rev   line source
ivan@161 1 function data = generateAudioDenoiseProblem(soundfile, sigma, windowSize,...
ivan@161 2 overlap, wa, ws, trainnum, redundancyFactor, initdict)
ivan@161 3 %% Audio Denoising Problem
idamnjanovic@10 4 %
idamnjanovic@10 5 % generateAudioDenoiseProblem is part of the SMALLbox and generate a
idamnjanovic@10 6 % problem for comaprison of Dictionary Learning/Sparse Representation
ivan@161 7 % techniques in audio denoising scenario.
ivan@161 8 %
ivan@161 9 % The function takes as an optional input
ivan@161 10 % soundfile - name of the file
ivan@161 11 % sigma - noise level (dB)
ivan@161 12 % windowSize - 1D frame size (eg 512)
ivan@161 13 % overlap - ammount of overlaping frames between 0 and 1
ivan@161 14 % wa,ws - analisys and synthesis window functions
ivan@161 15 %
ivan@161 16 % trainnum - number of frames for training
ivan@161 17 % redundancyFactor - overcompletness of dictionary (default 2)
ivan@161 18 % initdict - initial dictionary
idamnjanovic@21 19 %
ivan@161 20 % The function outputs the structure with following fields:
ivan@161 21 % Original - original signal
ivan@161 22 % Noisy - signal with added noise
ivan@161 23 % fs - sample rate of the original signal in Hertz
ivan@161 24 % nbits - the number of bits per sample
ivan@161 25 % sigma - added noise level
ivan@161 26 % b - matrix of training samples for dictionary learning
ivan@161 27 % b1 - matrix containing all frames for reconstruction step
ivan@161 28 % m - size od dictionary atom
ivan@161 29 % n - number of frames for training
ivan@161 30 % p - number of atoms in dictionary
ivan@161 31 % windowSize - 1D frame size (eg 512)
ivan@161 32 % overlap - ammount of overlaping frames between 0 and 1
ivan@161 33 % wa,ws - analisys and synthesis window functions
ivan@161 34 % initdict - initial dictionary
idamnjanovic@10 35
ivan@128 36 % Centre for Digital Music, Queen Mary, University of London.
ivan@161 37 % This file copyright 2011 Ivan Damnjanovic.
ivan@128 38 %
ivan@128 39 % This program is free software; you can redistribute it and/or
ivan@128 40 % modify it under the terms of the GNU General Public License as
ivan@128 41 % published by the Free Software Foundation; either version 2 of the
ivan@128 42 % License, or (at your option) any later version. See the file
ivan@128 43 % COPYING included with this distribution for more information.
ivan@128 44 %%
idamnjanovic@10 45 disp(' ');
idamnjanovic@10 46 disp(' ********** Denoising Problem **********');
idamnjanovic@10 47 disp(' ');
idamnjanovic@10 48 disp(' This function reads an audio, adds random Gaussian noise,');
idamnjanovic@10 49 disp(' that can be later denoised by using dictionary learning techniques.');
idamnjanovic@10 50 disp(' ');
idamnjanovic@10 51
idamnjanovic@10 52 FS=filesep;
idamnjanovic@10 53
idamnjanovic@10 54 %% prompt user for wav file %%
idamnjanovic@10 55 %ask for file name
idamnjanovic@10 56
idamnjanovic@10 57 TMPpath=pwd;
ivan@161 58 if ~ exist( 'soundfile', 'var' ) || isempty(soundfile)
ivan@161 59 %ask for file name
luis@186 60 [pathstr1, name, ext] = fileparts(which('SMALLboxSetup.m'));
ivan@161 61 cd([pathstr1,FS,'data',FS,'audio']);
ivan@161 62 [filename,pathname] = uigetfile({'*.mat; *.mid; *.wav'},'Select a file to transcribe');
luis@186 63 [pathstr, name, ext] = fileparts(filename);
idamnjanovic@10 64 data.name=name;
idamnjanovic@10 65
ivan@161 66 if strcmp(ext,'.mid')
ivan@161 67 midi=readmidi(filename);
ivan@161 68 % data.notesOriginal=midiInfo(midi);
ivan@161 69 y=midi2audio(midi);
ivan@161 70 wavwrite(y, 44100, 16, 'temp.wav');
ivan@161 71 [x.signal, x.fs, x.nbits]=wavread('temp.wav');
ivan@161 72 delete('temp.wav');
ivan@161 73 elseif strcmp(ext,'.wav')
ivan@161 74 % cd([pathstr1,FS, 'data', FS, 'audio', FS, 'midi']);
ivan@161 75 % filename1=[name, '.mid'];
ivan@161 76 % if exist(filename1, 'file')
ivan@161 77 % midi=readmidi(filename1);
ivan@161 78 % data.notesOriginal=midiInfo(midi);
ivan@161 79 % end
ivan@161 80 cd([pathstr1,FS, 'data', FS, 'audio', FS, 'wav']);
ivan@161 81 [x.signal, x.fs, x.nbits]=wavread(filename);
ivan@161 82 else
ivan@161 83 % cd([pathstr1,FS, 'data', FS, 'audio', FS, 'midi']);
ivan@161 84 % filename1=[name, '.mid'];
ivan@161 85 % if exist(filename1, 'file')
ivan@161 86 % midi=readmidi(filename1);
ivan@161 87 % data.notesOriginal=midiInfo(midi);
ivan@161 88 % end
ivan@161 89 cd([pathstr1,FS, 'data', FS, 'audio', FS, 'mat']);
ivan@161 90 x=load([pathname,filename]);
ivan@161 91 end
ivan@161 92 else
ivan@161 93 [x.signal, x.fs, x.nbits]=wavread(soundfile);
luis@186 94 [pathstr, name, ext] = fileparts(soundfile);
ivan@161 95 data.name=name;
ivan@161 96 end
idamnjanovic@10 97
idamnjanovic@10 98 %% set parameters %%
ivan@161 99 if ~ exist( 'sigma', 'var' ) || isempty(sigma), sigma = 0.2; end
idamnjanovic@10 100
ivan@161 101 if ~ exist( 'windowSize', 'var' ) || isempty(windowSize), windowSize = 256;end
ivan@161 102 if ~ exist( 'overlap', 'var' ) || isempty(overlap), overlap = 0.5; end
ivan@161 103 if ~ exist( 'wa', 'var' ) || isempty(wa), wa = @wSine; end % Analysis window
ivan@161 104 if ~ exist( 'ws', 'var' ) || isempty(ws), ws = @wSine; end % Synthesis window
idamnjanovic@10 105
idamnjanovic@10 106
ivan@161 107 if ~ exist( 'redundancyFactor', 'var' ) || isempty(windowSize),...
ivan@161 108 redundancyFactor = 2;end
ivan@161 109 if ~ exist( 'initdict', 'var' ) || isempty(initdict),...
ivan@161 110 initdict = 'odct'; end
ivan@161 111 if ~ exist( 'trainnum', 'var' ) || isempty(trainnum), ...
ivan@161 112 trainnum = 16*redundancyFactor*windowSize;end
idamnjanovic@10 113
idamnjanovic@10 114 if (strcmpi(initdict,'odct'))
ivan@161 115 initdict = odctndict(windowSize, redundancyFactor*windowSize, 1);
idamnjanovic@10 116 elseif (strcmpi(initdict,'data'))
idamnjanovic@10 117 clear initdict; % causes initialization using random examples
idamnjanovic@10 118 else
idamnjanovic@10 119 error('Invalid initial dictionary specified.');
idamnjanovic@10 120 end
idamnjanovic@10 121
idamnjanovic@10 122 if exist( 'initdict', 'var' )
ivan@161 123 initdict = initdict(:,1:redundancyFactor*windowSize);
idamnjanovic@10 124 end
idamnjanovic@10 125
idamnjanovic@10 126
idamnjanovic@10 127 %%%% create training data %%%
idamnjanovic@10 128
ivan@161 129 %% generate noisy audio %%
idamnjanovic@10 130
ivan@161 131 disp(' ');
ivan@161 132 disp('Generating noisy audio...');
ivan@161 133 x.signal = x.signal/max(abs(x.signal(:)))*0.99999;
ivan@161 134 n = randn(size(x.signal)) .* sigma;
ivan@161 135
ivan@161 136 xnoise = x.signal + n;% here we can load noise audio if available
ivan@161 137 % for example: wavread('icassp06_x.wav');%
ivan@161 138
ivan@161 139
ivan@161 140
ivan@161 141
ivan@161 142 X = im2colstep(xnoise,[windowSize 1],[overlap*windowSize 1]);
ivan@161 143 X = diag(wa(windowSize)) * X;
ivan@161 144
ivan@161 145
ivan@161 146
ivan@161 147
idamnjanovic@10 148
idamnjanovic@10 149 % remove dc in blocks to conserve memory %
idamnjanovic@10 150 % bsize = 2000;
idamnjanovic@10 151 % for i = 1:bsize:size(X,2)
idamnjanovic@10 152 % blockids = i : min(i+bsize-1,size(X,2));
idamnjanovic@10 153 % X(:,blockids) = remove_dc(X(:,blockids),'columns');
idamnjanovic@10 154 % end
ivan@161 155 data.Original = x.signal;
ivan@161 156 data.Noisy = xnoise;
ivan@161 157 data.fs = x.fs;
ivan@161 158 data.nbits = x.nbits;
ivan@161 159
idamnjanovic@10 160 data.sigma = sigma;
ivan@161 161
ivan@161 162
ivan@161 163 if (trainnum<size(X,2))
ivan@161 164 p = randperm(size(X,2));
ivan@161 165 p=sort(p(1:trainnum));
ivan@161 166 data.b = X(:,p);
ivan@161 167 else
ivan@161 168 data.b = X;
ivan@161 169 end
ivan@161 170
ivan@161 171 data.b1 = X;
ivan@161 172 [data.m, data.n] = size(data.b);
ivan@161 173 data.p = redundancyFactor*windowSize;
ivan@161 174
ivan@161 175 data.windowSize = windowSize;
ivan@161 176 data.overlap = overlap;
ivan@161 177 data.ws = ws;
ivan@161 178 data.wa = wa;
ivan@161 179
idamnjanovic@10 180 data.initdict= initdict;
ivan@161 181
idamnjanovic@10 182 cd(TMPpath);
idamnjanovic@10 183