comparison nonExposed/adjustScaleForEBR.m @ 14:b1901e8d8f5f

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