Mercurial > hg > smallbox
comparison toolboxes/AudioInpaintingToolbox/Problems/generateDeclippingProblem.m @ 144:19e0af570914 release_1.5
Merge from branch "ivand_dev"
| author | Ivan <ivan.damnjanovic@eecs.qmul.ac.uk> | 
|---|---|
| date | Tue, 26 Jul 2011 15:14:15 +0100 | 
| parents | 56d719a5fd31 | 
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| 143:8d866d96f006 | 144:19e0af570914 | 
|---|---|
| 1 function [problemData, solutionData] = generateDeclippingProblem(x,clippingLevel,GR) | |
| 2 % | |
| 3 % | |
| 4 % Usage: | |
| 5 % | |
| 6 % | |
| 7 % Inputs: | |
| 8 % - | |
| 9 % - | |
| 10 % - | |
| 11 % - | |
| 12 % - | |
| 13 % - | |
| 14 % - | |
| 15 % - | |
| 16 % | |
| 17 % Outputs: | |
| 18 % - | |
| 19 % - | |
| 20 % - | |
| 21 % - | |
| 22 % | |
| 23 % Note that the CVX library is needed. | |
| 24 % | |
| 25 % ------------------- | |
| 26 % | |
| 27 % Audio Inpainting toolbox | |
| 28 % Date: June 28, 2011 | |
| 29 % By Valentin Emiya, Amir Adler, Maria Jafari | |
| 30 % This code is distributed under the terms of the GNU Public License version 3 (http://www.gnu.org/licenses/gpl.txt). | |
| 31 % Generate a clipping problem: normalize and clip a signal. | |
| 32 % | |
| 33 % Usage: | |
| 34 % [problemData, solutionData] = makeClippedSignal(x,clippingLevel,GR) | |
| 35 % | |
| 36 % Inputs: | |
| 37 % - x: input signal (may be multichannel) | |
| 38 % - clippingLevel: clipping level, between 0 and 1 | |
| 39 % - GR (default: false): flag to generate an optional graphical display | |
| 40 % | |
| 41 % Outputs: | |
| 42 % - problemData.x: clipped signal | |
| 43 % - problemData.IMiss: boolean vector (same size as problemData.x) that indexes clipped | |
| 44 % samples | |
| 45 % - problemData.clipSizes: size of the clipped segments (not necessary | |
| 46 % for solving the problem) | |
| 47 % - solutionData.xClean: clean signal (input signal after normalization | |
| 48 % | |
| 49 % Note that the input signal is normalized to 0.9999 (-1 is not allowed in | |
| 50 % wav files) to provide problemData.x and solutionData.xClean. | |
| 51 | |
| 52 if nargin<3 || isempty(GR) | |
| 53 GR = false; | |
| 54 end | |
| 55 | |
| 56 %% Normalization | |
| 57 xMax = 0.9999; | |
| 58 solutionData.xClean = x/max(abs(x(:)))*xMax; | |
| 59 clippingLevel = clippingLevel*xMax; | |
| 60 | |
| 61 %% Clipping (hard threshold) | |
| 62 problemData.x = min(max(solutionData.xClean,-clippingLevel),clippingLevel); | |
| 63 problemData.IMiss = abs(problemData.x)>=clippingLevel; % related indices | |
| 64 | |
| 65 %% Size of the clipped segments | |
| 66 problemData.clipSizes = diff(problemData.IMiss); | |
| 67 if problemData.clipSizes(find(problemData.clipSizes,1,'first'))==-1,problemData.clipSizes = [1;problemData.clipSizes]; end | |
| 68 if problemData.clipSizes(find(problemData.clipSizes,1,'last'))==1,problemData.clipSizes = [problemData.clipSizes;-1]; end | |
| 69 problemData.clipSizes = diff(find(problemData.clipSizes)); | |
| 70 problemData.clipSizes = problemData.clipSizes(1:2:end); | |
| 71 | |
| 72 %% Optional graphical display | |
| 73 if GR | |
| 74 | |
| 75 % Plot histogram of the sizes of the clipped segments | |
| 76 if ~isempty(problemData.clipSizes) | |
| 77 figure | |
| 78 hist(problemData.clipSizes,1:max(problemData.clipSizes)) | |
| 79 title('Size of missing segments') | |
| 80 xlabel('Size'),ylabel('# of segments') | |
| 81 end | |
| 82 | |
| 83 t = (0:length(solutionData.xClean)-1); % time scale in samples | |
| 84 | |
| 85 % Plot original and clipped signals | |
| 86 figure | |
| 87 plot(t,solutionData.xClean,'',t,problemData.x,'') | |
| 88 legend('original','clipped') | |
| 89 | |
| 90 % Scatter plot between original and clipped signals | |
| 91 figure | |
| 92 plot(solutionData.xClean,problemData.x,'.') | |
| 93 xlabel('Original signal'),ylabel('Clipped signal') | |
| 94 | |
| 95 % Spectrograms | |
| 96 N = 512; | |
| 97 w = hann(N); | |
| 98 fs = 1; | |
| 99 NOverlap = round(.8*N); | |
| 100 nfft = 2^nextpow2(N)*2*2; | |
| 101 figure | |
| 102 subplot(3,3,[1,4]) | |
| 103 spectrogram(solutionData.xClean,w,NOverlap,nfft,fs,'yaxis') | |
| 104 title('Original') | |
| 105 xlim(t([1,end])) | |
| 106 cl = get(gca,'clim'); | |
| 107 set(gca,'clim',cl); | |
| 108 subplot(3,3,[1,4]+1) | |
| 109 spectrogram(problemData.x,w,NOverlap,nfft,fs,'yaxis') | |
| 110 title('Clipped') | |
| 111 set(gca,'clim',cl); | |
| 112 subplot(3,3,[1,4]+2) | |
| 113 spectrogram(solutionData.xClean-problemData.x,w,NOverlap,nfft,fs,'yaxis') | |
| 114 title('Error (=original-clipped)') | |
| 115 set(gca,'clim',cl); | |
| 116 subplot(3,3,7) | |
| 117 plot(t,solutionData.xClean,'');xlim(t([1,end])) | |
| 118 subplot(3,3,8) | |
| 119 plot(t,solutionData.xClean,'',t,problemData.x,'');xlim(t([1,end])) | |
| 120 subplot(3,3,9) | |
| 121 plot(t,solutionData.xClean-problemData.x,'');xlim(t([1,end])) | |
| 122 end | |
| 123 | |
| 124 return | 
