Mercurial > hg > smallbox
comparison toolboxes/AudioInpaintingToolbox/Solvers/inpaintSignal_IndependentProcessingOfFrames.m @ 138:56d719a5fd31 ivand_dev
Audio Inpaintin Toolbox
author | Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk> |
---|---|
date | Thu, 21 Jul 2011 14:27:47 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
137:9207d56c5547 | 138:56d719a5fd31 |
---|---|
1 function [ReconstSignal1 ReconstSignal2] = inpaintSignal_IndependentProcessingOfFrames(problemData,param) | |
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 % ======================================================== | |
32 % Perform Audio De-clipping with overlapping blocks | |
33 % Synthesis Approach, union of overcomplete DCT dictionary | |
34 % Date: 14 Apr. 2010 | |
35 % Inputs: | |
36 % - x: Clipped signal | |
37 % - ClipMask: Indices of clipped samples | |
38 % - Optional parameters [and default values]: | |
39 % - param.N: frame length [256] | |
40 % - param.frameOverlapFactor: overlap factor between frames [2] | |
41 % - param.wa: weighting analysis window [@sineWin] | |
42 % - param.ws: weighting synthesis window [@sineWin] | |
43 % - param.OMPerr: error threshold to stop OMP iterations [0.001] | |
44 % - param.sparsityDegree: max number of non-zero components to | |
45 % stop OMP iterations [param.N/4]; | |
46 % - other fields: see the documentation of UDCT_Dictionary | |
47 % | |
48 % Outputs: | |
49 % ReconstSignal1 - reconstructed signal (all samples generated | |
50 % from the synthesis model) | |
51 % ReconstSignal2 - reconstructed signal (only clipped samples are generated | |
52 % from the synthesis model) | |
53 % | |
54 % By Valentin Emiya - SMALL Project, 2010 | |
55 % | |
56 % ======================================================== | |
57 | |
58 % Check parameters | |
59 defaultParam.N = 256; | |
60 defaultParam.OLA_frameOverlapFactor = 4; | |
61 defaultParam.wa = @wSine; | |
62 defaultParam.OLA_ws = @wSine; | |
63 defaultParam.OLA_par_waitingTime_mainProcess = 0.2; | |
64 defaultParam.OLA_par_waitingTime_thread = 0.2; | |
65 defaultParam.OLA_par_frameBlockSize = 1; | |
66 defaultParam.TCPIP_port = 3000; | |
67 defaultParam.COM_DISP = false; | |
68 defaultParam.STATE_DISP = false; | |
69 | |
70 if nargin<2 | |
71 param = defaultParam; | |
72 else | |
73 names = fieldnames(defaultParam); | |
74 for k=1:length(names) | |
75 if ~isfield(param,names{k}) || isempty(param.(names{k})) | |
76 param.(names{k}) = defaultParam.(names{k}); | |
77 end | |
78 end | |
79 end | |
80 | |
81 x = problemData.x; | |
82 ClipMask = find(problemData.IMiss); | |
83 | |
84 % According to this flag, switch between a parallel multithread processing | |
85 % and a singlethread processing. This can fasten the computations but does | |
86 % not affect the results. | |
87 if param.MULTITHREAD_FRAME_PROCESSING | |
88 [ReconstSignal1 ReconstSignal2] = multithreadProcessing(x,ClipMask,param); | |
89 else | |
90 [ReconstSignal1 ReconstSignal2] = singlethreadProcessing(x,ClipMask,param); | |
91 end | |
92 | |
93 return; | |
94 | |
95 function [ReconstSignal1 ReconstSignal2] = singlethreadProcessing(x,ClipMask,param) | |
96 % ======================================================== | |
97 % Overlap-and-add processing of a signal with missing samples. | |
98 % Decomposition into overlapping frames, processing of each | |
99 % frame independently and OLA reconstruction. | |
100 % Date: 01 Jun. 2010 | |
101 % Inputs: | |
102 % - x: Clipped signal | |
103 % - ClipMask: Indices of clipped samples | |
104 % - Optional parameters [and default values]: | |
105 % - param.N: frame length [256] | |
106 % - param.inpaintFrame: function handle for inpainting a frame | |
107 % [@inpaintFrame_OMP] | |
108 % - param.OLA_frameOverlapFactor: overlap factor between frames [2] | |
109 % - param.wa: weighting analysis window [@sineWin] | |
110 % - param.OLA_ws: weighting synthesis window [@sineWin] | |
111 % - param.SKIP_CLEAN_FRAMES: flag to skip frames with no | |
112 % missing samples [true] | |
113 % - other fields: see the documentation the inpainting method | |
114 % | |
115 % Outputs: | |
116 % ReconstSignal1 - reconstructed signal (all samples generated | |
117 % from the synthesis model) | |
118 % ReconstSignal2 - reconstructed signal (only clipped samples are generated | |
119 % from the synthesis model) | |
120 % | |
121 % By Amir Adler, Maria Jafari, Valentin Emiya - SMALL Project, 2010 | |
122 % | |
123 % ======================================================== | |
124 | |
125 % Check parameters | |
126 defaultParam.N = 256; | |
127 defaultParam.inpaintFrame = @inpaintFrame_OMP; | |
128 defaultParam.OLA_frameOverlapFactor = 2; | |
129 defaultParam.wa = @wSine; | |
130 defaultParam.ws = @wSine; | |
131 defaultParam.SKIP_CLEAN_FRAMES = true; | |
132 | |
133 if nargin<3 | |
134 param = defaultParam; | |
135 else | |
136 names = fieldnames(defaultParam); | |
137 for k=1:length(names) | |
138 if ~isfield(param,names{k}) || isempty(param.(names{k})) | |
139 param.(names{k}) = defaultParam.(names{k}); | |
140 end | |
141 end | |
142 end | |
143 % if ~isfield(param,'D') | |
144 % param.D = param.D_fun(param); | |
145 % end | |
146 | |
147 bb=param.N; % block size | |
148 | |
149 % modify signal length to accommodate integer number of blocks | |
150 L=floor(length(x)/bb)*bb; | |
151 x=x(1:L); | |
152 ClipMask(ClipMask>L) = []; | |
153 | |
154 % Extracting the signal blocks with 50% overlap | |
155 Ibegin = (1:bb/param.OLA_frameOverlapFactor:length(x)-bb); | |
156 if Ibegin(end)~=L-bb+1 | |
157 Ibegin(end+1) = L-bb+1; | |
158 end | |
159 Iblk = ones(bb,1)*Ibegin+(0:bb-1).'*ones(size(Ibegin)); | |
160 wa = param.wa(bb); % analysis window | |
161 xFrames=diag(wa)*x(Iblk); | |
162 | |
163 % Generating the block mask | |
164 Mask=ones(size(x)); | |
165 Mask(ClipMask)=0; | |
166 blkMask=Mask(Iblk); | |
167 | |
168 % Declipping the Patches | |
169 [n,P]=size(xFrames); | |
170 | |
171 if ~isdeployed | |
172 h=waitbar(0,'Processing each frame...'); | |
173 end | |
174 Reconst = zeros(n,P); | |
175 co = zeros(512,P); | |
176 for k=1:1:P, | |
177 if ~isdeployed | |
178 waitbar(k/P); | |
179 end | |
180 if param.SKIP_CLEAN_FRAMES && all(blkMask(:,k)) | |
181 continue | |
182 end | |
183 frameProblemData.x = xFrames(:,k); | |
184 frameProblemData.IMiss = ~blkMask(:,k); | |
185 | |
186 [Reconst(:,k)]= ... | |
187 param.inpaintFrame(frameProblemData,param); | |
188 | |
189 end; | |
190 if ~isdeployed | |
191 close(h); | |
192 end | |
193 | |
194 % Overlap and add | |
195 | |
196 % The completly reconstructed signal | |
197 ReconstSignal1 = zeros(size(x)); | |
198 ws = param.OLA_ws(bb); % synthesis window | |
199 wNorm = zeros(size(ReconstSignal1)); | |
200 for k=1:size(Iblk,2) | |
201 ReconstSignal1(Iblk(:,k)) = ReconstSignal1(Iblk(:,k)) + Reconst(:,k).*ws(:); | |
202 wNorm(Iblk(:,k)) = wNorm(Iblk(:,k)) + ws(:).*wa(:); | |
203 end | |
204 ReconstSignal1 = ReconstSignal1./wNorm; | |
205 | |
206 % Only replace the clipped samples with the reconstructed ones | |
207 ReconstSignal2=x; | |
208 ReconstSignal2(ClipMask)=ReconstSignal1(ClipMask); | |
209 | |
210 return; | |
211 | |
212 function [ReconstSignal1 ReconstSignal2] = multithreadProcessing(x,ClipMask,param) | |
213 % Send parameters to the threads | |
214 % initParamFilename = [param.OLA_par_threadDir 'par_param.mat']; | |
215 % save(initParamFilename,'param'); | |
216 | |
217 bb=param.N; % block size | |
218 | |
219 % modify signal length to accommodate integer number of blocks | |
220 L=floor(length(x)/bb)*bb; | |
221 x=x(1:L); | |
222 ClipMask(ClipMask>L) = []; | |
223 | |
224 % Extracting the signal blocks with 50% overlap | |
225 Ibegin = (1:round(bb/param.OLA_frameOverlapFactor):length(x)-bb); | |
226 if Ibegin(end)~=L-bb+1 | |
227 Ibegin(end+1) = L-bb+1; | |
228 end | |
229 Iblk = ones(bb,1)*Ibegin+(0:bb-1).'*ones(size(Ibegin)); | |
230 wa = param.wa(bb); % analysis window | |
231 xFrames=diag(wa)*x(Iblk); | |
232 | |
233 % Generating the block mask | |
234 Mask=ones(size(x)); | |
235 Mask(ClipMask)=0; | |
236 blkMask=Mask(Iblk); | |
237 | |
238 % Declipping the Patches | |
239 if ~isdeployed && false | |
240 h=waitbar(0,'Processing each frame...'); | |
241 end | |
242 [n,P]=size(xFrames); | |
243 Reconst = NaN(n,P); | |
244 % initializedThreads = []; | |
245 | |
246 % find the block of frames to process | |
247 k_lists = {}; | |
248 kTrame = 1; | |
249 while kTrame<=P | |
250 k_list = zeros(param.OLA_par_frameBlockSize,1); | |
251 ind = 0; | |
252 while ind<param.OLA_par_frameBlockSize && kTrame<=P | |
253 if param.SKIP_CLEAN_FRAMES && all(blkMask(:,kTrame)) | |
254 kTrame=kTrame+1; | |
255 continue | |
256 end | |
257 ind = ind+1; | |
258 k_list(ind) = kTrame; | |
259 kTrame=kTrame+1; | |
260 end | |
261 if ind==0 | |
262 break; | |
263 end | |
264 k_lists{end+1} = k_list(1:ind); | |
265 end | |
266 k_list_all = cell2mat(k_lists'); | |
267 | |
268 % Create a server | |
269 serverSocket = createServer(param); | |
270 % Definition of the client states | |
271 stateDef; | |
272 | |
273 param.COM_DISP = false; | |
274 | |
275 kBlock=1; | |
276 initializedClientIDs = []; | |
277 while any(isnan(Reconst(1,k_list_all))) | |
278 if ~isdeployed && false | |
279 waitbar(sum(~isnan(Reconst(1,k_list_all)))/length(k_list_all)); | |
280 end | |
281 | |
282 % Wait for a new client | |
283 currentClient = waitClient(serverSocket); | |
284 | |
285 % Receive client state | |
286 clientIDState = readData(currentClient,param.COM_DISP); | |
287 clientID = clientIDState(1); | |
288 clientState = clientIDState(2); | |
289 switch clientState | |
290 case INIT | |
291 if param.STATE_DISP | |
292 fprintf('INIT %d\n',clientID); | |
293 end | |
294 if 0 | |
295 sendData(currentClient,initParamFilename,param.COM_DISP); | |
296 else | |
297 sendData(currentClient,param,param.COM_DISP); | |
298 end | |
299 initializedClientIDs(end+1) = clientID; | |
300 case FREE | |
301 if ~ismember(clientID,initializedClientIDs) | |
302 sendData(currentClient,INIT_ORDER,param.COM_DISP); % INIT | |
303 elseif kBlock<=length(k_lists) | |
304 k_list = k_lists{kBlock}; | |
305 if param.STATE_DISP | |
306 fprintf('TO PROCESS %d:',clientID); | |
307 arrayfun(@(x)fprintf(' %d',x),k_list); | |
308 fprintf('\n'); | |
309 end | |
310 sendData(currentClient,k_list,param.COM_DISP); | |
311 sendData(currentClient,xFrames(:,k_list),param.COM_DISP); | |
312 sendData(currentClient,find(blkMask(:,k_list)),param.COM_DISP); | |
313 kBlock = kBlock+1; | |
314 else | |
315 if param.STATE_DISP | |
316 fprintf('WAIT %d\n',clientID); | |
317 end | |
318 sendData(currentClient,WAIT_ORDER,param.COM_DISP); % no data to process | |
319 end | |
320 case PROCESSED | |
321 processed_k_list = readData(currentClient,param.COM_DISP); | |
322 y = readData(currentClient,param.COM_DISP); | |
323 y = reshape(y,[],length(processed_k_list)); | |
324 if param.STATE_DISP | |
325 fprintf('PROCESSED %d:',clientID); | |
326 arrayfun(@(x)fprintf(' %d',x),processed_k_list); | |
327 fprintf('\n'); | |
328 end | |
329 if ~isempty(processed_k_list) | |
330 Reconst(:,processed_k_list) = y; | |
331 end | |
332 otherwise | |
333 error('switch:UndefinedClientState','Undefined client state'); | |
334 end | |
335 | |
336 closeClientConnection(currentClient); | |
337 end; | |
338 | |
339 % Close the server | |
340 closeServer(serverSocket); | |
341 | |
342 if ~isdeployed && false | |
343 close(h); | |
344 end | |
345 | |
346 % Overlap and add | |
347 | |
348 % The completly reconstructed signal | |
349 ReconstSignal1 = zeros(size(x)); | |
350 ws = param.OLA_ws(bb); % synthesis window | |
351 wNorm = zeros(size(ReconstSignal1)); | |
352 for k=1:size(Iblk,2) | |
353 ReconstSignal1(Iblk(:,k)) = ReconstSignal1(Iblk(:,k)) + Reconst(:,k).*ws(:); | |
354 wNorm(Iblk(:,k)) = wNorm(Iblk(:,k)) + ws(:).*wa(:); | |
355 end | |
356 ReconstSignal1 = ReconstSignal1./wNorm; | |
357 | |
358 % Only replace the clipped samples with the reconstructed ones | |
359 ReconstSignal2=x; | |
360 ReconstSignal2(ClipMask)=ReconstSignal1(ClipMask); | |
361 | |
362 killClients(param); | |
363 | |
364 return; | |
365 | |
366 % ======================================================== | |
367 % ======================================================== | |
368 | |
369 | |
370 | |
371 % ======================================================== | |
372 % ======================================================== |