annotate toolboxes/AudioInpaintingToolbox/Utils/makeClippedSignal.m @ 234:c96880c0c47c

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