mathieu@14
|
1 function scale = adjustScaleForEBR (fg, bg, offset, targetEBR, snrType, eps)
|
mathieu@14
|
2 % Computes the value of 'scale' such that fg*scale has a given EBR
|
mathieu@14
|
3 % relatively to bg at position 'offset'.
|
mathieu@14
|
4 % ebrType is used to select what kind of EBR should be considered:
|
mathieu@14
|
5 % 1 - standard temporal ebr over whole length of sample
|
mathieu@14
|
6 % 2 - max of standard temporal ebr over 1/4s windows
|
mathieu@14
|
7 % 3 - more to come ?
|
mathieu@14
|
8 % eps is the epsilon used to check for convergence; optional, defaults to
|
mathieu@14
|
9 % 0.01
|
mathieu@14
|
10
|
mathieu@14
|
11 % This program was written by Mathias Rossignol & Grégoire Lafay
|
mathieu@14
|
12 % is Copyright (C) 2015 IRCAM <http://www.ircam.fr>
|
mathieu@14
|
13 %
|
mathieu@14
|
14 % This program is free software: you can redistribute it and/or modify it
|
mathieu@14
|
15 % under the terms of the GNU General Public License as published by the Free
|
mathieu@14
|
16 % Software Foundation, either version 3 of the License, or (at your option)
|
mathieu@14
|
17 % any later version.
|
mathieu@14
|
18 %
|
mathieu@14
|
19 % This program is distributed in the hope that it will be useful, but
|
mathieu@14
|
20 % WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
mathieu@14
|
21 % or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
mathieu@14
|
22 % for more details.
|
mathieu@14
|
23 %
|
mathieu@14
|
24 % You should have received a copy of the GNU General Public License along
|
mathieu@14
|
25 % with this program. If not, see <http://www.gnu.org/licenses/>.
|
mathieu@14
|
26
|
mathieu@14
|
27 if (nargin == 5) eps = 0.01; end
|
mathieu@14
|
28 %if (targetSNR <= 0) targetSNR = exp(targetSNR); end
|
mathieu@14
|
29 prevScale = 1;
|
mathieu@14
|
30 scale = 1;
|
mathieu@14
|
31 ebrFunc=@ebr;
|
mathieu@14
|
32 if (snrType==2) ebrFunc=@maxWindowEBR; end
|
mathieu@14
|
33 count = 0;
|
mathieu@14
|
34 while (ebrFunc(fg, bg, offset, scale) > targetEBR && count < 100)
|
mathieu@14
|
35 prevScale = scale;
|
mathieu@14
|
36 scale = scale/2;
|
mathieu@14
|
37 count = count+1;
|
mathieu@14
|
38 end
|
mathieu@14
|
39 if (count == 100)
|
mathieu@14
|
40 msg = ['Max count, stage 1, scale = ', num2str(scale), ' - ', num2str(ebrFunc(fg, bg, offset, scale)), ' - ', num2str(ebrFunc(fg, bg, offset, prevScale)), ' - ', num2str(ebrFunc(fg, bg, offset, 1))];
|
mathieu@14
|
41 error(msg)
|
mathieu@14
|
42 end
|
mathieu@14
|
43 count=0;
|
mathieu@14
|
44 while (ebrFunc(fg, bg, offset, scale) < targetEBR && count < 100)
|
mathieu@14
|
45 prevScale = scale;
|
mathieu@14
|
46 scale = scale*2;
|
mathieu@14
|
47 count = count+1;
|
mathieu@14
|
48 end
|
mathieu@14
|
49 if (count == 100)
|
mathieu@14
|
50 msg = ['Max count, stage 2, scale = ', num2str(scale)];
|
mathieu@14
|
51 error(msg)
|
mathieu@14
|
52 end
|
mathieu@14
|
53 count=0;
|
mathieu@14
|
54 if (scale<prevScale)
|
mathieu@14
|
55 a = scale;
|
mathieu@14
|
56 b = prevScale;
|
mathieu@14
|
57 else
|
mathieu@14
|
58 a=prevScale;
|
mathieu@14
|
59 b=scale;
|
mathieu@14
|
60 end
|
mathieu@14
|
61 delta = ebrFunc(fg, bg, offset, (a+b)/2) - targetEBR;
|
mathieu@14
|
62 while (abs(delta) > eps && count < 100)
|
mathieu@14
|
63 if (delta>0)
|
mathieu@14
|
64 b = (a+b)/2;
|
mathieu@14
|
65 else
|
mathieu@14
|
66 a = (a+b)/2;
|
mathieu@14
|
67 end
|
mathieu@14
|
68 delta = ebrFunc(fg, bg, offset, (a+b)/2) - targetEBR;
|
mathieu@14
|
69 count = count+1;
|
mathieu@14
|
70 end
|
mathieu@14
|
71 if (count == 100)
|
mathieu@14
|
72 msg = ['Max count, stage 3, scale = ', num2str(a)];
|
mathieu@14
|
73 error(msg)
|
mathieu@14
|
74 end
|
mathieu@14
|
75 scale = (a+b)/2;
|
mathieu@14
|
76 end |