aboutsummaryrefslogtreecommitdiff
path: root/src/modules/audio_processing/aecm/main/matlab/compsup.m
blob: 9575ec40fce8975532f1cfb8242868ca10e84073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
function [emicrophone,aaa]=compsup(microphone,TheFarEnd,avtime,samplingfreq);
% microphone = microphone signal
% aaa = nonlinearity input variable
% TheFarEnd = far end signal
% avtime = interval to compute suppression from (seconds)
% samplingfreq = sampling frequency

%if(nargin==6)
%    fprintf(1,'suppress has received a delay sequence\n');
%end


Ap500=[  1.00, -4.95, 9.801, -9.70299, 4.80298005, -0.9509900499];
Bp500=[  0.662743088639636, -2.5841655608125, 3.77668102146288, -2.45182477425154, 0.596566274575251, 0.0];


Ap200=[ 1.00, -4.875, 9.50625, -9.26859375, 4.518439453125, -0.881095693359375];
Bp200=[ 0.862545460994275, -3.2832804496114, 4.67892032308828, -2.95798023879133, 0.699796870041299, 0.0];

maxDelay=0.4; %[s]
histLen=1; %[s]


% CONSTANTS THAT YOU CAN EXPERIMENT WITH
A_GAIN=10.0;	 	% for the suppress case
oversampling = 2;	% must be power of 2; minimum is 2; 4 works
% fine for support=64, but for support=128,
% 8 gives better results.
support=64; %512	% fft support (frequency resolution; at low
% settings you can hear more distortion
% (e.g. pitch that is left-over from far-end))
% 128 works well, 64 is ok)

lowlevel = mean(abs(microphone))*0.0001;

G_ol = 0;  % Use overlapping sets of estimates

% ECHO SUPPRESSION SPECIFIC PARAMETERS
suppress_overdrive=1.0;   % overdrive factor for suppression 1.4 is good
gamma_echo=1.0;           % same as suppress_overdrive but at different place
de_echo_bound=0.0;
mLim=10;                  % rank of matrix G
%limBW = 1;		  % use bandwidth-limited response for G
if mLim > (support/2+1)
    error('mLim in suppress.m too large\n');
end


dynrange=1.0000e-004;

% other, constants
hsupport = support/2;
hsupport1 = hsupport+1;
factor =  2 / oversampling;
updatel = support/oversampling;
win=sqrt(designwindow(0,support));
estLen = round(avtime * samplingfreq/updatel)

runningfmean =0.0;

mLim = floor(hsupport1/2);
V = sqrt(2/hsupport1)*cos(pi/hsupport1*(repmat((0:hsupport1-1) + 0.5, mLim, 1).* ...
    repmat((0:mLim-1)' + 0.5, 1, hsupport1)));

fprintf(1,'updatel is %5.3f s\n', updatel/samplingfreq);



bandfirst=8; bandlast=25;
dosmooth=0;  % to get rid of wavy bin counts (can be worse or better)

% compute some constants
blockLen = support/oversampling;
maxDelayb = floor(samplingfreq*maxDelay/updatel); % in blocks
histLenb = floor(samplingfreq*histLen/updatel); % in blocks

x0=TheFarEnd;
y0=microphone;


%input
tlength=min([length(microphone),length(TheFarEnd)]);
updateno=floor(tlength/updatel);
tlength=updatel*updateno;
updateno = updateno - oversampling + 1;

TheFarEnd =TheFarEnd(1:tlength);
microphone =microphone(1:tlength);

TheFarEnd =[zeros(hsupport,1);TheFarEnd(1:tlength)];
microphone =[zeros(hsupport,1);microphone(1:tlength)];


% signal length
n = min([floor(length(x0)/support)*support,floor(length(y0)/support)*support]);
nb = n/blockLen - oversampling + 1; % in blocks

% initialize space
win = sqrt([0 ; hanning(support-1)]);
sxAll2 = zeros(hsupport1,nb);
syAll2 = zeros(hsupport1,nb);

z500=zeros(5,maxDelayb+1);
z200=zeros(5,hsupport1);

bxspectrum=uint32(zeros(nb,1));
bxhist=uint32(zeros(maxDelayb+1,1));
byspectrum=uint32(zeros(nb,1));
bcount=zeros(1+maxDelayb,nb);
fcount=zeros(1+maxDelayb,nb);
fout=zeros(1+maxDelayb,nb);
delay=zeros(nb,1);
tdelay=zeros(nb,1);
nlgains=zeros(nb,1);

% create space (mainly for debugging)
emicrophone=zeros(tlength,1);
femicrophone=complex(zeros(hsupport1,updateno));
thefilter=zeros(hsupport1,updateno);
thelimiter=ones(hsupport1,updateno);
fTheFarEnd=complex(zeros(hsupport1,updateno));
afTheFarEnd=zeros(hsupport1,updateno);
fmicrophone=complex(zeros(hsupport1,updateno));
afmicrophone=zeros(hsupport1,updateno);

G = zeros(hsupport1, hsupport1);
zerovec = zeros(hsupport1,1);
zeromat = zeros(hsupport1);

% Reset sums
mmxs_a = zerovec;
mmys_a = zerovec;
s2xs_a = zerovec;
s2ys_a = zerovec;
Rxxs_a = zeromat;
Ryxs_a = zeromat;
count_a = 1;

mmxs_b = zerovec;
mmys_b = zerovec;
s2xs_b = zerovec;
s2ys_b = zerovec;
Rxxs_b = zeromat;
Ryxs_b = zeromat;
count_b = 1;

nog=0;

aaa=zeros(size(TheFarEnd));

% loop over signal blocks
fprintf(1,'.. Suppression; averaging G over %5.1f seconds; file length %5.1f seconds ..\n',avtime, length(microphone)/samplingfreq);
fprintf(1,'.. SUPPRESSING ONLY AFTER %5.1f SECONDS! ..\n',avtime);
fprintf(1,'.. 20 seconds is good ..\n');
hh = waitbar_j(0,'Please wait...');


for i=1:updateno

    sb = (i-1)*updatel + 1;
    se=sb+support-1;
    
    % analysis FFTs
    temp=fft(win .* TheFarEnd(sb:se));
    fTheFarEnd(:,i)=temp(1:hsupport1);
    xf=fTheFarEnd(:,i);
    afTheFarEnd(:,i)= abs(fTheFarEnd(:,i));
    
    temp=win .* microphone(sb:se);
    
    temp=fft(win .* microphone(sb:se));
    fmicrophone(:,i)=temp(1:hsupport1);
    yf=fmicrophone(:,i);
    afmicrophone(:,i)= abs(fmicrophone(:,i));

    
    ener_orig = afmicrophone(:,i)'*afmicrophone(:,i);
    if( ener_orig == 0)
        afmicrophone(:,i)=lowlevel*ones(size(afmicrophone(:,i)));
    end
    
    
    	% use log domain (showed improved performance)
xxf= sqrt(real(xf.*conj(xf))+1e-20);
yyf= sqrt(real(yf.*conj(yf))+1e-20);
        sxAll2(:,i) = 20*log10(xxf);
	syAll2(:,i) = 20*log10(yyf);

       mD=min(i-1,maxDelayb);
      xthreshold = sum(sxAll2(:,i-mD:i),2)/(maxDelayb+1);

      [yout, z200] = filter(Bp200,Ap200,syAll2(:,i),z200,2);
      yout=yout/(maxDelayb+1);
      ythreshold = mean(syAll2(:,i-mD:i),2);
      

  bxspectrum(i)=getBspectrum(sxAll2(:,i),xthreshold,bandfirst,bandlast);
  byspectrum(i)=getBspectrum(syAll2(:,i),yout,bandfirst,bandlast);

  bxhist(end-mD:end)=bxspectrum(i-mD:i);
  
  bcount(:,i)=hisser2( ...
     byspectrum(i),flipud(bxhist),bandfirst,bandlast);
 
 
  [fout(:,i), z500] = filter(Bp500,Ap500,bcount(:,i),z500,2);
  fcount(:,i)=sum(bcount(:,max(1,i-histLenb+1):i),2); % using the history range
 fout(:,i)=round(fout(:,i)); 
  [value,delay(i)]=min(fout(:,i),[],1);
  tdelay(i)=(delay(i)-1)*support/(samplingfreq*oversampling);

    % compensate

    idel =  max(i - delay(i) + 1,1);
    
  
    % echo suppression
    
    noisyspec = afmicrophone(:,i);
    
    % Estimate G using covariance matrices
    
    % Cumulative estimates    
    xx = afTheFarEnd(:,idel);
    yy = afmicrophone(:,i);
    
    % Means
    mmxs_a = mmxs_a + xx;
    mmys_a = mmys_a + yy;
    if (G_ol)
        mmxs_b = mmxs_b + xx;  
        mmys_b = mmys_b + yy;
        mmy = mean([mmys_a/count_a mmys_b/count_b],2);
        mmx = mean([mmxs_a/count_a mmxs_b/count_b],2);
    else
        mmx = mmxs_a/count_a;   
        mmy = mmys_a/count_a;   
    end
    count_a = count_a + 1;
    count_b = count_b + 1;
    
    % Mean removal
    xxm = xx - mmx;
    yym = yy - mmy;
    
    % Variances
    s2xs_a = s2xs_a +  xxm .* xxm;
    s2ys_a = s2ys_a +  yym .* yym;
    s2xs_b = s2xs_b +  xxm .* xxm;
    s2ys_b = s2ys_b +  yym .* yym;
    
    % Correlation matrices  
    Rxxs_a = Rxxs_a + xxm * xxm';
    Ryxs_a = Ryxs_a + yym * xxm';
    Rxxs_b = Rxxs_b + xxm * xxm';
    Ryxs_b = Ryxs_b + yym * xxm';
    
    
    % Gain matrix A
    
    if mod(i, estLen) == 0
        
        
        % Cumulative based estimates
        Rxxf = Rxxs_a / (estLen - 1);
        Ryxf = Ryxs_a / (estLen - 1);
        
        % Variance normalization
        s2x2 = s2xs_a / (estLen - 1);
        s2x2 = sqrt(s2x2);
       % Sx = diag(max(s2x2,dynrange*max(s2x2)));
        Sx = diag(s2x2);
        if (sum(s2x2) > 0)
          iSx = inv(Sx);
         else
                 iSx= Sx + 0.01;
         end
             
        s2y2 = s2ys_a / (estLen - 1);
        s2y2 = sqrt(s2y2);
       % Sy = diag(max(s2y2,dynrange*max(s2y2)));
        Sy = diag(s2y2);
        iSy = inv(Sy);        
        rx = iSx * Rxxf * iSx;
        ryx = iSy * Ryxf * iSx;
        
     
        
        dbd= 7; % Us less than the full matrix
        
        % k x m
        % Bandlimited structure on G
        LSEon = 0; % Default is using MMSE
        if (LSEon)
            ryx = ryx*rx;
            rx = rx*rx;
        end
        p = dbd-1;
        gaj = min(min(hsupport1,2*p+1),min([p+(1:hsupport1); hsupport1+p+1-(1:hsupport1)]));
        cgaj = [0 cumsum(gaj)];
        
        G3 = zeros(hsupport1);
        for kk=1:hsupport1
            ki = max(0,kk-p-1);
            if (sum(sum(rx(ki+1:ki+gaj(kk),ki+1:ki+gaj(kk))))>0)
               G3(kk,ki+1:ki+gaj(kk)) = ryx(kk,ki+1:ki+gaj(kk))/rx(ki+1:ki+gaj(kk),ki+1:ki+gaj(kk));
           else
               G3(kk,ki+1:ki+gaj(kk)) = ryx(kk,ki+1:ki+gaj(kk));
           end
        end
        % End Bandlimited structure
        
        G = G3;
        G(abs(G)<0.01)=0;
        G = suppress_overdrive * Sy * G * iSx;
        
        if 1
            figure(32); mi=2;
            surf(max(min(G,mi),-mi)); view(2)
            title('Unscaled Masked Limited-bandwidth G');
        end
        pause(0.05);
        
        % Reset sums
        mmxs_a = zerovec;
        mmys_a = zerovec;
        s2xs_a = zerovec;
        s2ys_a = zerovec;
        Rxxs_a = zeromat;
        Ryxs_a = zeromat;
        count_a = 1;
        
    end
    
    if (G_ol)    
        % Gain matrix B
        
        if ((mod((i-estLen/2), estLen) == 0) & i>estLen)
            
            
            % Cumulative based estimates
            Rxxf = Rxxs_b / (estLen - 1);
            Ryxf = Ryxs_b / (estLen - 1);
            
            % Variance normalization
            s2x2 = s2xs_b / (estLen - 1);
            s2x2 = sqrt(s2x2);
            Sx = diag(max(s2x2,dynrange*max(s2x2)));
            iSx = inv(Sx);
            s2y2 = s2ys_b / (estLen - 1);
            s2y2 = sqrt(s2y2);
            Sy = diag(max(s2y2,dynrange*max(s2y2)));
            iSy = inv(Sy);        
            rx = iSx * Rxxf * iSx;
            ryx = iSy * Ryxf * iSx;
            
            
            % Bandlimited structure on G
            LSEon = 0; % Default is using MMSE
            if (LSEon)
                ryx = ryx*rx;
                rx = rx*rx;
            end
            p = dbd-1;
            gaj = min(min(hsupport1,2*p+1),min([p+(1:hsupport1); hsupport1+p+1-(1:hsupport1)]));
            cgaj = [0 cumsum(gaj)];
            
            G3 = zeros(hsupport1);
            for kk=1:hsupport1
                ki = max(0,kk-p-1);
                G3(kk,ki+1:ki+gaj(kk)) = ryx(kk,ki+1:ki+gaj(kk))/rx(ki+1:ki+gaj(kk),ki+1:ki+gaj(kk));
            end
            % End Bandlimited structure
            
            G = G3;
            G(abs(G)<0.01)=0;
            G = suppress_overdrive * Sy * G * iSx;
            
            if 1
                figure(32); mi=2;
                surf(max(min(G,mi),-mi)); view(2)
                title('Unscaled Masked Limited-bandwidth G');
            end
            pause(0.05);
            
            
            % Reset sums
            mmxs_b = zerovec;
            mmys_b = zerovec;
            s2xs_b = zerovec;
            s2ys_b = zerovec;
            Rxxs_b = zeromat;
            Ryxs_b = zeromat;
            count_b = 1;
            
        end
        
    end
    
    FECestimate2 = G*afTheFarEnd(:,idel);
    
    % compute Wiener filter and suppressor function
    thefilter(:,i) = (noisyspec - gamma_echo*FECestimate2) ./ noisyspec;
    ix0 = find(thefilter(:,i)<de_echo_bound);   % bounding trick 1
    thefilter(ix0,i) = de_echo_bound;     % bounding trick 2
    ix0 = find(thefilter(:,i)>1);   % bounding in reasonable range
    thefilter(ix0,i) = 1;
    
    % NONLINEARITY
    nl_alpha=0.8;    % memory; seems not very critical
    nlSeverity=0.3;  % nonlinearity severity: 0 does nothing; 1 suppresses all
    thefmean=mean(thefilter(8:16,i));
    if (thefmean<1)
        disp('');
    end
    runningfmean = nl_alpha*runningfmean + (1-nl_alpha)*thefmean;
    aaa(sb+20+1:sb+20+updatel)=10000*runningfmean* ones(updatel,1); % debug
    slope0=1.0/(1.0-nlSeverity); %
    thegain = max(0.0,min(1.0,slope0*(runningfmean-nlSeverity)));
    % END NONLINEARITY
    thefilter(:,i) = thegain*thefilter(:,i);
    
    
    % Wiener filtering
    femicrophone(:,i) = fmicrophone(:,i) .* thefilter(:,i);
    thelimiter(:,i) = (noisyspec - A_GAIN*FECestimate2) ./ noisyspec;
    index = find(thelimiter(:,i)>1.0);
    thelimiter(index,i) = 1.0;
    index = find(thelimiter(:,i)<0.0);
    thelimiter(index,i) = 0.0;
    
    if (rem(i,floor(updateno/20))==0)
        fprintf(1,'.');
    end
    if mod(i,50)==0
        waitbar_j(i/updateno,hh); 
    end
    
    
    % reconstruction; first make spectrum odd
    temp=[femicrophone(:,i);flipud(conj(femicrophone(2:hsupport,i)))];
    emicrophone(sb:se) = emicrophone(sb:se) + factor * win .* real(ifft(temp));

end
fprintf(1,'\n');

close(hh);