annotate toolboxes/AudioInpaintingToolbox/Problems/generateDeclippingProblem.m @ 229:ae71e160f80c luisf_dev

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