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