annotate Problems/generateAudioDeclippingProblem.m @ 136:1334d2302dd9 ivand_dev

Added Audio declipping problem (problem, reconstruct and example function)
author Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk>
date Thu, 14 Jul 2011 16:26:07 +0100
parents
children 9207d56c5547
rev   line source
ivan@136 1 function data = generateAudioDeclippingProblem(soundfile, clippingLevel, windowSize, overlap, wa, ws, wd, Dict_fun, redundancyFactor)
ivan@136 2 %% Generate Audio Declipping Problem
ivan@136 3 %
ivan@136 4 % CHANGE!!!generateAMT_Learning_Problem is a part of the SMALLbox and generates
ivan@136 5 % a problem that can be used for comparison of Dictionary Learning/Sparse
ivan@136 6 % Representation techniques in automatic music transcription scenario.
ivan@136 7 % The function prompts a user for an audio file (mid, wav, mat) reads it
ivan@136 8 % and generates a spectrogram given fft size (default nfft=4096), analysis
ivan@136 9 % window size (windowSize=2822), and analysis window overlap (overlap =
ivan@136 10 % 0.5).
ivan@136 11 %
ivan@136 12 % The output of the function is stucture with following fields:
ivan@136 13 % b - matrix with magnitudes of the spectrogram
ivan@136 14 % f - vector of frequencies at wihch spectrogram is computed
ivan@136 15 % windowSize - analysis window size
ivan@136 16 % overlap - analysis window overlap
ivan@136 17 % fs - sampling frequency
ivan@136 18 % m - number of frequenciy points in spectrogram
ivan@136 19 % n - number of time points in the spectrogram
ivan@136 20 % p - number of dictionary elements to be learned (eg 88 for piano)
ivan@136 21 % notesOriginal - notes of the original audio to be used for
ivan@136 22 % comparison (if midi of the original exists)
ivan@136 23 % name - name of the audio file to transcribe
ivan@136 24
ivan@136 25 % Centre for Digital Music, Queen Mary, University of London.
ivan@136 26 % This file copyright 2011 Ivan Damnjanovic.
ivan@136 27 %
ivan@136 28 % This program is free software; you can redistribute it and/or
ivan@136 29 % modify it under the terms of the GNU General Public License as
ivan@136 30 % published by the Free Software Foundation; either version 2 of the
ivan@136 31 % License, or (at your option) any later version. See the file
ivan@136 32 % COPYING included with this distribution for more information.
ivan@136 33 %
ivan@136 34 %%
ivan@136 35 FS=filesep;
ivan@136 36 TMPpath=pwd;
ivan@136 37
ivan@136 38 if ~ exist( 'soundfile', 'var' ) || isempty(soundfile)
ivan@136 39 %ask for file name
ivan@136 40 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
ivan@136 41 cd([pathstr1,FS,'data',FS,'audio']);
ivan@136 42 [filename,pathname] = uigetfile({'*.mat; *.mid; *.wav'},'Select a file to transcribe');
ivan@136 43 [pathstr, name, ext, versn] = fileparts(filename);
ivan@136 44 data.name=name;
ivan@136 45
ivan@136 46 if strcmp(ext,'.mid')
ivan@136 47 midi=readmidi(filename);
ivan@136 48 % data.notesOriginal=midiInfo(midi);
ivan@136 49 y=midi2audio(midi);
ivan@136 50 wavwrite(y, 44100, 16, 'temp.wav');
ivan@136 51 [x.signal, x.fs, x.nbits]=wavread('temp.wav');
ivan@136 52 delete('temp.wav');
ivan@136 53 elseif strcmp(ext,'.wav')
ivan@136 54 % cd([pathstr1,FS, 'data', FS, 'audio', FS, 'midi']);
ivan@136 55 % filename1=[name, '.mid'];
ivan@136 56 % if exist(filename1, 'file')
ivan@136 57 % midi=readmidi(filename1);
ivan@136 58 % data.notesOriginal=midiInfo(midi);
ivan@136 59 % end
ivan@136 60 cd([pathstr1,FS, 'data', FS, 'audio', FS, 'wav']);
ivan@136 61 [x.signal, x.fs, x.nbits]=wavread(filename);
ivan@136 62 else
ivan@136 63 % cd([pathstr1,FS, 'data', FS, 'audio', FS, 'midi']);
ivan@136 64 % filename1=[name, '.mid'];
ivan@136 65 % if exist(filename1, 'file')
ivan@136 66 % midi=readmidi(filename1);
ivan@136 67 % data.notesOriginal=midiInfo(midi);
ivan@136 68 % end
ivan@136 69 cd([pathstr1,FS, 'data', FS, 'audio', FS, 'mat']);
ivan@136 70 x=load([pathname,filename]);
ivan@136 71 end
ivan@136 72 else
ivan@136 73 [x.signal, x.fs, x.nbits]=wavread(soundfile);
ivan@136 74 [pathstr, name, ext, versn] = fileparts(soundfile);
ivan@136 75 data.name=name;
ivan@136 76 end
ivan@136 77
ivan@136 78 if ~ exist( 'clippingLevel', 'var' ) || isempty(clippingLevel), clippingLevel = 0.6; end
ivan@136 79 if ~ exist( 'windowSize', 'var' ) || isempty(windowSize), windowSize = 256; end
ivan@136 80 if ~ exist( 'overlap', 'var' ) || isempty(overlap), overlap = 0.5; end
ivan@136 81 if ~ exist( 'wa', 'var' ) || isempty(wa), wa = @wRect; end % Analysis window
ivan@136 82 if ~ exist( 'ws', 'var' ) || isempty(ws), ws = @wSine; end % Synthesis window
ivan@136 83 if ~ exist( 'wd', 'var' ) || isempty(wd), wd = @wRect; end % Weighting window for dictionary atoms
ivan@136 84
ivan@136 85 %% preparing signal
ivan@136 86
ivan@136 87 [problemData, solutionData] = generateDeclippingProblem(x.signal,clippingLevel);
ivan@136 88
ivan@136 89 x_clip = im2colstep(problemData.x,[windowSize 1],[overlap*windowSize 1]);
ivan@136 90 x_clip= diag(wa(windowSize)) * x_clip;
ivan@136 91 blkMask=im2colstep(double(~problemData.IMiss),[256 1],[128 1]);
ivan@136 92
ivan@136 93 %% Building dictionary
ivan@136 94 if ~exist( 'redundancyFactor', 'var' ) || isempty(redundancyFactor), redundancyFactor = 2; end % Weighting window for dictionary atoms
ivan@136 95 if exist('Dict_fun', 'var')&&~isempty(Dict_fun)
ivan@136 96 param=struct('N', windowSize, 'redundancyFactor', redundancyFactor, 'wd', wd);
ivan@136 97 data.B = Dict_fun(param);
ivan@136 98 end
ivan@136 99
ivan@136 100 data.b = x_clip;
ivan@136 101 data.M = blkMask;
ivan@136 102 data.original = solutionData.xClean;
ivan@136 103 data.clipped = problemData.x;
ivan@136 104 data.clipMask = problemData.IMiss;
ivan@136 105 data.clippingLevel = clippingLevel;
ivan@136 106 data.windowSize = windowSize;
ivan@136 107 data.overlap = overlap;
ivan@136 108 data.ws = ws;
ivan@136 109 data.wa = wa;
ivan@136 110 data.wd = wd;
ivan@136 111
ivan@136 112 data.fs = x.fs;
ivan@136 113 data.nbits = x.nbits;
ivan@136 114
ivan@136 115 [data.m, data.n] = size(x_clip);
ivan@136 116 data.p = windowSize*redundancyFactor; %number of dictionary elements
ivan@136 117
ivan@136 118 cd(TMPpath);
ivan@136 119
ivan@136 120 end